Adaptive heartbeat interval adjustment method and system

By setting up TCP connections with different heartbeat intervals between the client and the server, and adjusting the heartbeat interval range according to the disconnection situation, the problem of time-consuming heartbeat interval adjustment in the existing technology is solved, and the method of quickly finding the most suitable heartbeat interval is realized. Save resources and improve connection adaptability.

CN115695251BActive Publication Date: 2024-09-06SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202211108795.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-13
Publication Date
2024-09-06
Estimated Expiration
2042-09-13

AI Technical Summary

Technical Problem

In the existing technology, the method of adaptively calculating the heartbeat interval takes a long time, and it is difficult to quickly adjust the heartbeat interval of the TCP connection, resulting in a waste of resources.

Method used

By detecting the TCP connection environment between the client and the server, the range of the heartbeat interval is set, and different heartbeat interval values ​​are set in the two TCP connections, and the range is narrowed according to the disconnection situation until the most suitable heartbeat interval is found. Value, updates the heartbeat interval of the TCP connection.

Benefits of technology

It achieves rapid and effective adjustment of the heartbeat interval, saves resources, and improves the adaptability and reliability of TCP connections.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115695251B
    Figure CN115695251B
Patent Text Reader

Abstract

The present invention discloses an adaptive heartbeat interval adjustment method and system, which belongs to the technical field of communication between a client and a server. The technical problem to be solved is how to quickly and adaptively adjust the heartbeat interval of a TCP connection to save resources to a certain extent. The method comprises the following steps: S100, detecting whether the current environment needs to adjust the heartbeat interval; S200, setting the interval range of the heartbeat interval; S300, establishing two TCP connections between the client and the server; S400, calculating two different heartbeat interval values ​​based on the interval range of the heartbeat interval; S500, checking whether the first TCP connection and the second TCP connection are disconnected, and reducing the value range of the heartbeat interval according to the disconnection situation; repeatedly executing steps S400 and S500 until the value range of the heartbeat interval cannot be divided; S600, updating the heartbeat interval of the TCP connection.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention relates to the technical field of communication between a client and a server, and in particular to an adaptive heartbeat interval adjustment method and system. Background Art

[0002] Some current systems require the client and server to maintain reliable and real-time communication, which requires the client and server to maintain a TCP long connection. However, due to NAT timeout, DHCP expiration, routing node abnormality, etc., the link between the client and the server may be interrupted. Both ends of the TCP communication cannot detect the link interruption in time, affecting the reliability of communication. In order to prevent the link from being disconnected due to long-term lack of data transmission on the TCP long connection, data packets can be sent periodically between the two devices. This periodic sending of data packets is called a heartbeat. However, the actual heartbeat time interval is difficult to determine. If the interval is set too long, the connection may be disconnected. If the interval is too short, it will cause the loss of device traffic, power, and performance. Therefore, it is necessary to set a suitable heartbeat interval to maintain network connection and reduce resource loss.

[0003] In the existing technology, the most commonly used method is to use a fixed heartbeat interval. Although it is simple to implement, it has poor adaptability to the network. In recent literature, there are mainly two methods for adaptively calculating the heartbeat interval:

[0004] 1. Incremental method. The steps of the incremental method are as follows:

[0005] Step 1: Set the range of the heartbeat interval, the maximum heartbeat interval is H, the minimum heartbeat interval is L, and the step length is S;

[0006] Step 2: Use the client to establish a TCP long connection with the server, and set the heartbeat interval of the connection to C=L;

[0007] Step 3: If the connection is not disconnected after a period of time, set the heartbeat interval of the connection C=C+S;

[0008] Repeat step 3 until C=H and the connection is not disconnected, then go to step 4, otherwise go to step 5;

[0009] Step 4: Determine the most appropriate heartbeat interval C=H for the connection;

[0010] Step 5: Determine the most appropriate heartbeat interval C=CS for the connection.

[0011] 2. Dichotomy:

[0012] This method is based on the binary idea. Each time, the middle value of the heartbeat interval is taken as the test value. According to the test results, the heartbeat interval range is continuously narrowed until the most suitable heartbeat interval value is found.

[0013] The time spent on determining the heartbeat interval by using an incremental method is relatively long, and the time spent on determining the heartbeat interval by using a binary method is relatively short.

[0014] How to quickly and adaptively adjust the heartbeat time interval of the TCP connection to save resources to a certain extent is a technical problem that needs to be solved. Summary of the invention

[0015] The technical task of the present invention is to provide an adaptive heartbeat interval adjustment method and system to solve the technical problem of how to quickly and adaptively adjust the heartbeat time interval of a TCP connection and save resources to a certain extent.

[0016] In a first aspect, the present invention provides an adaptive heartbeat interval adjustment method for adjusting the heartbeat interval of a TCP connection between a client and a server, the method comprising the following steps:

[0017] S100, detecting whether the current environment needs to adjust the heartbeat interval of the TCP connection between the client and the server, if the heartbeat interval adjustment condition is met, executing the next step;

[0018] S200, setting a heartbeat interval range, wherein the range includes a maximum heartbeat interval value H, a minimum heartbeat interval value L, and a step length S;

[0019] S300, establishing two TCP connections between the client and the server, namely a first TCP connection and a second TCP connection, wherein the first TCP connection and the second TCP connection work simultaneously, and a heartbeat interval value set for the second TCP connection is greater than a heartbeat interval value set for the first TCP connection;

[0020] S400, based on the interval range of the heartbeat interval, two different heartbeat interval values ​​are calculated, namely a first heartbeat interval value C1 and a second heartbeat interval value C2, and C2>C1, the first heartbeat interval value C1 is assigned to the first TCP connection, and the second heartbeat interval value C2 is assigned to the second TCP connection;

[0021] S500, after C1 time or C2 time, check whether the first TCP connection and the second TCP connection are disconnected, and reduce the value range of the heartbeat interval according to the disconnection situation;

[0022] Repeat step S400 and step S500 until the heartbeat interval value interval cannot be divided any further, and the corresponding heartbeat interval value is the most suitable heartbeat interval value;

[0023] S600: Update the heartbeat interval of the TCP connection based on the most suitable heartbeat interval value.

[0024] Preferably, the heartbeat interval adjustment condition is satisfied including:

[0025] The system restarts and starts;

[0026] The TCP connection failed to send a heartbeat packet.

[0027] Preferably, the first heartbeat interval value C1 is the median of the heartbeat interval value interval [L, H], and the second heartbeat interval value C2 is the median of the heartbeat interval value interval [C 1, H].

[0028] Preferably, the following method is used to check whether the first TCP connection and the second TCP connection are disconnected:

[0029] After the set heartbeat interval value is reached, a heartbeat packet is sent to the other party to detect whether the other party responds. If there is a response, it proves that the connection is not disconnected, otherwise it means that the connection has been disconnected;

[0030] The method of sending the heartbeat packet is to trigger the socket to send the heartbeat packet by setting a timer, or to trigger the socket to send the heartbeat packet through a thread after a sleep heartbeat interval.

[0031] Preferably, after the C1 time or the C2 time, checking whether the first TCP connection and the second TCP connection are disconnected, and narrowing the value range of the heartbeat interval according to the disconnection situation, including the following operations:

[0032] If the first TCP connection is disconnected after C1 time, H=C1-S is set, the adjusted heartbeat interval value range [L,H] is equal to [L,C1-S], and the first heartbeat interval value C1 and the second heartbeat interval value C2 are recalculated based on the adjusted heartbeat interval value range;

[0033] If the first TCP connection is not disconnected after C1 time, and the second TCP connection is disconnected after C2 time, H=C2-L, L=C1 is set, and the value range of the adjusted heartbeat interval [L, H] is equal to [C1, C2-L], and the first heartbeat interval value C1 and the second heartbeat interval value C2 are recalculated based on the value range of the adjusted heartbeat interval;

[0034] If the second TCP connection is not disconnected after C2 time, set L=C2, the adjusted heartbeat interval value range [L, H] is equal to [C 2, H], and the first heartbeat interval value C1 and the second heartbeat interval value C2 are recalculated based on the adjusted heartbeat interval value range;

[0035] For the value range [L, H] of the adjusted heartbeat interval, when H <= L + S, it indicates that the interval can no longer be divided. At this time, the most appropriate heartbeat time interval can be obtained. When H = L + S, the most appropriate time interval is L. When H < L + S, the most appropriate time interval is H.

[0036] In a second aspect, an adaptive heartbeat interval adjustment system of the present invention adjusts the heartbeat time interval of the TCP connection between the client and the server through the adaptive heartbeat interval adjustment method described in any item of the first aspect. The system includes:

[0037] A monitoring module for detecting whether the current environment requires adjustment of the heartbeat interval of the TCP connection between the client and the server;

[0038] A heartbeat interval parameter acquisition module for setting the interval range of the heartbeat interval. The interval range includes the maximum heartbeat interval value H, the minimum heartbeat interval value L, and the step size S;

[0039] A connection maintenance module for establishing two TCP connections between the client and the server, namely the first TCP connection and the second TCP connection. The first TCP connection and the second TCP connection work simultaneously, and the heartbeat interval value set for the second TCP connection is greater than the heartbeat interval value set for the first TCP connection;

[0040] An interval value calculation module for calculating two different heartbeat interval values based on the interval range of the heartbeat interval, namely the first heartbeat interval value C1 and the second heartbeat interval value C2, and C2 > C1. Assign the first heartbeat interval value C1 to the first TCP connection and assign the second heartbeat interval value C2 to the second TCP connection;

[0041] A connection detection module for checking whether the first TCP connection and the second TCP connection are disconnected after C1 time or C2 time, and narrowing the value range of the heartbeat interval according to the disconnection situation. Correspondingly, the interval value calculation module is used to calculate two different heartbeat interval values based on the interval range of the heartbeat interval until the value interval of the heartbeat interval can no longer be divided, and the corresponding heartbeat interval value is the most suitable heartbeat interval value;

[0042] An update module for updating the heartbeat interval of the TCP connection based on the most suitable heartbeat interval value.

[0043] Preferably, the satisfaction of the heartbeat interval adjustment condition includes:

[0044] The system restarts;

[0045] The TCP connection fails to send a heartbeat packet.

[0046] Preferably, the first heartbeat interval value C1 is the median of the heartbeat interval value range [L, H], and the second heartbeat interval value C2 is the median of the heartbeat interval value range [C1, H].

[0047] Preferably, the connection detection module is used to check whether the first TCP connection and the second TCP connection are disconnected by the following method:

[0048] After reaching the set heartbeat interval value, send a heartbeat packet to the other party to detect whether the other party responds. If there is a response, it proves that the connection is not disconnected; otherwise, it means that the connection has been disconnected.

[0049] The method of sending the heartbeat packet is to trigger the socket to send the heartbeat packet by setting a timer, or to trigger the socket to send the heartbeat packet by a thread after sleeping for the heartbeat interval time.

[0050] Preferably, the connection detection module is used to narrow down the value range of the heartbeat interval according to the disconnection situation by the following operations:

[0051] If the first TCP connection is disconnected after C1 time, set H = C1 - S. The adjusted heartbeat interval value range [L, H] is equivalent to [L, C1 - S], and recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted heartbeat interval value range.

[0052] If the first TCP connection is not disconnected after C1 time and the second TCP connection is disconnected after C2 time, set H = C2 - L and L = C1. The adjusted heartbeat interval value range [L, H] is equivalent to [C1, C2 - L], and recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted heartbeat interval value range.

[0053] If the second TCP connection is not disconnected after C2 time, set L = C2. The adjusted heartbeat interval value range [L, H] is equivalent to [C2, H], and recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted heartbeat interval value range.

[0054] For the adjusted heartbeat interval value range [L, H], when H <= L + S, it means that the interval can no longer be divided. At this time, the most suitable heartbeat time interval can be obtained. When H = L + S, the most suitable time interval is L; when H < L + S, the most suitable time interval is H.

[0055] The update module is used to update the heartbeat interval of the TCP connection by means of global variables or parameter transfer;

[0056] The heartbeat interval is updated by means of parameter variables, including: setting a global variable, which is read after each heartbeat cycle of the TCP connection ends;

[0057] The heartbeat interval is updated by means of parameter variables, including: the heartbeat interval value is transmitted to each TCP connection in the form of a parameter.

[0058] The adaptive heartbeat interval adjustment method and system of the present invention have the following advantages:

[0059] 1. Set the TCP interval range, and calculate two different heartbeat interval values ​​C1 and C2 based on the interval range, assign the above two heartbeat interval values ​​to the two set TCP connections, check whether the first TCP connection and the second TCP connection are disconnected after C1 and C2 time, and narrow the value range of the heartbeat interval according to the disconnection situation, and then reset the heartbeat interval values ​​C1 and C2 according to the adjusted value range, check whether the first TCP connection and the second TCP connection are disconnected again after C1 and C2 time, and narrow the value range of the heartbeat interval according to the disconnection situation, and so on, until the value range of the heartbeat interval can no longer be divided, and the corresponding heartbeat interval value is the most suitable heartbeat interval value. The heartbeat interval value can be quickly and effectively obtained by this method, which saves resources to a certain extent. BRIEF DESCRIPTION OF THE DRAWINGS

[0060] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following will describe the embodiments or prior art.

[0061] The drawings required for the technical description are briefly introduced. Obviously, the drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, it is easy to understand without inventiveness.

[0062] On the premise of labor, other drawings can also be obtained based on these drawings.

[0063] The present invention is further described below in conjunction with the accompanying drawings.

[0064] Figure 1 It is a flowchart of the adaptive heartbeat interval adjustment method of Example 1;

[0065] Figure 2 Schematic diagram of an example of calculating the heartbeat interval in the adaptive heartbeat interval adjustment method of embodiment 1; Figure 3 This is a schematic diagram of an example of calculating the time consumption in the adaptive heartbeat interval adjustment method in Example 1. DETAILED DESCRIPTION

[0066] The present invention will be further described below in conjunction with the accompanying drawings and specific embodiments so that those skilled in the art can better understand the present invention and implement it. However, the embodiments are not intended to limit the present invention.

[0067] In the absence of conflict, the embodiments of the present invention and the technical features in the embodiments may be combined with each other.

[0068] 5 Embodiment 1:

[0069] The present invention provides an adaptive heartbeat interval adjustment method for adjusting the heartbeat interval between a client and a server.

[0070] The heartbeat time interval of the TCP connection, the method includes the following six steps.

[0071] Step S100: Detect whether the current environment requires adjustment of the heartbeat interval of the TCP connection between the client and the server. If the heartbeat interval adjustment condition is met, execute the next step.

[0072] 0 In step S100 of this embodiment, it is monitored whether the heartbeat interval needs to be adjusted.

[0073] The logic process of adjusting the heartbeat interval will be triggered only when the system is started,

[0074] When the TCP service connection fails to send a heartbeat packet, and the above conditions are met, execute the subsequent steps.

[0075] S200, setting a range of heartbeat intervals, wherein the range includes a maximum heartbeat interval value H, a minimum heartbeat interval value L, and a step length S.

[0076] Step S200 sets the maximum heartbeat interval to H, the minimum heartbeat interval to L, and the step length to S; wherein the maximum heartbeat interval H and the minimum heartbeat interval L are an interval range, and the most suitable heartbeat interval should be found within this interval range. The size of this range is not limited, and this range can be a large interval or a small interval, such as [1,10] or [1,1000]. The step length S represents the accuracy, and can be set to 1, 2, 3, etc. based on experience, or the default S=1 can be set. For example, if the heartbeat interval is [1,10] and S=2 is set, the most suitable heartbeat interval will be calculated in the 6 interval values ​​of [1,3,5,7,9,10].

[0077] S300. Establish two TCP connections between the client and the server, namely a first TCP connection and a second TCP connection. The first TCP connection and the second TCP connection work simultaneously, and a heartbeat interval value set for the second TCP connection is greater than a heartbeat interval value set for the first TCP connection.

[0078] Step S300 maintains two TCP connections. The connection in this embodiment can be considered as a socket. The purpose of maintaining two TCP connections is to test whether the connection will be disconnected under different heartbeat intervals. The two connections work simultaneously. The heartbeat interval value set for the second connection must be greater than the interval value of the first connection. For example, when the heartbeat interval C1 of the first connection is set to 5, the heartbeat interval C2 of the second connection may be set to 7. Since the heartbeat interval of the second connection is greater than that of the first connection, when the first connection is disconnected, the second connection will also be disconnected. When the connection is disconnected, a new connection will be re-established for testing, that is, two connection tests are always maintained.

[0079] S400. Two different heartbeat interval values ​​are calculated based on the interval range of the heartbeat interval, namely a first heartbeat interval value C1 and a second heartbeat interval value C2, and C2>C1; the first heartbeat interval value C1 is assigned to the first TCP connection, and the second heartbeat interval value C2 is assigned to the second TCP connection.

[0080] Step S400 calculates the heartbeat intervals C1 and C2; C1 and C2 are the heartbeat interval values ​​of the first TCP connection and the second TCP connection respectively, where C1 is the median of the interval [L, H], and C2 is the median of the interval [C1, H]. For example, L = 1, H = 13, step size S = 1, C1 is the median 7 of the interval [1, 13], and C2 is the median 10 of the interval [7, 13].

[0081] S500. After C1 time or C2 time, check whether the first TCP connection and the second TCP connection are disconnected, and narrow the value range of the heartbeat interval according to the disconnection situation, and repeat steps S400 and S500 until the value range of the heartbeat interval can no longer be divided, and the corresponding heartbeat interval value is the most suitable heartbeat interval value.

[0082] In step S500 of this embodiment, when detecting whether the connection between C1 and C2 is disconnected, the detection method is to send a heartbeat packet to the other party after the set heartbeat interval to detect whether the other party responds. If there is a response, it proves that the connection is not disconnected, otherwise it means that the connection has been disconnected.

[0083] The sending of the heartbeat packet may be triggered by setting a timer to trigger the socket to send the heartbeat packet, or may be triggered by a thread to trigger the socket to send the heartbeat packet after a sleep heartbeat interval.

[0084] In step S500 of this embodiment, after the C1 time or the C2 time has passed, checking whether the first TCP connection and the second TCP connection are disconnected, and reducing the value range of the heartbeat interval according to the disconnection situation, includes the following operations:

[0085] (1) If the first TCP connection is disconnected after time C1, then set H = C1 - S. The range of the adjusted heartbeat interval [L, H] is equivalent to [L, C1 - S]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the range of the adjusted heartbeat interval.

[0086] (2) If the first TCP connection is not disconnected after time C1 and the second TCP connection is disconnected after time C2, then set H = C2 - L and L = C1. The range of the adjusted heartbeat interval [L, H] is equivalent to [C1, C2 - L]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the range of the adjusted heartbeat interval.

[0087] (3) If the second TCP connection is not disconnected after time C2, then set L = C2. The range of the adjusted heartbeat interval [L, H] is equivalent to [C2, H]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the range of the adjusted heartbeat interval.

[0088] For the range of the adjusted heartbeat interval [L, H], when H <= L + S, it means the interval can no longer be divided. At this time, the most suitable heartbeat time interval can be obtained. When H = L + S, the most suitable time interval is L. When H < L + S, the most suitable time interval is H.

[0089] As a specific implementation of resetting the maximum heartbeat interval H and the minimum heartbeat interval L, as Figure 2 shown, L = 1, H = 13, step size S = 1, C1 = 7, C2 = 10. If the first TCP connection is disconnected when time C1 arrives, then set H = C1 - S = 6, and re - divide the new interval as [1, 6]. Continue to calculate the values of C1 and C2 in the interval [1, 6]; if the first TCP connection is not disconnected when time C1 arrives and the second connection is disconnected when time C2 arrives, then set L = C1 = 7 and H = C2 - 1 = 9, and the new interval is [7, 9]; if the second TCP connection is not disconnected when time C2 arrives, then set L = C2 = 10, and the new interval is [10, 13].

[0090] S600. Update the heartbeat interval of the TCP connection based on the most suitable heartbeat interval value.

[0091] The update method in step S600 can be to set a global variable, and each TCP service connection reads this global variable after each heartbeat cycle, or it can be through parameter passing, that is, passing the heartbeat interval value to each TCP service connection in the form of a parameter. This embodiment does not make a limitation.

[0092] There is no limitation on when to update the heartbeat interval of the TCP service connection. It can be updated after calculating the most appropriate heartbeat interval, or it can be continuously updated during the calculation process.

[0093] For example, this embodiment takes the heartbeat interval interval [1,60], step size S=1, unit is minute, and the most suitable heartbeat interval is 40 minutes as an example. At the 30th, 67th, 85th, and 126th minutes, respectively, the connection will not be disconnected when the heartbeat interval is 30, 37, and 40. Therefore, the heartbeat interval of the TCP service connection can be updated to 30 at the 30th minute, and the heartbeat interval of the TCP service connection can be updated to 37 at the 67th minute. It can also be directly updated to 40 at the 126th minute.

[0094] The logic of adjusting the heartbeat interval can be started in a new thread for execution, or it can be executed in an existing thread, which is not limited in this embodiment. The initial heartbeat interval of the TCP service connection can be set to the minimum value L of the heartbeat interval, or a default value can be set based on experience, which is not limited in this embodiment. In order to save resources, after the heartbeat interval value of the service TCP connection is updated, the two TCP connections used for the test will be released.

[0095] Figure 3 This is a diagram of the adaptive heartbeat interval adjustment of the heartbeat sending time in this embodiment. Taking the heartbeat interval interval [1,60], the step size S=1, the unit is minute, and the most suitable heartbeat period is 40 minutes as an example, the most suitable heartbeat interval calculation process is as follows:

[0096] Step 1: Calculate the heartbeat intervals C1=30 and C2=45, set the heartbeat interval of test connection 1 to 30, and the heartbeat interval of test connection 2 to 45; 30 minutes later, connection 1 sends a heartbeat packet and is found to be not disconnected; 45 minutes later, connection 2 sends a heartbeat packet and is found to be disconnected; the new heartbeat interval range is [30,44];

[0097] Step 2: Calculate the heartbeat intervals C1=37 and C2=40, set the heartbeat interval of test connection 1 to 37, and the heartbeat interval of test connection 2 to 40. After 67 minutes, connection 1 sends a heartbeat packet and is not disconnected; after 85 minutes, connection 2 sends a heartbeat packet and is not disconnected; the new heartbeat interval range is [40,44];

[0098] Step 3: Calculate the heartbeat intervals C1=42 and C2=43, set the heartbeat interval of test connection 1 to 42, and the heartbeat interval of test connection 2 to 43. After 109 minutes, connection 1 sends a heartbeat packet and finds that the connection is disconnected; at this time, there is no need for connection 2 to send a heartbeat packet after the 128th minute.

[0099] Step 4: At this time, there are 41 untested. Since connection 2 sent a heartbeat packet at the 85th minute, connection 2 sends another heartbeat packet at the 126th minute, and the most appropriate heartbeat interval can be obtained;

[0100] Step 5: The most suitable heartbeat interval value is 40, which takes 126 minutes.

[0101] The method of this embodiment and the method in the recent literature calculate the time consumption required for the most appropriate heartbeat interval. Assuming L=1, H=60, S=1, and the most appropriate heartbeat intervals are 10, 20, 30, 40, 50, and 60 respectively, the time consumed by different methods is different. The method of this embodiment can effectively shorten the time consumed to calculate the most appropriate heartbeat interval when compared with the incremental method and the binary method.

[0102] Embodiment 2:

[0103] The present invention discloses an adaptive heartbeat interval adjustment system, comprising a monitoring module, a heartbeat interval parameter acquisition module, a connection maintenance module, an interval value calculation module, a connection detection module and an update module. The system adjusts the heartbeat time interval of a TCP connection between a client and a server through the method disclosed in Example 1.

[0104] The monitoring module is used to detect whether the current environment requires adjustment of the heartbeat interval of the TCP connection between the client and the server.

[0105] The monitoring module is used to monitor whether the heartbeat interval needs to be adjusted. The logical process of adjusting the heartbeat interval will only be triggered when specific conditions are met. The heartbeat interval adjustment conditions are when the system is started and when the TCP service connection fails to send a heartbeat packet. When any of the above conditions is met, the subsequent steps are executed.

[0106] The heartbeat interval parameter acquisition module is used to set the interval range of the heartbeat interval, and the interval range includes the maximum heartbeat interval value H, the minimum heartbeat interval value L and the step size S.

[0107] Among them, the maximum heartbeat interval H and the minimum heartbeat interval L are an interval range. The most suitable heartbeat interval should be found within this interval. There is no limit on the size of this range. This range can be a large interval or a small interval, such as [1,10] or [1,1000]. The step size S represents the accuracy. It can be set to 1, 2, 3, etc. based on experience, or it can be set to the default S=1. For example, if the heartbeat interval is [1,10] and S=2 is set, the most suitable heartbeat interval will be calculated in the 6 interval values ​​[1,3,5,7,9,10].

[0108] The connection maintenance module is used to establish two TCP connections between the client and the server, namely a first TCP connection and a second TCP connection. The first TCP connection and the second TCP connection work simultaneously, and the heartbeat interval value set for the second TCP connection is greater than the heartbeat interval value set for the first TCP connection.

[0109] The connection maintenance module is used to maintain two TCP connections. The connection in this embodiment can be considered as a socket. The purpose of maintaining two TCP connections is to test whether the connection will be disconnected under different heartbeat intervals. The two connections work simultaneously. The heartbeat interval value set for the second connection must be greater than the interval value of the first connection. For example, when the heartbeat interval C1 of the first connection is set to 5, the heartbeat interval C2 of the second connection may be set to 7. Since the heartbeat interval of the second connection is greater than that of the first connection, when the first connection is disconnected, the second connection will also be disconnected. When the connection is disconnected, a new connection will be re-established for testing, that is, two connection tests are maintained all the time.

[0110] The interval value calculation module is used to calculate two different heartbeat interval values ​​based on the interval range of the heartbeat interval, namely a first heartbeat interval value C1 and a second heartbeat interval value C2, and C2>C1, and assign the first heartbeat interval value C1 to the first TCP connection, and assign the second heartbeat interval value C2 to the second TCP connection.

[0111] The interval value calculation module of this embodiment is used to calculate the heartbeat intervals C1 and C2; C1 and C2 are the heartbeat interval values ​​of the first TCP connection and the second TCP connection respectively, where C1 is the median of the interval [L, H], and C2 is the median of the interval [C1, H]. For example, L = 1, H = 13, step size S = 1, C1 is the median 7 of the interval [1, 13], and C2 is the median 10 of the interval [7, 13].

[0112] The connection detection module is used to check whether the first TCP connection and the second TCP connection are disconnected after C1 time or C2 time, and narrow the value range of the heartbeat interval according to the disconnection situation. Correspondingly, the interval value calculation module is used to calculate two different heartbeat interval values ​​based on the interval range of the heartbeat interval until the value interval of the heartbeat interval can no longer be divided, and the corresponding heartbeat interval value is the most suitable heartbeat interval value.

[0113] When detecting whether the C1 and C2 time connections are disconnected, the detection method is to send a heartbeat packet to the other party after the set heartbeat interval to detect whether the other party responds. If there is a response, it proves that the connection is not disconnected, otherwise it means that the connection has been disconnected.

[0114] The heartbeat packet can be sent by setting a timer to trigger the socket to send the heartbeat packet, or by a thread to trigger the socket to send the heartbeat packet after the sleep heartbeat interval.

[0115] After C1 time or C2 time, check whether the first TCP connection and the second TCP connection are disconnected, and narrow down the value range of the heartbeat interval according to the disconnection situation, including the following operations:

[0116] (1) If the first TCP connection is disconnected after C1 time, set H = C1 - S, and the adjusted value range of the heartbeat interval [L, H] is equivalent to [L, C1 - S]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted value range of the heartbeat interval;

[0117] (2) If the first TCP connection is not disconnected after C1 time and the second TCP connection is disconnected after C2 time, set H = C2 - L, L = C1, and the adjusted value range of the heartbeat interval [L, H] is equivalent to [C1, C2 - L]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted value range of the heartbeat interval;

[0118] (3) If the second TCP connection is not disconnected after C2 time, set L = C2, and the adjusted value range of the heartbeat interval [L, H] is equivalent to [C2, H]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted value range of the heartbeat interval.

[0119] For the adjusted value range of the heartbeat interval [L, H], when H <= L + S, it means that the interval can no longer be divided, and at this time, the most appropriate heartbeat time interval can be obtained. When H = L + S, the most appropriate time interval is L, and when H < L + S, the most appropriate time interval is H.

[0120] The update module is used to update the heartbeat interval of the TCP connection based on the most suitable heartbeat interval value.

[0121] The update method can be to set a global variable, and each TCP service connection reads this global variable after each heartbeat cycle, or by parameter passing, that is, passing the heartbeat interval value to each TCP service connection in the form of a parameter. This embodiment does not make a limitation.

[0122] Specifically, when to update the heartbeat interval of the TCP service connection is not limited here. It can be updated after calculating the most appropriate heartbeat interval, or continuously updated during the calculation process.

[0123] For example, taking the heartbeat interval interval [1,60], step size S=1, unit is minute, and the most suitable heartbeat interval is 40 minutes as an example, the connection will not be disconnected when the heartbeat interval is 30, 37, and 40 at the 30th, 67th, 85th, and 126th minutes respectively. Therefore, the heartbeat interval of the TCP service connection can be updated to 30 at the 30th minute, and the heartbeat interval of the TCP service connection can be updated to 37 at the 67th minute, or the heartbeat interval of the TCP service connection can be directly updated to 40 at the 126th minute.

[0124] The logic of adjusting the heartbeat interval can be started in a new thread for execution, or it can be executed in an existing thread, which is not limited in this embodiment. The initial heartbeat interval of the TCP service connection can be set to the minimum value L of the heartbeat interval, or a default value can be set based on experience, which is not limited in this embodiment. In order to save resources, after the heartbeat interval value of the service TCP connection is updated, the two TCP connections used for the test will be released.

[0125] The present invention has been shown and described in detail above through the accompanying drawings and preferred embodiments. However, the present invention is not limited to these disclosed embodiments. Based on the above multiple embodiments, those skilled in the art can know that the means in the above different embodiments can be combined to obtain more embodiments of the present invention, and these embodiments are also within the protection scope of the present invention.

Claims

1. An adaptive heartbeat interval adjustment method, It is characterized in that The method is used to adjust the heartbeat time interval of the TCP connection between the client and the server, and comprises the following steps: S100, detecting whether the current environment needs to adjust the heartbeat interval of the TCP connection between the client and the server, and if the heartbeat interval adjustment condition is met, executing the next step, wherein the heartbeat interval adjustment condition includes: system restart; TCP connection fails to send a heartbeat packet; S200, setting a heartbeat interval range, wherein the range includes a maximum heartbeat interval value H, a minimum heartbeat interval value L, and a step length S; S300, establishing two TCP connections between the client and the server, namely a first TCP connection and a second TCP connection, wherein the first TCP connection and the second TCP connection work simultaneously, and a heartbeat interval value set for the second TCP connection is greater than a heartbeat interval value set for the first TCP connection; S400, based on the interval range of the heartbeat interval, two different heartbeat interval values ​​are calculated, namely a first heartbeat interval value C1 and a second heartbeat interval value C2, and C2>C1, the first heartbeat interval value C1 is assigned to the first TCP connection, and the second heartbeat interval value C2 is assigned to the second TCP connection, the first heartbeat interval value C1 is the median of the heartbeat interval value interval [L, H], and the second heartbeat interval value C2 is the median of the heartbeat interval value interval [C 1, H ]; S500, after C1 time or C2 time, check whether the first TCP connection and the second TCP connection are disconnected, and reduce the value range of the heartbeat interval according to the disconnection situation; Repeat step S400 and step S500 until the heartbeat interval value interval cannot be divided any further, and the corresponding heartbeat interval value is the most suitable heartbeat interval value; S600, updating the heartbeat interval of the TCP connection based on the most suitable heartbeat interval value; Check whether the first TCP connection and the second TCP connection are disconnected by the following method: After the set heartbeat interval value is reached, a heartbeat packet is sent to the other party to detect whether the other party responds. If there is a response, it proves that the connection is not disconnected, otherwise it means that the connection has been disconnected; The method of sending the heartbeat packet is to trigger the socket to send the heartbeat packet by setting a timer, or to trigger the socket to send the heartbeat packet through a thread after a sleep heartbeat interval; After the C1 time or the C2 time has passed, check whether the first TCP connection and the second TCP connection are disconnected, and reduce the value range of the heartbeat interval according to the disconnection situation, including the following operations: If the first TCP connection is disconnected after C1 time, H is set to C1-S, the adjusted heartbeat interval value range [L, H] is equal to [L, C1-S], and the first heartbeat interval value C1 and the second heartbeat interval value C2 are recalculated based on the adjusted heartbeat interval value range; If the first TCP connection is not disconnected after time C1, and the second TCP connection is disconnected after time C2, then set H = C2 - L, L = C1, and the adjusted heartbeat interval range [L, H] is equivalent to [C1, C2 - L]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted heartbeat interval range. If the second TCP connection is not disconnected after time C2, set L = C2, and the adjusted heartbeat interval range [L, H] is equivalent to [C2, H]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted heartbeat interval range. For the adjusted heartbeat interval range [L, H], when H <= L + S, it means the interval can no longer be divided. At this time, the most suitable heartbeat time interval can be obtained. When H = L + S, the most suitable time interval is L. When H < L + S, the most suitable time interval is H.

2. An adaptive heartbeat interval adjustment system Characterized in that The heartbeat time interval between the client and the server is adjusted by the adaptive heartbeat interval adjustment method as described in Claim 1. The system includes: A monitoring module for detecting whether the current environment requires adjusting the heartbeat interval between the client and the server. The conditions for meeting the heartbeat interval adjustment include: the system restarts; the heartbeat packet sent by the TCP connection fails. A heartbeat interval parameter acquisition module for setting the interval range of the heartbeat interval, where the interval range includes the maximum heartbeat interval value H, the minimum heartbeat interval value L, and the step size S. A connection maintenance module for establishing two TCP connections between the client and the server, namely the first TCP connection and the second TCP connection. The first TCP connection and the second TCP connection work simultaneously, and the heartbeat interval value set for the second TCP connection is greater than the heartbeat interval value set for the first TCP connection. The first heartbeat interval value C1 is the median of the heartbeat interval value range [L, H], and the second heartbeat interval value C2 is the median of the heartbeat interval value range [C1, H]. An interval value calculation module for calculating two different heartbeat interval values based on the interval range of the heartbeat interval, namely the first heartbeat interval value C1 and the second heartbeat interval value C2, and C2 > C1. Assign the first heartbeat interval value C1 to the first TCP connection and the second heartbeat interval value C2 to the second TCP connection. A connection detection module for checking whether the first TCP connection and the second TCP connection are disconnected after time C1 or time C2, and narrowing the value range of the heartbeat interval according to the disconnection situation. Correspondingly, the interval value calculation module is used to calculate two different heartbeat interval values based on the interval range of the heartbeat interval until the value interval of the heartbeat interval can no longer be divided, and the corresponding heartbeat interval value is the most suitable heartbeat interval value. An update module, which is used to update the heartbeat interval of the TCP connection based on the most suitable heartbeat interval value; The connection detection module is used to check whether the first TCP connection and the second TCP connection are disconnected by the following method: After reaching the set heartbeat interval value, send a heartbeat packet to the other party to detect whether the other party responds. If there is a response, it proves that the connection is not disconnected; otherwise, it means that the connection has been disconnected; The method of sending the heartbeat packet is to trigger the socket to send the heartbeat packet by setting a timer, or to trigger the socket to send the heartbeat packet by a thread after sleeping for the heartbeat interval time; The connection detection module is used to narrow down the value range of the heartbeat interval according to the disconnection situation by the following operations: If the first TCP connection is disconnected after time C1, set H = C1 - S. The value range of the adjusted heartbeat interval [L, H] is equal to [L, C1 - S]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted value range of the heartbeat interval; If the first TCP connection is not disconnected after time C1 and the second TCP connection is disconnected after time C2, set H = C2 - L and L = C1. The value range of the adjusted heartbeat interval [L, H] is equal to [C1, C2 - L]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted value range of the heartbeat interval; If the second TCP connection is not disconnected after time C2, set L = C2. The value range of the adjusted heartbeat interval [L, H] is equal to [C2, H]. Recalculate the first heartbeat interval value C1 and the second heartbeat interval value C2 based on the adjusted value range of the heartbeat interval; For the value range of the adjusted heartbeat interval [L, H], when H <= L + S, it means that the interval can no longer be divided. At this time, the most suitable heartbeat time interval can be obtained. When H = L + S, the most suitable time interval is L. When H < L + S, the most suitable time interval is H; The update module is used to update the heartbeat interval of the TCP connection by means of global variables or parameter passing; Updating the heartbeat interval by means of parameter variables includes: setting a global variable, and each TCP connection reads this global variable after each heartbeat cycle ends; Updating the heartbeat interval by means of parameter variables includes: passing the heartbeat interval value to each TCP connection in the form of a parameter.