WO2022105611A1 - 一种心跳超时检测方法、服务器及电子设备 - Google Patents
一种心跳超时检测方法、服务器及电子设备 Download PDFInfo
- Publication number
- WO2022105611A1 WO2022105611A1 PCT/CN2021/128489 CN2021128489W WO2022105611A1 WO 2022105611 A1 WO2022105611 A1 WO 2022105611A1 CN 2021128489 W CN2021128489 W CN 2021128489W WO 2022105611 A1 WO2022105611 A1 WO 2022105611A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- timeout
- server
- client
- heartbeat
- time
- Prior art date
Links
- 238000001514 detection method Methods 0.000 title claims abstract description 159
- 238000004891 communication Methods 0.000 claims abstract description 116
- 230000004044 response Effects 0.000 claims abstract description 104
- 238000012790 confirmation Methods 0.000 claims description 55
- 238000000034 method Methods 0.000 claims description 25
- 101100129500 Caenorhabditis elegans max-2 gene Proteins 0.000 claims description 15
- 101100083446 Danio rerio plekhh1 gene Proteins 0.000 claims description 15
- 230000002708 enhancing effect Effects 0.000 abstract 1
- 101150060894 def2 gene Proteins 0.000 description 14
- 101150005874 DEF1 gene Proteins 0.000 description 10
- 101150060629 def gene Proteins 0.000 description 10
- 230000005540 biological transmission Effects 0.000 description 9
- 230000007246 mechanism Effects 0.000 description 9
- 230000003993 interaction Effects 0.000 description 7
- 230000000903 blocking effect Effects 0.000 description 3
- 238000010586 diagram Methods 0.000 description 3
- 230000008569 process Effects 0.000 description 3
- 230000002159 abnormal effect Effects 0.000 description 2
- 230000003044 adaptive effect Effects 0.000 description 2
- 230000006870 function Effects 0.000 description 2
- 230000002452 interceptive effect Effects 0.000 description 2
- 238000012986 modification Methods 0.000 description 2
- 230000004048 modification Effects 0.000 description 2
- 230000003068 static effect Effects 0.000 description 2
- 238000013473 artificial intelligence Methods 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 230000002457 bidirectional effect Effects 0.000 description 1
- 238000004364 calculation method Methods 0.000 description 1
- 230000008859 change Effects 0.000 description 1
- 230000001934 delay Effects 0.000 description 1
- 230000003111 delayed effect Effects 0.000 description 1
- 230000002542 deteriorative effect Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000010247 heart contraction Effects 0.000 description 1
- 230000000737 periodic effect Effects 0.000 description 1
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L43/00—Arrangements for monitoring or testing data switching networks
- H04L43/10—Active monitoring, e.g. heartbeat, ping or trace-route
-
- Y—GENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
- Y02—TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
- Y02D—CLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
- Y02D30/00—Reducing energy consumption in communication networks
- Y02D30/70—Reducing energy consumption in communication networks in wireless communication networks
Definitions
- the invention relates to the field of communication technologies, and in particular, to a heartbeat timeout detection method, a server and an electronic device.
- a communication connection between the client and the server may be established based on a communication protocol.
- the communication protocol is, for example, TCP (Transmission Control Protocol, Transmission Control Protocol), WebSocket protocol, etc.
- a heartbeat mechanism is introduced into the existing communication, and the client and the server periodically send heartbeat messages to each other. For example, the client sends a heartbeat message 1 to the server. If the network is good, the server will quickly send back to the client. Heartbeat message 2; if the client does not receive the heartbeat feedback from the server within the threshold time, that is, the heartbeat detection times out, the communication connection is considered unavailable and the connection is automatically disconnected.
- this method of detecting heartbeat timeout through a limited threshold time is prone to problems such as misjudgment of timeout, incorrect closing of communication connection, and weak anti-network jitter/blocking ability.
- the invention provides a heartbeat timeout detection method, server and electronic equipment, which can improve the accuracy of heartbeat timeout detection and enhance the adaptability of communication connection to network instability.
- the heartbeat timeout detection method provided by the embodiment of the first aspect, applied to a client includes:
- the client After the communication connection between the client and the server is successful, the client sends a heartbeat request message corresponding to the i-th detection period to the server, and records the sending time of the heartbeat request message;
- the client receives the response information returned by the server and belongs to the i-th detection cycle within the first timeout period currently recorded by the local end, it records the receiving time of the response information, and records the receiving time of the response information according to the sending time of the heartbeat request message. and the receiving time of the response information, update the first timeout time;
- the client If the client does not receive the response information that is returned by the server and belongs to the i-th detection period within the first timeout period, the client disconnects the communication connection with the server.
- the client updates the first timeout time as follows: calculating the time difference between the sending time of the heartbeat request message and the receiving time of the response message, and obtaining the client The round-trip time of the heartbeat corresponding to the ith detection period; according to the round-trip time of the heartbeat corresponding to the ith detection period of the client, the new first timeout period is adaptively matched.
- the client adaptively matches the new first timeout time according to the following formula
- Timeout Ai min(max(a 1 *RTT Ai , b 1 *cycle), Timeout max1 )
- Timeout Ai is the first timeout time
- cycle is the preset sending interval of two adjacent heartbeat request messages
- a 1 is the first coefficient set by the client
- b 1 is the second coefficient set by the client
- Timeout max1 is the maximum timeout time preset by the client
- RTT Ai is the round-trip time of the heartbeat corresponding to the ith detection period of the client.
- the method further includes: when the client receives the response information fed back by the server that belongs to the i-th detection period within the first timeout period, sending the client to the server Both belong to the heartbeat confirmation message in the i-th detection cycle, the heartbeat confirmation message is used to instruct the server to record the reception time of the heartbeat confirmation message, and the reception time of the heartbeat confirmation message is used to calculate the server-side corresponding to the i-th detection cycle The heartbeat round-trip time.
- the method further includes:
- the client After the client disconnects the jth communication connection with the server, it clears the i value recorded by the local end and sends a reconnection request to the server.
- the reconnection request is used to request the server to establish the j+1th communication connect;
- the client After the j+1th communication connection is successfully established, before receiving the response information fed back by the server for the first time, the client takes the preset first threshold time as the first timeout time.
- the first aspect is mainly aimed at the client side in the communication connection.
- the validity of the connection is detected through the heartbeat mechanism.
- the client periodically sends a heartbeat request message to the server.
- the server receives the response information within the same detection period returned by the server after responding to the heartbeat request message, the client considers that the connection is valid and waits for the next period of heartbeat detection. Otherwise, there is a heartbeat timeout problem. You can actively disconnect the communication connection with the server.
- the key point of the present application is that instead of using a fixed timeout period, the first timeout period can be adaptively adjusted according to the actual network state.
- Reqi is used to represent the heartbeat request message corresponding to the ith detection period.
- Res i is used to represent the response information fed back by the server after responding to Req i , that is, Req i and Res i are interactive messages that belong to the same i-th detection cycle.
- the client can determine the time consumed by the client in the ith heartbeat detection of the client through the sending time of the heartbeat request message Req i (denoted as T Asi ) and the reception time of the response information Res i (denoted as T ARi ), That is, the heartbeat round-trip time (referred to as RTT Ai ) corresponding to the i-th detection period of the client, the time length of the heartbeat round-trip time RTT Ai can be used to evaluate the current state of the communication network, and then adaptively matches the first timeout time, when the network is good , the first timeout time can be relatively reduced, for example, the preset first threshold time (the default heartbeat timeout time on the client side) is used as the first timeout time; when the network is unstable, such as network jitter, congestion, weak network In other cases, the first timeout period can be dynamically adjusted, and the first timeout period can be relatively increased to provide a longer time limit for receiving the response information from the server, so that the communication connection is easier to retain,
- the client can adaptively and dynamically adjust the first timeout of the local end according to the network status, and enhance the ability to adapt to the weak network, network jitter and congestion.
- the first timeout will not increase unlimitedly. To a certain extent, it can decide to timeout and disconnect, which improves the adaptability and accuracy of heartbeat timeout detection.
- the heartbeat timeout detection method provided by the embodiment of the second aspect, applied to the server side includes:
- the server After the communication connection between the server and the client is successful, the server receives the heartbeat request message corresponding to the ith detection period sent by the client;
- the server feeds back the response information that belongs to the i-th detection cycle to the client, and records the sending time of the response information;
- the server If the server receives the heartbeat confirmation message sent by the client and belongs to the i-th detection period within the second timeout period currently recorded by the local end, it records the receiving time of the heartbeat confirmation message, and sends the response message according to the sending time of the heartbeat confirmation message. time and the reception time of the heartbeat confirmation message, and update the second timeout time;
- the server If the server does not receive a heartbeat confirmation message sent by the client within the i-th detection period within the second timeout period, the server disconnects the communication connection with the client.
- the server determines to update the second timeout time in the following manner: calculating the time difference between the sending time of the response message and the receiving time of the heartbeat confirmation message, and obtaining the server-side The round-trip time of the heartbeat corresponding to the ith detection period; according to the round-trip time of the heartbeat corresponding to the ith detection period of the server, the new second timeout period is adaptively matched.
- the server adaptively matches the new second timeout period according to the following formula
- Timeout Bi min(max(a 2 *RTT Bi , b 2 *cycle), Timeout max2 )
- Timeout Bi is the second timeout time
- cycle is the preset sending interval of two adjacent heartbeat request messages
- a 2 is the third coefficient set by the server
- b 2 is the fourth coefficient set by the server
- Timeout max2 is the maximum timeout time preset by the server
- RTT Bi is the heartbeat round-trip time corresponding to the i-th detection cycle on the server side.
- the method further includes:
- the server After updating the second timeout time, the server uses the receiving time of the heartbeat acknowledgment message in the i-th detection period as the reference point, and detects whether the i+1-th message sent by the client is received within the updated second timeout time.
- the heartbeat request message corresponding to the detection period;
- the server If the server receives the heartbeat request message corresponding to the i+1th detection period sent by the client within the updated second timeout period, it will feed back the response information that belongs to the i+1th detection period to the client;
- the server If the server does not receive the heartbeat request message corresponding to the i+1 th detection period sent by the client within the second timeout period after the update, the server disconnects the communication connection with the client.
- the method further includes:
- the server After disconnecting the jth communication connection with the client, the server receives the reconnection request initiated by the client;
- the server In response to the reconnection request, the server establishes the j+1th communication connection with the client;
- the server After the j+1th communication connection is successfully established, the server takes the preset second threshold time as the second timeout time before receiving the heartbeat confirmation message sent by the client for the first time.
- the second aspect is mainly aimed at the server side in the communication connection.
- the validity of the connection is detected through the heartbeat mechanism.
- the client periodically sends a heartbeat request message to the server.
- the server receives a heartbeat request message
- the client sends a heartbeat confirmation message to the server after receiving a response information.
- Reqi is used to represent the heartbeat request message corresponding to the i -th detection period
- Res i is used to represent The server responds to the response information fed back by Reqi, and uses Sure i to represent the heartbeat confirmation message sent by the client to the server after receiving Res i , that is, Reqi, Res i , and Sure i belong to the same interaction message in the i -th detection cycle.
- the server can determine the time consumed by the message round trip in the i-th heartbeat detection on the server side through the sending time of the response information Res i (denoted as T BSi ) and the reception time of the heartbeat confirmation message Sure i (denoted as T BRi ), that is,
- the heartbeat round-trip time corresponding to the i-th detection period on the server side (referred to as RTT Bi ), the length of the heartbeat round-trip time RTT Bi can be used to evaluate the current state of the communication network, and then the server can adaptively match the second timeout time, and the network
- the second timeout time can be relatively reduced, for example, the preset second threshold time (the default heartbeat timeout time on the server side) is used as the second timeout time; when the network is unstable, such as network jitter, congestion, weak network, etc., the second timeout period can be dynamically adjusted, and the second timeout period can be relatively increased to provide a longer time limit for receiving messages sent by the peer end, so that the communication
- the server can adaptively and dynamically adjust the second timeout period of the local end according to the network status, so as to enhance the ability to adapt to the weak network, network jitter and congestion.
- the second timeout period will not increase unlimitedly. It can decide to timeout and disconnect when the degree of heartbeat timeout is reached, which improves the adaptability and accuracy of heartbeat timeout detection.
- An electronic device provided by an embodiment of the third aspect, the electronic device is installed with a client, and the client is configured to:
- the reception time of the response information is recorded, and according to the sending time of the heartbeat request message and all the reception time of the response information, and update the first timeout time;
- the communication connection with the server is disconnected.
- a server provided by an embodiment of the fourth aspect is configured as:
- a heartbeat confirmation message sent by the client that belongs to the i-th detection period is received, the receiving time of the heartbeat confirmation message is recorded, and according to the sending time of the response information and the receiving time of the heartbeat confirmation message, update the second timeout period;
- the communication connection with the client is disconnected.
- the electronic device provided by the embodiment of the fifth aspect includes: a memory and at least one processor; the memory is used to store instructions; the at least one processor is used to execute the instructions in the memory, and execute the above-mentioned first step.
- the memory is used to store instructions
- the at least one processor is used to execute the instructions in the memory, and execute the above-mentioned first step.
- the server provided by the embodiment of the sixth aspect includes: a memory and at least one processor; the memory is used to store instructions; the at least one processor is used to execute the instructions in the memory, and execute the above second The heartbeat timeout detection method according to any one of the aspects.
- the embodiment of the seventh aspect provides a computer-readable storage medium, including instructions, which, when executed on a computer, cause the computer to execute the heartbeat timeout detection method described in any one of the first aspect above.
- An embodiment of the eighth aspect provides a computer-readable storage medium, including instructions, which, when executed on a computer, cause the computer to execute the heartbeat timeout detection method described in any one of the second aspect above.
- Fig. 1 exemplarily shows a schematic diagram of a communication scenario
- Fig. 2 exemplarily shows a heartbeat timeout detection method performed by the client side
- Fig. 3 exemplarily shows a heartbeat timeout detection method performed by the server side
- FIG. 4(a) exemplarily shows a schematic diagram of the communication interaction between the client and the server under the mode of using a fixed timeout on both sides;
- FIG. 4( b ) exemplarily shows a schematic diagram of the communication interaction between the client and the server in the mode of dynamically adaptively adjusting the timeout time on both sides.
- module refers to any known or later developed hardware, software, firmware, artificial intelligence, fuzzy logic, or combination of hardware or/and software code capable of performing the functions associated with that element.
- the client 10 and the server 20 are two ends of communication interaction, and the client 10 may be installed in an actual electronic device, including but not limited to display devices, smart phones, Computers, etc.
- the client 10 is a program corresponding to the server that provides corresponding services to the client.
- the running form of the client 10 includes but is not limited to a web browser, an email client, and instant messaging client software.
- the client 10 and the server 20 can establish a communication connection based on some communication protocols, such as a TCP connection, a WebSocket connection, etc. After the client 10 and the server 20 are successfully connected, the required communication functions such as data transmission and information exchange can be realized.
- Full-duplex communication may be used between the client 10 and the server 20, that is, bidirectional data/information/message transmission can be simultaneously performed between the two ends.
- the communication between the client 10 and the server 20 is generally based on the network, and the network status is uncertain and unpredictable.
- the network is good, the communication between the two ends is stable and the data sending and receiving speed is fast; when the network is jittered, blocked, weak or even disconnected
- the communication between the client 10 and the server 20 will be disturbed. For example, there is a certain degree of lag in data transmission and reception. Even though the two ends are connected, they cannot receive the data sent by the other end. The phenomenon of "false connection".
- a heartbeat detection mechanism is generally introduced in the existing communication, and the client 10 and the server 20 can periodically send heartbeat messages to each other to ask whether the opposite end is "alive", so as to decide whether to disconnect the communication connection with the opposite end. For example, the client 10 periodically sends a heartbeat request to the server 20.
- the client 10 receives the response feedback from the server within the timeout period, it reflects that the current network is good, and the client 10 maintains the communication connection with the server 20; otherwise, if If the client 10 does not receive the response feedback from the server within the timeout period, it means that the current network is abnormal, and the client 10 can disconnect the invalid communication connection with the server.
- both ends of the client 10 and the server 20 set a fixed timeout period, and the timeout period will not change with factors such as the network and the communication interaction between the two ends.
- Heartbeat requests are sent at regular intervals, but the receiving end may not receive heartbeat requests at regular intervals due to network transmission factors.
- the network may temporarily A little jitter or blockage may be regarded as a timeout and the communication connection will be disconnected, and there is no opportunity for the network to return to normal, which leads to poor resistance to network fluctuations in the heartbeat detection, and even misjudgment of timeout and false closing of the connection. question.
- This application provides a solution for adaptively adjusting the timeout time at both ends based on the network state, which can enhance the adaptability of heartbeat detection to weak networks, network jitter and network congestion, and the timeout time at both ends will not increase unlimitedly.
- the time limit can decide to time out and disconnect the communication connection.
- a heartbeat timeout detection method performed on the client side includes the following program steps:
- Step S101 after the client and the server are successfully connected in communication, the client sends the heartbeat request message Reqi corresponding to the ith detection period to the server, and records the sending time T ASi of the heartbeat request message Reqi .
- Req i is used to represent the heartbeat request message corresponding to the i-th detection period
- Res i is used to represent the response information fed back after the server responds to Req i , that is, Req i and Res i belong to the same
- T ASi represents the sending time of the heartbeat request message Req i
- T ARi represents the receiving time of the response information Res i .
- the client 10 can periodically and at equal intervals send the heartbeat request message Req i to the server, and every time the heartbeat request message Reqi is sent, the sending time T ASi needs to be recorded, and the sending time T ASi is one of the parameters for the subsequent calculation of the first timeout period on the client side.
- i is used to identify the order of heartbeat request/heartbeat detection/heartbeat cycle, that is, every time a heartbeat detection cycle is reached, the value of i is incremented by 1, until the current jth communication connection (marked as Link j ) is disconnected, and the The value of i is cleared to zero.
- Link j+1 the value of i is counted again.
- Step S102 whether the client receives the response information Res i fed back by the server within the first timeout period currently recorded by the local end.
- step S102 if the judgment result is yes, it is determined that the heartbeat detection has not timed out, and then step S103 is executed; if the judgment result is no, that is, it is recognized that the heartbeat has timed out, and step S104 is executed.
- the response information Res i may be a heartbeat reply message fed back by the server after responding to the heartbeat request message Reqi i , or may be business data or information sent by the server to the client, that is, the specific content of the response information Res i Or the form is not limited, as long as the client receives any form of data/information/message fed back by the peer server within the first timeout period, the client can determine that the server is "alive", and the communication connection can be maintained.
- Step S103 recording the receiving time T ARi of the response information Res i , and updating the first timeout time according to the sending time T ASi and the receiving time T ARi .
- Step S104 disconnect the communication connection with the server.
- the heartbeat round-trip time RTT Ai will be updated as the value of i increases.
- the heartbeat round-trip time RTT Ai is the time interval between the request and response of the i-th heartbeat on the client side, which can be used to evaluate the network status.
- the heartbeat round-trip time RTT The larger the Ai , the longer the communication time and the worse the network conditions; the smaller the heartbeat round-trip time RTT Ai , the faster the communication speed, the better and more stable the network, so the heartbeat round-trip time RTT Ai can be used as a matching and updating client The key parameter of the first timeout period on the terminal side.
- the first timeout time can be adaptively adjusted by comparing changes in the heartbeat round-trip time on the client side. For example, when the RTT Ai is greater than the RTT Ai-1 corresponding to the previous detection period, it indicates that the network is relatively weak. In order to improve the client’s ability to adapt to network fluctuations, the first timeout period can be relatively increased; if the RTT Ai is smaller than the previous detection period The periodic RTT Ai-1 indicates that the network is relatively strong, so the first timeout period can be relatively reduced; if the RTT Ai is equal to the RTT Ai-1 of the previous detection period, the current first timeout period can be kept unchanged.
- the client can also adaptively match the new first timeout time according to the following formula
- Timeout Ai min(max(a 1 *RTT Ai , b 1 *cycle), Timeout max1 )
- Timeout Ai is the first timeout time
- cycle is the preset sending interval of two adjacent heartbeat request messages (that is, the preset heartbeat cycle)
- a 1 is the first coefficient set by the client
- b 1 is the first coefficient set by the client
- Timeout max1 is the maximum timeout time preset by the client.
- the first timeout period Timeout Ai is not static, but can be adaptively adjusted according to the variable of the heartbeat round-trip time RTT Ai , so as to obtain a better first timeout period suitable for the current network.
- a 1 and b 1 are also adjustable coefficients, which can be set and adjusted according to actual application requirements.
- a 1 is 3 to 5
- b 1 is 2 to 5.
- Timeout max1 is the maximum timeout time preset on the client side, which is used to represent the extent to which the network can be allowed to deteriorate and must disconnect the communication connection, and is the upper limit of the first timeout time .
- Timeout max1 c 1 *cycle
- c 1 is the fifth coefficient set by the client
- the fifth coefficient is also an adjustable coefficient
- the value of Timeout max1 is set synchronously by adjusting the fifth coefficient c 1 .
- Timeout max1 may be set according to actual application requirements, which is not specifically limited in this embodiment of the present application, but Timeout max1 cannot be increased indefinitely, so as to avoid invalid communication caused by maintaining the communication connection even when the network is extremely weak.
- the client after the client sends the first heartbeat request message Req 1 to the server, it detects whether the response information Res 1 is received within the first threshold time Timeout def1 , and if so, obtains the heartbeat round-trip time RTT A1 , and according to the heartbeat
- the round-trip time RTT A1 updates the first timeout time, that is, the first timeout time can be changed from Timeout def1 to min(max(a 1 *RTT A1 , b 1 *cycle), Timeout max1 ), and so on, according to i
- the value is incremented to dynamically update the first timeout period, and it is detected whether a response feedback from the peer server is received within the first timeout period.
- the first timeout period can be relatively reduced; when the network is unstable, such as network jitter, congestion, weak network, etc., the first timeout period can be dynamically adjusted, and the first timeout period can be relatively increased, in order to receive
- the response information of the server provides a longer time limit, which makes it easier to retain the communication connection without disconnection due to the temporary fluctuation of the network. If the heartbeat timeout is still detected under the relatively large first timeout time, it means that the network is very poor. , you don't need to keep the communication connection and disconnect it directly.
- the client can adaptively and dynamically adjust the first timeout period of the local end according to the network status, so as to enhance the ability to adapt to the weak network, network jitter, and congestion.
- the self-adaptive adjustment manner of the first timeout period is not limited to that described in the embodiments of the present application.
- the client may try to reconnect.
- the client disconnects the jth communication connection Link j with the server, the i value recorded by the local end is cleared, and at the same time The client sends a reconnection request to the server, and the server establishes the j+1th communication connection Link j+1 with the client in response to the reconnection request.
- the server By clearing the i value to zero, it is convenient to re-count the i value in a new loop after reconnection.
- the communication connection Link j+1 is successfully established, a new cycle is entered, and the aforementioned heartbeat timeout detection mechanism on the client side can be implemented.
- the foregoing example shows that the client periodically sends the heartbeat request message Req i , and then receives the response information Res i fed back by the server. When the detection period of the next heartbeat is reached, the client sends the heartbeat request message Req i+1 again. In this way, the client can obtain the heartbeat round-trip time RTT Ai , and dynamically adjust and update the first timeout time dynamically.
- the server side can use its preset second threshold time Timeout def2 to perform heartbeat timeout detection, that is, adopt a conventional detection mode of fixed timeout time.
- the second threshold time Timeout def2 can be set according to actual application requirements, for example, the second threshold time Timeout def2 is 3*cycle. It should be noted that the second threshold time Timeout def2 and the second threshold time Timeout def2 may be the same or different.
- a heartbeat timeout detection method performed by the server side is provided, the method supports the server side to dynamically adjust and update the second timeout time dynamically, and the method includes the following program steps:
- Step S201 after the server and the client are successfully connected in communication, the server receives the heartbeat request message Reqi corresponding to the ith detection period sent by the client.
- Req i represents the heartbeat request message corresponding to the i-th detection period
- Res i represents the response information fed back after the server responds to Req i
- Sure i represents that the client receives Res
- the heartbeat confirmation message sent to the server after i that is, Req i , Res i and Sure i belong to the communication interaction message in the i-th detection period
- T BSi is used to indicate the sending time of the response information Res i
- T BRi is used to indicate The receiving time of the heartbeat confirmation message Sure i .
- Step S202 the server feeds back response information Res i to the client, and records the sending time T BSi of the response information Res i .
- Step S203 the server detects whether the heartbeat confirmation message Sure i sent by the client is received within the currently recorded second timeout period.
- step S203 If the detection result of step S203 is yes, it is determined that the heartbeat detection has not timed out, and then step S204 is performed; if the detection result is no, that is, it is recognized that the heartbeat is overtime, and step S205 is performed.
- Step S204 recording the receiving time T BRi of the heartbeat confirmation message Sure i , and updating the second timeout time according to the sending time T BSi and the receiving time T BRi .
- Step S205 disconnect the communication connection with the client.
- the heartbeat round-trip time RTT Bi will be updated as the value of i increases.
- the heartbeat round-trip time RTT Bi is the time interval between the response and confirmation of the i-th heartbeat on the server side, which can be used to evaluate the network status.
- the heartbeat round-trip time RTT Bi The larger the value, the longer the communication time and the poorer the network conditions; the smaller the heartbeat round-trip time RTT Bi , the faster the communication speed, the better and more stable the network, so the heartbeat round-trip time RTT Bi can be used as the matching and updating server side The key parameter of the second timeout period.
- the second timeout time can be adaptively adjusted by comparing changes in the heartbeat round-trip time on the server side. For example, when the RTT Bi is greater than the RTT Bi-1 corresponding to the previous detection period, it indicates that the network is relatively weak.
- the second timeout period can be relatively increased; if the RTT Bi is smaller than the previous detection period If the RTT Bi-1 is equal to the RTT Bi-1 of the previous detection period, it means that the network is relatively strengthened, and the second timeout period can be relatively reduced; if the RTT Bi is equal to the RTT Bi-1 of the previous detection period, the current second timeout period can be kept unchanged.
- the server may also adaptively match the new second timeout period according to the following formula:
- Timeout Bi min(max(a 2 *RTT Bi , b 2 *cycle), Timeout max2 )
- Timeout Bi is the second timeout time
- cycle is the preset sending interval of two adjacent heartbeat request messages
- a 2 is the third coefficient set by the server
- b 2 is the fourth coefficient set by the server
- Timeout max2 The maximum timeout time preset for the server.
- the second timeout time Timeout Bi is not static, but can be adaptively adjusted according to the variable of the heartbeat round-trip time RTT Bi , so as to obtain a better second timeout time suitable for the current network.
- a 2 and b 2 are also adjustable coefficients, which can be set and adjusted according to actual application requirements.
- a 2 is 3 to 5
- b 2 is 2 to 5.
- Timeout max2 is the maximum timeout time preset on the server side, which is used to represent the extent to which the network can be allowed to deteriorate and must be disconnected from the communication connection, and is the upper limit of the second timeout time.
- Timeout max2 c 2 *cycle
- c 2 is the sixth coefficient set on the server side
- the sixth coefficient is also an adjustable coefficient
- the value of Timeout max2 is set synchronously by adjusting the sixth coefficient c 2 .
- Timeout max2 may be set according to actual application requirements, which is not specifically limited in the embodiments of the present application, but Timeout max2 cannot be increased indefinitely, so as to avoid invalid communication caused by maintaining the communication connection even when the network is extremely weak. It should be noted that Timeout max1 and Timeout max2 may be the same or different.
- Timeout def2 when the server receives the first heartbeat request message Req 1 sent by the client, the server returns the response information Res 1 to the client, and is waiting to receive the heartbeat confirmation message Sure sent by the client
- the server side has not yet obtained the heartbeat round-trip time RTT B1 , that is, the server side has not updated the second timeout time, then the second timeout time defaults to the preset second threshold time (referred to as Timeout def2 ).
- Timeout def2 can be set according to the actual application, for example, Timeout def2 is 3*cycle.
- Time RTT B1 updates the second timeout time, that is, the second timeout time can be changed from Timeout def2 to min(max(a 2 *RTT B1 , b 2 *cycle), Timeout max2 ), and so on, according to the value of i
- the second timeout period is dynamically updated with the increment of , and it is detected whether the heartbeat request message, heartbeat confirmation message or data sent by the peer client is received within the second timeout period.
- the second timeout period can be relatively reduced; when the network is unstable, such as network jitter, congestion, weak network, etc., the second timeout period can be dynamically adjusted, and the second timeout period can be relatively increased, in order to receive Any form of information from the client provides a longer time limit, making it easier to retain the communication connection without disconnection due to temporary fluctuations in the network. If the heartbeat timeout is still detected under a relatively large second timeout If it is very poor, then there is no need to keep the communication connection, and the connection is directly disconnected.
- the server can adaptively and dynamically adjust the second timeout period of the local end according to the network status, so as to enhance the ability to adapt to the weak network, network jitter, and congestion.
- the second timeout period will not increase unlimitedly. It can decide to timeout and disconnect when it reaches the maximum level, which improves the adaptability and accuracy of heartbeat timeout detection, and the whole process will not affect the sending and receiving of data/messages/information.
- the self-adaptive adjustment manner of the second timeout period is not limited to that described in the embodiments of the present application.
- the client may try to reconnect.
- the server After disconnecting the communication connection Link j with the client, the server receives the reconnection request initiated by the client, and in response to the reconnection request, A communication connection Link j+1 with the client is established. After the communication connection Link j+1 is successfully established, a new cycle is entered, and the aforementioned server-side heartbeat timeout detection mechanism can be implemented.
- FIG. 4( a ) shows an example of communication between the client 10 and the server 20 under the mode of using a fixed timeout on both sides.
- the client 10 periodically sends the heartbeat request information Reqi to the server, After receiving the heartbeat request information Reqi , the server feeds back the response information Res i .
- the network state is good during the first two heartbeat detections.
- the client sends a heartbeat request message Req 3 at the T AS3 node, and the client side adopts the first threshold time Timeout def1 as the default timeout time , then taking the T AS3 node as the reference point, the timeout detection interval corresponding to the third heartbeat detection can be constructed as [T AS3 , T AS3 +Timeout def1 ] .
- the response information Res 3 is not received, so it is determined that the heartbeat has timed out, and the client will directly disconnect the current communication connection with the server. Therefore, in the mode of Figure 4(a), in the case of network jitter, blocking, etc., there is a delay and lag in sending and receiving information at both ends. Once the fixed default timeout time is exceeded, the connection is directly disconnected. Therefore, the heartbeat detection mechanism has poor resistance to network interference. Frequent disconnection and reconnection are prone to occur, which increases the load of the double-ended communication connection.
- FIG. 4( b ) shows an example of communication between the client 10 and the server 20 in the mode of dynamically adaptively adjusting the timeout period on both sides.
- FIG. 4 ( In b) only the cycles of the first two heartbeat detections are shown, and other subsequent heartbeat detections have been omitted.
- the network is good during the first heartbeat detection, and the network fluctuates from the second heartbeat detection, and the two-end sending and receiving messages begin to appear hysteresis.
- send the heartbeat request message Req 1 ⁇ receive the response information Res 1 (the node that updates the first timeout time for the first time) ⁇ send the heartbeat confirmation message Sure 1 ⁇ send the heartbeat request message Req 2 ⁇ Send the heartbeat request message Req 3 ⁇ receive the response message Res 2 ⁇ send the heartbeat confirmation message Sure 2 (the node that updates the first timeout time for the second time).
- the first timeout period currently recorded by the client is the initial default first threshold time Timeout def1 , which is the sending time of the heartbeat request message Req 1 .
- T AS1 is the reference point, and the timeout detection interval A1 corresponding to the first heartbeat detection on the client side is constructed as [T AS1 , T AS1 +Timeout def1 ] .
- the client adaptively updates the first timeout time as the value of i increases, and locates and constructs the timeout detection interval A i according to the updated first timeout time and key nodes. Perform heartbeat timeout detection; if the heartbeat timeout is diagnosed during the period, the current communication connection will be disconnected.
- Fig. 4(b) for the server side: receive the heartbeat request message Req 1 ⁇ feedback the response information Res 1 ⁇ receive the heartbeat confirmation message Sure 1 again (the node that updates the second timeout time for the first time) ⁇ receive the heartbeat request message Req 2 ⁇ feedback Response information Res 2 ⁇ Receive heartbeat request message Req 3 ⁇ Feedback response information Res 3 ⁇ Receive heartbeat confirmation message Sure 2 (the node that updates the second timeout time for the second time).
- the second timeout period currently recorded by the server is the initial default second threshold time Timeout def2 , in response to the sending time T BS1 of the information Res 1
- the server uses the reception time T BR1 of the heartbeat confirmation message Sure 1 as the reference point, and constructs the timeout detection interval B2 corresponding to the second heartbeat detection on the server side as [T BR1 , T BR1 +Timeout B1 ] , and detect whether the heartbeat request message Req 2 sent by the client is received within the interval [T BR1 , T BR1 +Timeout B1 ]. Since the heartbeat request message Req 2 is received within [T BR1 , T BR1 +Timeout B1 ], it is determined that the heartbeat detection has not timed out, the communication connection is maintained, and the server feeds back the response information Res 2 to the client.
- the second timeout period is adaptively updated as the value of i increases, and the timeout detection interval B i is located and constructed according to the updated second timeout period and key nodes for heartbeating. Timeout detection; if the heartbeat timeout is diagnosed during the period, the current communication connection will be disconnected.
- Fig. 4(a) and Fig. 4(b) From the comparison of Fig. 4(a) and Fig. 4(b), it can be seen that the present application can perform adaptive matching and update of the timeout time on the client side and the server side respectively based on the network status and fluctuation in different time periods.
- the round-trip time of the heartbeats on both sides increases, thereby adaptively increasing the timeout time on both sides relatively, avoiding the direct judgment of timeout and disconnection in the example of Figure 4(a), and improving the performance of the heartbeat detection mechanism in the communication connection.
- FIG. 4(a) and FIG. 4(b) are only exemplary and do not limit the actual communication and network status.
- An embodiment of the present application further provides an electronic device, where a client is installed in the electronic device, and the client is configured to execute the above-mentioned method for detecting heartbeat timeout on the client side.
- An embodiment of the present application further provides a server, where the server is configured to execute the above-mentioned server-side heartbeat timeout detection method.
- An embodiment of the present application further provides an electronic device, including: a memory and at least one processor; the memory is used to store instructions; the at least one processor is used to execute the instructions in the memory, and execute the above Heartbeat timeout detection method configured on the client side.
- An embodiment of the present application further provides a server, including: a memory and at least one processor; the memory is used to store instructions; the at least one processor is used to execute the instructions in the memory to execute the above server Heartbeat timeout detection method configured on the side.
- Embodiments of the present application further provide a computer-readable storage medium, including instructions, which, when running on a computer, cause the computer to execute the heartbeat timeout detection method configured on the client side above.
- Embodiments of the present application further provide a computer-readable storage medium, including instructions, which, when running on a computer, cause the computer to execute the heartbeat timeout detection method configured on the server side above.
Landscapes
- Health & Medical Sciences (AREA)
- Cardiology (AREA)
- General Health & Medical Sciences (AREA)
- Engineering & Computer Science (AREA)
- Computer Networks & Wireless Communication (AREA)
- Signal Processing (AREA)
- Computer And Data Communications (AREA)
Abstract
本发明公开一种心跳超时检测方法、服务器及电子设备,在客户端与服务器通信连接成功之后,客户端向服务器发送第i个检测周期对应的心跳请求消息,并记录心跳请求消息的发送时间;客户端若在本端当前记录的第一超时时间内,接收到服务器反馈的同属第i个检测周期内的应答信息,记录所述应答信息的接收时间,并根据所述心跳请求消息的发送时间和所述应答信息的接收时间,更新所述第一超时时间;客户端若在所述第一超时时间内未接收到服务器反馈的同属第i个检测周期内的应答信息,则断开与服务器的通信连接。客户端能够根据网络状态自适应调整本端的第一超时时间,增强适应弱网、网络抖动和阻塞等状态的能力,提高心跳超时检测的自适应性和准确性。
Description
本申请要求在2021年06月29日提交中国专利局、申请号为202110725277.0、发明名称为“一种心跳超时检测方法、服务器及电子设备”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。
本发明涉及通信技术领域,尤其涉及一种心跳超时检测方法、服务器及电子设备。
在某些通信场景中,可基于通信协议,建立客户端与服务器的通信连接。以实现数据传输和信息交互,所述通信协议例如是TCP(Transmission Control Protocol,传输控制协议)、WebSocket协议等。
在通信连接成功后,可能会出现网络异常,例如网络抖动、阻塞和断网等,而客户端和服务器无法获知当前存在网络异常,这就导致两端虽然仍保持连接状态,但实际上已无法进行有效通信,不仅影响数据传输,还占用通信资源。对此,在现有通信中引入心跳机制,客户端与服务器之间周期性地互发心跳消息,例如客户端向服务器端发送心跳消息1,若网络良好,则服务器会向客户端快速回传心跳消息2;若客户端在阈值时间内始终未接收到服务器的心跳反馈,即心跳检测超时,则认为通信连接不可用,自动断开连接。但这种通过限定的阈值时间来检测心跳超时的方式,容易出现超时误判、通信连接误关闭、抗网络抖动/阻塞能力弱等问题。
发明内容
本发明提供一种心跳超时检测方法、服务器及电子设备,可以提高心跳超时检测的准确性,增强通信连接对网络不稳定性的适应能力。
第一方面实施例提供的心跳超时检测方法,应用于客户端,包括:
在客户端与服务器通信连接成功之后,客户端向服务器发送第i个检测周期对应的心跳请求消息,并记录所述心跳请求消息的发送时间;
客户端若在本端当前记录的第一超时时间内,接收到服务器反馈的同属第i个检测周期内的应答信息,记录所述应答信息的接收时间,并根据所述心跳请求消息的发送时间和所述应答信息的接收时间,更新所述第一超时时间;
客户端若在所述第一超时时间内未接收到服务器反馈的同属第i个检测周期内的应答信息,则断开与服务器的通信连接。
在第一方面第一种示例性的实现方式中,客户端按照如下方式更新所述第一超时时间:计算所述心跳请求消息的发送时间与所述应答信息的接收时间的时间差,得到客户端第i个检测周期对应的心跳往返时间;根据客户端第i个检测周期对应的心跳往返时间,自适应匹配新的第一超时时间。
在第一方面第二种示例性的实现方式中,客户端按照如下公式自适应匹配新的第一超时时间;
Timeout
Ai=min(max(a
1*RTT
Ai,b
1*cycle),Timeout
max1)
式中,Timeout
Ai为所述第一超时时间,cycle为相邻两次心跳请求消息的预设发送间隔,a
1为客户端设置的第一系数,b
1为客户端设置的第二系数,Timeout
max1为客户 端预设的最大超时时间,RTT
Ai为客户端第i个检测周期对应的心跳往返时间。
在第一方面第三种示例性的实现方式中,所述方法还包括:客户端在所述第一超时时间内接收到服务器反馈的同属第i个检测周期内的应答信息时,向服务器发送同属第i个检测周期内的心跳确认消息,所述心跳确认消息用于指示服务器记录所述心跳确认消息的接收时间,所述心跳确认消息的接收时间用于计算服务器端第i个检测周期对应的心跳往返时间。
在第一方面第四种示例性的实现方式中,所述方法还包括:
客户端在断开与服务器的第j次通信连接之后,将本端记录的i值清零,并向服务器发送重连请求,所述重连请求用于向服务器请求建立第j+1次通信连接;
在第j+1次通信连接建立成功后,客户端在接收到服务器首次反馈的应答信息之前,将预设的第一阈值时间作为所述第一超时时间。
第一方面主要针对通信连接中的客户端侧,在客户端与服务器连接成功之后,通过心跳机制检测连接的有效性,客户端周期性地向服务器发送心跳请求消息,若能在本侧记录的第一超时时间内,接收到服务器在响应心跳请求消息后反馈的同属一个检测周期内的应答信息,则客户端认为连接有效,等待下一周期的心跳检测,反之则存在心跳超时问题,客户端即可主动断开与服务器的通信连接。其中本申请的关键在于,不是采用固定的超时时间,第一超时时间可以根据实际网络状态进行自适应调整,为便于描述,示例性地,用Req
i表示第i个检测周期对应的心跳请求消息,用Res
i表示服务器响应Req
i后反馈的应答信息,即Req
i和Res
i为同属第i个检测周期内的交互消息。
客户端通过心跳请求消息Req
i的发送时间(记为T
Asi)和应答信息Res
i的接收时间(记为T
ARi),即可确定客户端第i次心跳检测中消息往返所消耗的时间,即客户端第i个检测周期对应的心跳往返时间(记为RTT
Ai),心跳往返时间RTT
Ai的时间长短可用于评估当前通信网络的状态,然后自适应匹配第一超时时间,在网络良好时,可以相对减小第一超时时间,比如采用预设的第一阈值时间(客户端侧默认的心跳超时时间)作为第一超时时间;在网络不稳定时,比如网络出现抖动、阻塞、弱网等情况,可以动态调整第一超时时间,相对增大第一超时时间,为接收服务器的应答信息提供更长时限,使通信连接更易于保留,而不会因网络的暂时波动而断连,若在相对较大的第一超时时间下仍旧检测到心跳超时,则说明网络很差,则无需保留通信连接,直接断开连接。第一方面中客户端能够根据网络状态自适应动态调整本端的第一超时时间,增强适应弱网、网络抖动和阻塞等状态的能力,第一超时时间不会无限制的增加,在网络恶化的一定程度时可以决策超时并断连,提高心跳超时检测的自适应性和准确性。
第二方面实施例提供的心跳超时检测方法,应用于服务器端,包括:
在服务器与客户端通信连接成功之后,服务器接收客户端发送的第i个检测周期对应的心跳请求消息;
服务器向客户端反馈同属第i个检测周期内的应答信息,并记录所述应答信息的发送时间;
服务器若在本端当前记录的第二超时时间内,接收到客户端发送的同属第i个检测周期内的心跳确认消息,记录所述心跳确认消息的接收时间,并根据所述应答信息的发送时间和所述心跳确认消息的接收时间,更新所述第二超时时间;
服务器若在所述第二超时时间内未接收到客户端发送的同属第i个检测周期内的心跳确认消息,则断开与客户端的通信连接。
在第二方面第一种示例性的实现方式中,服务器按照如下方式确定更新所述第二超时时间:计算所述应答信息的发送时间与所述心跳确认消息的接收时间的时间差,得到服务器端第i个检测周期对应的心跳往返时间;根据服务器端第i个检测周期对应的心跳往返时间,自适应匹配新的第二超时时间。
在第二方面第二种示例性的实现方式中,服务器按照如下公式自适应匹配新的第二超时时间;
Timeout
Bi=min(max(a
2*RTT
Bi,b
2*cycle),Timeout
max2)
式中,Timeout
Bi为所述第二超时时间,cycle为相邻两次心跳请求消息的预设发送间隔,a
2为服务器设置的第三系数,b
2为服务器设置的第四系数,Timeout
max2为服务器预设的最大超时时间,RTT
Bi为服务器端第i个检测周期对应的心跳往返时间。
在第二方面第三种示例性的实现方式中,所述方法还包括:
服务器在更新所述第二超时时间之后,以第i个检测周期内心跳确认消息的接收时间为基准点,检测在更新后的第二超时时间内是否接收到客户端发送的第i+1个检测周期对应的心跳请求消息;
服务器若在更新后的第二超时时间内接收到客户端发送的第i+1个检测周期对应的心跳请求消息,则向客户端反馈同属第i+1个检测周期内的应答信息;
服务器若在更新后的第二超时时间内未接收到客户端发送的第i+1个检测周期对应的心跳请求消息,则断开与客户端的通信连接。
在第二方面第四种示例性的实现方式中,所述方法还包括:
服务器在断开与客户端的第j次通信连接之后,接收客户端发起的重连请求;
服务器响应于所述重连请求,建立与客户端的第j+1次通信连接;
在第j+1次通信连接建立成功后,服务器在接收到客户端首次发送的心跳确认消息之前,将预设的第二阈值时间作为所述第二超时时间。
第二方面主要针对通信连接中的服务器侧,在服务器与客户端连接成功之后,通过心跳机制检测连接的有效性,客户端周期性地向服务器发送心跳请求消息,服务器每接收到一个心跳请求消息后立即反馈应答信息,客户端每接收到一个应答信息后再向服务器发送心跳确认消息,为便于描述,示例性地,用Req
i表示第i个检测周期对应的心跳请求消息,用Res
i表示服务器响应Req
i后反馈的应答信息,用Sure
i表示客户端接收到Res
i后向服务器发送的心跳确认消息,即Req
i、Res
i和Sure
i为同属第i个检测周期内的交互消息。
服务器通过应答信息Res
i的发送时间(记为T
BSi)和心跳确认消息Sure
i的接收时间(记为T
BRi),即可确定服务器侧第i次心跳检测中消息往返所消耗的时间,即服务器侧第i个检测周期对应的心跳往返时间(记为RTT
Bi),心跳往返时间RTT
Bi的时间长短可用于评估当前通信网络的状态,然后服务器即可自适应匹配第二超时时间,在网络良好时,可以相对减小第二超时时间,比如采用预设的第二阈值时间(服务器侧默认的心跳超时时间)作为第二超时时间;在网络不稳定时,比如网络出现抖动、阻塞、弱网等情况,可以动态调整第二超时时间,相对增大第二超时时间,为接收对端发送的消息提供更长时限,使通信连接更易于保留,而不会因网络的暂时波动而断连,若在相对较大的第二超时时间下仍旧检测到心跳超时,则说明网络很差,则无需保留通信连接,直接断开连接。第二方面中服务器能够根据网络状态自适应动态调整本端的第二超时时间,增强适应弱网、网络抖动和阻塞等状态的能力,第二超时时间不会无限制的增加,在网络恶化的一定程度时可以决策超时并断连,提高心跳超时检测的自 适应性和准确性。
第三方面实施例提供的一种电子设备,所述电子设备安装有客户端,所述客户端被配置为:
在客户端与服务器通信连接成功之后,向服务器发送第i个检测周期对应的心跳请求消息,并记录所述心跳请求消息的发送时间;
若在本端当前记录的第一超时时间内,接收到服务器反馈的同属第i个检测周期内的应答信息,记录所述应答信息的接收时间,并根据所述心跳请求消息的发送时间和所述应答信息的接收时间,更新所述第一超时时间;
若在所述第一超时时间内未接收到服务器反馈的同属第i个检测周期内的应答信息,则断开与服务器的通信连接。
第四方面实施例提供的一种服务器,所述服务器被配置为:
在服务器与客户端通信连接成功之后,接收客户端发送的第i个检测周期对应的心跳请求消息;
向客户端反馈同属第i个检测周期内的应答信息,并记录所述应答信息的发送时间;
若在本端当前记录的第二超时时间内,接收到客户端发送的同属第i个检测周期内的心跳确认消息,记录所述心跳确认消息的接收时间,并根据所述应答信息的发送时间和所述心跳确认消息的接收时间,更新所述第二超时时间;
若在所述第二超时时间内未接收到客户端发送的同属第i个检测周期内的心跳确认消息,则断开与客户端的通信连接。
第五方面实施例提供的电子设备,包括:存储器和至少一个处理器;所述存储器,用于存储指令;所述至少一个处理器,用于执行所述存储器中的所述指令,执行以上第一方面任意一项所述的心跳超时检测方法。
第六方面实施例提供的服务器,包括:存储器和至少一个处理器;所述存储器,用于存储指令;所述至少一个处理器,用于执行所述存储器中的所述指令,执行以上第二方面任意一项所述的心跳超时检测方法。
第七方面实施例提供一种计算机可读存储介质,包括指令,当其在计算机上运行时,使得计算机执行以上第一方面任意一项所述的心跳超时检测方法。
第八方面实施例提供一种计算机可读存储介质,包括指令,当其在计算机上运行时,使得计算机执行以上第二方面任意一项所述的心跳超时检测方法。
其他方面的实施例的方案以及具备的有益效果可适应性参照第一方面和第二方面,具体不再赘述。
为了更清楚地说明本发明实施例或现有技术中的技术方案,下面将对实施例中所需要访问的附图作简单地介绍,显而易见地,下面描述中的附图仅仅是本发明的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动的前提下,还可以根据这些附图获得其他的附图。
图1中示例性示出了一种通信场景的示意图;
图2中示例性示出了一种客户端侧执行的心跳超时检测方法;
图3中示例性示出了一种服务器侧执行的心跳超时检测方法;
图4(a)中示例性示出了在双侧采用固定超时时间的模式下,客户端与服务器的通信交互示意图;
图4(b)中示例性示出了在双侧采用动态自适应调整超时时间的模式下,客户端与服务器的通信交互示意图。
为使本申请的目的和实施方式更加清楚,下面将结合本申请示例性实施例中的附图,对本申请示例性实施方式进行清楚、完整地描述,显然,描述的示例性实施例仅是本申请一部分实施例,而不是全部的实施例。
需要说明的是,本申请中对于术语的简要说明,仅是为了方便理解接下来描述的实施方式,而不是意图限定本申请的实施方式。除非另有说明,这些术语应当按照其普通和通常的含义理解。
本申请中说明书和权利要求书及上述附图中的术语“第一”、“第二”、“第三”等是用于区别类似或同类的对象或实体,而不必然意味着限定特定的顺序或先后次序,除非另外注明。应该理解这样使用的用语在适当情况下可以互换。
术语“包括”和“具有”以及他们的任何变形,意图在于覆盖但不排他的包含,例如,包含了一系列组件的产品或设备不必限于清楚地列出的所有组件,而是可包括没有清楚地列出的或对于这些产品或设备固有的其它组件。
术语“模块”是指任何已知或后来开发的硬件、软件、固件、人工智能、模糊逻辑或硬件或/和软件代码的组合,能够执行与该元件相关的功能。
在一种示例性的通信场景中,参照图1,客户端10和服务器20为通信交互的双端,客户端10可以安装在实际电子设备中,电子设备包括但不限于显示设备、智能手机、计算机等,客户端10是与服务器相对应的为客户提供相应服务的程序,客户端10的运行形式包括但不限于网页浏览器、电子邮件客户端、即时通讯的客户端软件等。客户端10与服务器20可以基于某些通信协议建立通信连接,比如TCP连接、WebSocket连接等,客户端10与服务器20通信连接成功之后,即可实现数据传输和信息交流等所需的通信功能。客户端10与服务器20之间可采用全双工通信,即允许双端之间可以同时进行双向数据/信息/消息的传输。
在客户端10与服务器20一般是基于网络通信,而网络状态存在不确定性和不可预测性,网络良好时两端通信稳定、数据收发速度较快;在网络出现抖动、阻塞、弱网甚至断网等异常状态时,客户端10与服务器20之间的通信会受到干扰,比如数据收发呈现一定程度的滞后性,甚至两端虽保持连接,但始终无法接收到对端发送的数据,呈现出“虚假连接”的现象。由于客户端10和服务器20无法感知网络状态,即便在网络恶化的情况下已无法实现有效通信,但双端若仍旧保持彼此的通信连接,不仅会导致数据传输出现延迟甚至中断,影响客户端的任务执行,还会占用通信资源。对此,现有通信中一般引入心跳检测机制,客户端10与服务器20之间可周期性地互发心跳消息,以询问对端是否“活着”,从而决定是否断开与对端的通信连接,例如客户端10向服务器20周期性地发送心跳请求,若客户端10在超时时间内接收到服务器的应答反馈,则反映了当前网络良好,客户端10保持与服务器20的通信连接;反之,若客户端10未在超时时间接收到服务器的应答反馈,则说明当前网络异常,则客户端10可断开与服务器之间无效的通信连接。
对于现有的心跳检测机制,一方面客户端10和服务器端20两端设置固定的超时时间,该超时时间不会随网络、双端间的通信交互等因素而变化,另一方面发送端周期性等间隔地发送心跳请求,但接收端受网络传输因素的影响可能不会周期性等间隔 地接收心跳请求,如果采用限定的超时时间进行心跳检测,在网络条件非恶劣的环境下,网络暂时呈现些许抖动或阻塞即可能会被认定为超时并断开通信连接,没有为网络恢复正常提供机会,这就导致了心跳检测时普遍存在抗网络波动能力差,甚至误判超时和误关闭连接的问题。
本申请提供基于网络状态自适应调整两端超时时间的方案,可以增强心跳检测对弱网、网络抖动和网络阻塞的适应能力,并且两端的超时时间不会无限制的增加,当网络恶化到上限时可以决策超时,并断开通信连接。
如图2所示,在一些实施例中,提供一种客户端侧执行的心跳超时检测方法,所述方法包括如下程序步骤:
步骤S101,在客户端与服务器通信连接成功之后,客户端向服务器发送第i个检测周期对应的心跳请求消息Req
i,并记录所述心跳请求消息Req
i的发送时间T
ASi。
对于客户端侧,为便于描述,示例性地,用Req
i表示第i个检测周期对应的心跳请求消息,用Res
i表示服务器响应Req
i后反馈的应答信息,即Req
i和Res
i为同属第i个检测周期内的交互消息,以及,用T
ASi表示心跳请求消息Req
i的发送时间,用T
ARi表示应答信息Res
i的接收时间。
在客户端10与服务器20连接成功之后,客户端10可以周期性、等间隔地向服务器发送心跳请求消息Req
i,每次发送心跳请求消息Req
i时都需要记录其发送时间T
ASi,发送时间T
ASi是后续计算客户端侧的第一超时时间的参数之一。其中,i用于标识心跳请求/心跳检测/心跳周期的次序,即每达到一个心跳的检测周期,将i值递增1,直至当前第j次的通信连接(记为Link
j)断开,将i值清零,在重新连接后,即建立第j+1次通信连接(记为Link
j+1)成功后,i值重新计数。
步骤S102,客户端是否在本端当前记录的第一超时时间内接收到服务器反馈的应答信息Res
i。
其中,心跳请求消息Req
i和应答信息Res
i需要保持i值一致,通过i值的标识来识别心跳检测的周期次序,保证能准确识别每次心跳的请求及应答,从而根据每次心跳往返评估网络状态,使自适应调整的超时时间更精准。步骤S102中若判断结果为是,则确定心跳检测未超时,则执行步骤S103;若判断结果为否,即识别心跳超时,执行步骤S104。
在一些实施例中,所述应答信息Res
i可以是服务器响应心跳请求消息Req
i后反馈的心跳回复消息,还可以是服务器向客户端发送的业务数据或信息,即应答信息Res
i的具体内容或形式不限定,只要客户端在第一超时时间内接收到对端服务器反馈的任何形式的数据/信息/消息,客户端都能确定服务器是“活着”的,即可保持通信连接。
步骤S103,记录所述应答信息Res
i的接收时间T
ARi,并根据所述发送时间T
ASi和所述接收时间T
ARi,更新所述第一超时时间。
步骤S104,断开与服务器的通信连接。
在步骤S103一种示例性的实现方式中,计算发送时间T
ASi与接收时间T
ARi的时间差,得到客户端第i个检测周期对应的心跳往返时间(记为RTT
Ai),即RTT
Ai=T
ARi-T
ASi,然后即可根据心跳往返时间RTT
Ai,自适应匹配新的第一超时时间。心跳往返时间RTT
Ai会随着i值的递增而更新,心跳往返时间RTT
Ai为客户端侧第i次心跳的请求与应答的时间间隔,可用于评估网络状态,一般来说,心跳往返时间RTT
Ai越大,说明通信耗时越长,网络条件越差;心跳往返时间RTT
Ai越小,说明通信速度越快,网络越好越、越稳定,因此心跳往返时间RTT
Ai可作为匹配和更新客户端侧第一超时时间的关键参数。
在一种示例性的实现方式中,由于心跳往返时间RTT
Ai可用于评估通信网络的状态,因此可通过比较客户端侧的心跳往返时间的变化,来适应性调整第一超时时间。例如,RTT
Ai大于前一次检测周期对应的RTT
Ai-1时,说明网络相对变弱,为提升客户端对网络波动的适应能力,可相对增大第一超时时间;若RTT
Ai小于前一次检测周期的RTT
Ai-1,说明网络相对增强,则可相对减小第一超时时间;若RTT
Ai等于前一次检测周期的RTT
Ai-1,则可保持当前的第一超时时间不变。
在另一种示例性的实现方式中,客户端还可按照如下公式自适应匹配新的第一超时时间;
Timeout
Ai=min(max(a
1*RTT
Ai,b
1*cycle),Timeout
max1)
式中,Timeout
Ai为所述第一超时时间,cycle为相邻两次心跳请求消息的预设发送间隔(即预设的心跳周期),a
1为客户端设置的第一系数,b
1为客户端设置的第二系数,Timeout
max1为客户端预设的最大超时时间。
第一超时时间Timeout
Ai不是静态不变的,而是可根据心跳往返时间RTT
Ai这一变量进行自适应调整,从而得到适应于当前网络的较佳的第一超时时间。此外,a
1和b
1也是可调系数,可以根据实际应用要求进行设置和调节,可选地,a
1取值为3~5,b
1取值为2~5。
在一种示例性的实现方式中,Timeout
max1为客户端侧预设的最大超时时间,用于表征能够允许网络恶化到何种程度时必须断开通信连接,是第一超时时间的上限值。可选地,Timeout
max1=c
1*cycle,c
1为客户端设置的第五系数,第五系数也为可调系数,通过调节第五系数c
1同步设置Timeout
max1的取值。Timeout
max1可以根据实际应用要求进行设置,本申请实施例不作具体限定,但是Timeout
max1不可无限制的增大,以避免网络极弱时仍维系通信连接导致的无效通信。
在一些实施例中,当i=1时,即客户端向服务器发送第一次心跳请求消息Req
1之后,等待服务器端的应答反馈,即等待接收服务器首次反馈的应答信息Res
1,这时客户端尚未获取到心跳往返时间RTT
A1,即客户端侧尚未更新过第一超时时间,则此时第一超时时间默认为预设的第一阈值时间(记为Timeout
def1)。Timeout
def1可以根据实际应用进行设置,例如Timeout
def1为3*cycle。
也就是说,客户端向服务器发送第一次心跳请求消息Req
1之后,检测是否在第一阈值时间Timeout
def1内接收到应答信息Res
1,若是,则获取到心跳往返时间RTT
A1,并根据心跳往返时间RTT
A1更新第一超时时间,即可将第一超时时间由Timeout
def1变更为min(max(a
1*RTT
A1,b
1*cycle),Timeout
max1),以此类推,即可根据i值的递增而动态更新第一超时时间,并检测是否在第一超时时间内接收到对端服务器的应答反馈。
在网络良好时,可以相对减小第一超时时间;在网络不稳定时,比如网络出现抖动、阻塞、弱网等情况,可以动态调整第一超时时间,相对增大第一超时时间,为接收服务器的应答信息提供更长时限,使通信连接更易于保留,而不会因网络的暂时波动而断连,若在相对较大的第一超时时间下仍旧检测到心跳超时,则说明网络很差,则无需保留通信连接,直接断开连接。本申请中客户端能够根据网络状态自适应动态调整本端的第一超时时间,增强适应弱网、网络抖动和阻塞等状态的能力,第一超时时间不会无限制的增加,在网络恶化的一定程度时可以决策超时并断连,提高心跳超时检测的自适应性和准确性,并且整个过程不会影响数据/消息/信息的收发。第一超时时间的自适应调整方式不限于本申请实施例所述。
在一些实施例中,步骤S104客户端与服务器断开通信连接后,客户端可以尝试重新连接。在实际应用场景中,客户端与服务器之间可能存在多次断连和重连,客户端 在断开与服务器的第j次通信连接Link
j之后,将本端记录的i值清零,同时客户端向服务器发送重连请求,服务器响应于重连请求,建立与客户端的第j+1次通信连接Link
j+1。通过将i值清零,便于重连后在新的循环中重新对i值进行计数。在通信连接Link
j+1建立成功后,即进入了一个新的循环中,即可执行前述客户端侧的心跳超时检测机制。
前述示例示出的是客户端周期性发送心跳请求消息Req
i,然后接收服务器反馈的应答信息Res
i,待达到下一心跳的检测周期时,客户端再发送心跳请求消息Req
i+1,这种方式客户端可以获取心跳往返时间RTT
Ai,并对第一超时时间进行动态自适应调整和更新。但对于服务器侧来说,其在被动接收心跳请求消息Req
i后会立即反馈应答信息Res
i,但客户端接收到应答信息Res
i后,不会再针对应答信息Res
i作出响应,而是直接等待通过发送第i+1个检测周期对应的心跳请求消息Req
i+1来进入下一次序的心跳检测,因此服务器侧无法获取到心跳往返时间RTT
Bi,也就无法进行第二超时时间的自适应调整和更新,对于这种情况,服务器侧可以利用其预设的第二阈值时间Timeout
def2进行心跳超时检测,即采用常规的固定超时时间的检测模式。第二阈值时间Timeout
def2可以根据实际应用要求进行设置,例如第二阈值时间Timeout
def2为3*cycle。需要说明的是,第二阈值时间Timeout
def2和第二阈值时间Timeout
def2可相同或不同。
如图3所示,在一些实施例中,提供一种服务器侧执行的心跳超时检测方法,所述方法支持服务器侧对第二超时时间进行动态自适应调整和更新,方法包括如下程序步骤:
步骤S201,在服务器与客户端通信连接成功之后,服务器接收客户端发送的第i个检测周期对应的心跳请求消息Req
i。
对于服务器侧,为便于描述,示例性地,用Req
i表示第i个检测周期对应的心跳请求消息,用Res
i表示服务器响应Req
i后反馈的应答信息,用Sure
i表示客户端接收到Res
i后向服务器发送的心跳确认消息,即Req
i、Res
i和Sure
i为同属第i个检测周期内的通信交互消息,以及,用T
BSi表示应答信息Res
i的发送时间,用T
BRi表示心跳确认消息Sure
i的接收时间。
步骤S202,服务器向客户端反馈应答信息Res
i,并记录所述应答信息Res
i的发送时间T
BSi。
步骤S203,服务器检测在当前记录的第二超时时间内是否接收到客户端发送的心跳确认消息Sure
i。
与图2中示例的区别在于,客户端在第一超时时间内接收到服务器反馈的应答信息Res
i时,不再处于等待发送心跳请求消息Req
i+1的状态,而是会响应应答信息Res
i,向服务器发送心跳确认消息Sure
i。若步骤S203的检测结果为是,则确定心跳检测未超时,则执行步骤S204;若检测结果为否,即识别心跳超时,执行步骤S205。
步骤S204,记录所述心跳确认消息Sure
i的接收时间T
BRi,并根据所述发送时间T
BSi和所述接收时间T
BRi,更新所述第二超时时间。
步骤S205,断开与客户端的通信连接。
对于服务器侧,接收心跳请求消息Req
i→反馈应答信息Res
i→再接收心跳确认消息Sure
i,构成服务器侧的第i次心跳检测,其中应答信息Res
i和心跳确认消息Sure
i构成一次心跳往返,在步骤S204一种示例性的实现方式中,计算心跳确认消息Sure
i的接收时间T
BRi与应答信息Res
i的发送时间T
BSi的时间差,得到服务器侧第i个检测周期对应的心跳往返时间(记为RTT
Bi),即RTT
Bi=T
BRi-T
BSi,然后即可根据心跳往返时间RTT
Bi,自适应匹配新的第二超时时间。心跳往返时间RTT
Bi会随着i值的递增而 更新,心跳往返时间RTT
Bi为服务器侧第i次心跳的应答与确认的时间间隔,可用于评估网络状态,一般来说,心跳往返时间RTT
Bi越大,说明通信耗时越长,网络条件越差;心跳往返时间RTT
Bi越小,说明通信速度越快,网络越好越、越稳定,因此心跳往返时间RTT
Bi可作为匹配和更新服务器侧第二超时时间的关键参数。
在一种示例性的实现方式中,由于心跳往返时间RTT
Bi也可用于评估通信网络的状态,因此可通过比较服务器侧的心跳往返时间的变化,来适应性调整第二超时时间。例如,RTT
Bi大于前一次检测周期对应的RTT
Bi-1时,说明网络相对变弱,为提升服务器对网络波动的适应能力,可相对增大第二超时时间;若RTT
Bi小于前一次检测周期的RTT
Bi-1,说明网络相对增强,则可相对减小第二超时时间;若RTT
Bi等于前一次检测周期的RTT
Bi-1,则可保持当前的第二超时时间不变。
在另一种示例性的实现方式中,服务器还可按照如下公式自适应匹配新的第二超时时间;
Timeout
Bi=min(max(a
2*RTT
Bi,b
2*cycle),Timeout
max2)
式中,Timeout
Bi为所述第二超时时间,cycle为相邻两次心跳请求消息的预设发送间隔,a
2为服务器设置的第三系数,b
2为服务器设置的第四系数,Timeout
max2为服务器预设的最大超时时间。
第二超时时间Timeout
Bi不是静态不变的,而是可根据心跳往返时间RTT
Bi这一变量进行自适应调整,从而得到适应于当前网络的较佳的第二超时时间。此外,a
2和b
2也是可调系数,可以根据实际应用要求进行设置和调节,可选地,a
2取值为3~5,b
2取值为2~5。
在一种示例性的实现方式中,Timeout
max2为服务器侧预设的最大超时时间,用于表征能够允许网络恶化到何种程度时必须断开通信连接,是第二超时时间的上限值。可选地,Timeout
max2=c
2*cycle,c
2为服务器侧设置的第六系数,第六系数也为可调系数,通过调节第六系数c
2同步设置Timeout
max2的取值。Timeout
max2可以根据实际应用要求进行设置,本申请实施例不作具体限定,但是Timeout
max2不可无限制的增大,以避免网络极弱时仍维系通信连接导致的无效通信。需要说明的是,Timeout
max1和Timeout
max2可以相同或不同。
在一些实施例中,当i=1时,服务器接收到客户端发送的第一次心跳请求消息Req
1时,向客户端反馈的应答信息Res
1,在等待接收客户端发送的心跳确认消息Sure
1过程中,服务器侧尚未获取到心跳往返时间RTT
B1,即服务器侧尚未更新过第二超时时间,则此时第二超时时间默认为预设的第二阈值时间(记为Timeout
def2)。Timeout
def2可以根据实际应用进行设置,例如Timeout
def2为3*cycle。
也就是说,服务器向客户端发送反馈的应答信息Res
1之后,检测是否在第二阈值时间Timeout
def2内接收到心跳确认消息Sure
1,若是,则获取到心跳往返时间RTT
B1,并根据心跳往返时间RTT
B1更新第二超时时间,即可将第二超时时间由Timeout
def2变更为min(max(a
2*RTT
B1,b
2*cycle),Timeout
max2),以此类推,即可根据i值的递增而动态更新第二超时时间,并检测是否在第二超时时间内接收到对端客户端发送的心跳请求消息、心跳确认消息或数据等。
在网络良好时,可以相对减小第二超时时间;在网络不稳定时,比如网络出现抖动、阻塞、弱网等情况,可以动态调整第二超时时间,相对增大第二超时时间,为接收客户端的任意形式的信息提供更长时限,使通信连接更易于保留,而不会因网络的暂时波动而断连,若在相对较大的第二超时时间下仍旧检测到心跳超时,则说明网络很差,则无需保留通信连接,直接断开连接。本实施例中服务器能够根据网络状态自 适应动态调整本端的第二超时时间,增强适应弱网、网络抖动和阻塞等状态的能力,第二超时时间不会无限制的增加,在网络恶化的一定程度时可以决策超时并断连,提高心跳超时检测的自适应性和准确性,并且整个过程不会影响数据/消息/信息的收发。第二超时时间的自适应调整方式不限于本申请实施例所述。
在一些实施例中,步骤S205服务器与客户端断开通信连接后,客户端可以尝试重新连接。在实际应用场景中,客户端与服务器之间可能存在多次断连和重连,服务器在断开与客户端的通信连接Link
j之后,接收客户端发起的重连请求,响应于重连请求,建立与客户端的通信连接Link
j+1。在通信连接Link
j+1建立成功后,即进入了一个新的循环中,即可执行前述服务器侧的心跳超时检测机制。
在一些实施例中,图4(a)示出了在双侧采用固定超时时间的模式下,客户端10与服务器20的通信示例,客户端10周期性地想服务器发送心跳请求信息Req
i,服务器接收心跳请求信息Req
i后反馈应答信息Res
i。图4(a)中前两次心跳检测时网络状态良好,当i等于3时,客户端于T
AS3节点处发送心跳请求消息Req
3,客户端侧采用第一阈值时间Timeout
def1作为默认超时时间,则以T
AS3节点为基准点,可以构建第3次心跳检测对应的超时检测区间为[T
AS3,T
AS3+Timeout
def1],由于客户端在[T
AS3,T
AS3+Timeout
def1]区分时段内未接收到应答信息Res
3,因此判定为心跳超时,客户端会直接断开与服务器的当前通信连接。因此图4(a)模式下,在出现网络抖动、阻塞等情况,导致双端收发信息存在延迟和滞后,一旦超出固定的默认超时时间直接断连,因此该心跳检测机制抗网络干扰能力差,极易出现频繁断连和重连,增加了双端通信连接的负荷。
在一些实施例中,图4(b)示出了在双侧采用动态自适应调整超时时间的模式下,客户端10与服务器20的一种通信示例,为简化示例以便于描述,图4(b)中仅示出了前两次心跳检测的周期,后续其他心跳检测已省略。图4(b)中第一次心跳检测时网络良好,从第二次心跳检测开始网络出现波动,双端收发消息开始出现滞后性。
参照图4(b),对于客户端侧:发送心跳请求消息Req
1→接收应答信息Res
1(首次更新第一超时时间的节点)→再发送心跳确认消息Sure
1→发送心跳请求消息Req
2→发送心跳请求消息Req
3→接收应答信息Res
2→发送心跳确认消息Sure
2(第二次更新第一超时时间的节点)。
客户端在发送心跳请求消息Req
1后,由于第一超时时间未更新过,因此客户端当前记录的第一超时时间为初始默认的第一阈值时间Timeout
def1,以心跳请求消息Req
1的发送时间T
AS1为基准点,构建客户端侧第1次心跳检测对应的超时检测区间A1为[T
AS1,T
AS1+Timeout
def1],由于客户端在[T
AS1,T
AS1+Timeout
def1]区分时段内接收到了应答信息Res
1,因此记录应答信息Res
1的接收时间T
AR1,得到RTT
A1=T
AR1-T
AS1,并将第一超时时间由Timeout
def1更新为Timeout
A1,Timeout
A1=min(max(a
1*RTT
A1,b
1*cycle),Timeout
max1)。
当达到第2个心跳周期时,客户端向服务器发送心跳请求消息Req
2,即cycle=T
AS2-T
AS1,然后以心跳请求消息Req
2的发送时间T
AS2为基准点,构建客户端侧第2次心跳检测对应的超时检测区间A2为[T
AS2,T
AS2+Timeout
A1],客户端在[T
AS2,T
AS2+Timeout
A1]区间时段内接收到应答信息Res
2,即心跳检测未超时,则向服务器发送心跳确认消息Sure
2,并记录应答信息Res
2的接收时间T
AR2,并得到RTT
A2=T
AR2-T
AS2,并将第一超时时间由Timeout
A1更新为Timeout
A2,Timeout
A2=min(max(a
1*RTT
A2,b
1*cycle),Timeout
max1)。期间,由于网络出现波动,导致应答信息Res
2滞后,RTT
A2大于cycle,因此出现了先发送心跳请求消息Req
3,后接收到应答信息Res
2的情况。以此类推,在未心跳超时的情况下,客户端随着i值的递增而自适应更新第一超时时间,根据更新 的第一超时时间以及关键节点,来定位并构建超时检测区间A
i来进行心跳超时检测;期间若诊断出心跳超时,则断开当前的通信连接。
参照图4(b),对于服务器侧:接收心跳请求消息Req
1→反馈应答信息Res
1→再接收心跳确认消息Sure
1(首次更新第二超时时间的节点)→接收心跳请求消息Req
2→反馈应答信息Res
2→接收心跳请求消息Req
3→反馈应答信息Res
3→接收心跳确认消息Sure
2(第二次更新第二超时时间的节点)。
服务器在接收到心跳请求消息Req
1时,由于第二超时时间未更新过,因此服务器当前记录的第二超时时间为初始默认的第二阈值时间Timeout
def2,以应答信息Res
1的发送时间T
BS1为基准点,构建服务器侧第1次心跳检测对应的超时检测区间B1为[T
BS1,T
BS1+Timeout
def2],由于服务器在[T
BS1,T
BS1+Timeout
def2]区分时段内接收到了心跳确认消息Sure
1,因此记录心跳确认消息Sure
1的接收时间T
BR1,得到RTT
B1=T
BR1-T
BS1,并将第二超时时间由Timeout
def2更新为Timeout
B1,Timeout
B1=min(max(a
2*RTT
B1,b
2*cycle),Timeout
max2)。
在首次更新第二超时时间之后,服务器以心跳确认消息Sure
1的接收时间T
BR1为基准点,构建服务器侧第2次心跳检测对应的超时检测区间B2为[T
BR1,T
BR1+Timeout
B1],并检测在[T
BR1,T
BR1+Timeout
B1]区间时段内是否接收到客户端发送的心跳请求消息Req
2。由于在[T
BR1,T
BR1+Timeout
B1]内接收到心跳请求消息Req
2,确定心跳检测未超时,则保持通信连接,服务器向客户端反馈应答信息Res
2。
服务器以应答信息Res
2的发送时间T
BS2为基准点,构建服务器侧第3次心跳检测对应的超时检测区间B3为[T
BS2,T
BS2+Timeout
B1],由于在[T
BS2,T
BS2+Timeout
B1]区间时段内接收到了心跳确认消息Sure
2,因此记录心跳确认消息Sure
2的接收时间T
BR2,得到RTT
B2=T
BR2-T
BS2,并将第二超时时间由Timeout
B1更新为Timeout
B2,Timeout
B2=min(max(a
2*RTT
B2,b
2*cycle),Timeout
max2)。以此类推,在未心跳超时的情况下,随着i值的递增而自适应更新第二超时时间,根据更新的第二超时时间以及关键节点,来定位和构建超时检测区间B
i来进行心跳超时检测;期间若诊断出心跳超时,则断开当前的通信连接。
通过图4(a)和图4(b)的对比可知,本申请能够基于不同时段网络状态和波动,在客户端侧和服务器侧分别执行超时时间的自适应匹配和更新,在网络存在抖动和阻塞时,两侧的心跳往返时间增大,从而自适应地将两侧的超时时间相对增大,避免图4(a)示例中直接判定超时而断连,提升了通信连接中心跳检测机制的抗网络干扰能力,增强对弱网、网络抖动和网络阻塞的适应能力,并且超时时间不会无限制的增加,在网络恶化的一定程度(比如达到预设的最大超时时间)时,可以决策超时并断连,提高心跳超时检测的自适应性和准确性。图4(a)和图4(b)仅为示例性的,不限定实际的通信和网络状态。
本申请实施例还提供一种电子设备,电子设备中安装有客户端,所述客户端被配置为执行上述客户端侧的心跳超时检测方法。
本申请实施例还提供一种服务器,所述服务器被配置为执行上述服务器侧的心跳超时检测方法。
本申请实施例还提供一种电子设备,包括:存储器和至少一个处理器;所述存储器,用于存储指令;所述至少一个处理器,用于执行所述存储器中的所述指令,执行以上客户端侧配置的心跳超时检测方法。
本申请实施例还提供一种服务器,包括:存储器和至少一个处理器;所述存储器,用于存储指令;所述至少一个处理器,用于执行所述存储器中的所述指令,执行以上 服务器侧配置的心跳超时检测方法。
本申请实施例还提供一种计算机可读存储介质,包括指令,当其在计算机上运行时,使得计算机执行以上客户端侧配置的心跳超时检测方法。
本申请实施例还提供一种计算机可读存储介质,包括指令,当其在计算机上运行时,使得计算机执行以上服务器侧配置的心跳超时检测方法。
最后应说明的是:以上各实施例仅用以说明本申请的技术方案,而非对其限制;尽管参照前述各实施例对本申请进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分或者全部技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本申请各实施例技术方案的范围。
为了方便解释,已经结合具体的实施方式进行了上述说明。但是,上述示例性的讨论不是意图穷尽或者将实施方式限定到上述公开的具体形式。根据上述的教导,可以得到多种修改和变形。上述实施方式的选择和描述是为了更好的解释原理以及实际的应用,从而使得本领域技术人员更好的使用所述实施方式以及适于具体使用考虑的各种不同的变形的实施方式。
Claims (12)
- 一种心跳超时检测方法,其特征在于,包括:在客户端与服务器通信连接成功之后,客户端向服务器发送第i个检测周期对应的心跳请求消息,并记录所述心跳请求消息的发送时间;客户端若在本端当前记录的第一超时时间内,接收到服务器反馈的同属第i个检测周期内的应答信息,记录所述应答信息的接收时间,并根据所述心跳请求消息的发送时间和所述应答信息的接收时间,更新所述第一超时时间;客户端若在所述第一超时时间内未接收到服务器反馈的同属第i个检测周期内的应答信息,则断开与服务器的通信连接。
- 根据权利要求1所述的方法,其特征在于,客户端按照如下方式更新所述第一超时时间:计算所述心跳请求消息的发送时间与所述应答信息的接收时间的时间差,得到客户端第i个检测周期对应的心跳往返时间;根据客户端第i个检测周期对应的心跳往返时间,自适应匹配新的第一超时时间。
- 根据权利要求2所述的方法,其特征在于,客户端按照如下公式自适应匹配新的第一超时时间;Timeout Ai=min(max(a 1*RTT Ai,b 1*cycle),Timeout max1)式中,Timeout Ai为所述第一超时时间,cycle为相邻两次心跳请求消息的预设发送间隔,a 1为客户端设置的第一系数,b 1为客户端设置的第二系数,Timeout max1为客户端预设的最大超时时间,RTT Ai为客户端第i个检测周期对应的心跳往返时间。
- 根据权利要求1所述的方法,其特征在于,所述方法还包括:客户端在所述第一超时时间内接收到服务器反馈的同属第i个检测周期内的应答信息时,向服务器发送同属第i个检测周期内的心跳确认消息,所述心跳确认消息用于指示服务器记录所述心跳确认消息的接收时间,所述心跳确认消息的接收时间用于计算服务器端第i个检测周期对应的心跳往返时间。
- 根据权利要求1所述的方法,其特征在于,所述方法还包括:客户端在断开与服务器的第j次通信连接之后,将本端记录的i值清零,并向服务器发送重连请求,所述重连请求用于向服务器请求建立第j+1次通信连接;在第j+1次通信连接建立成功后,客户端在接收到服务器首次反馈的应答信息之前,将预设的第一阈值时间作为所述第一超时时间。
- 一种心跳超时检测方法,其特征在于,包括:在服务器与客户端通信连接成功之后,服务器接收客户端发送的第i个检测周期对应的心跳请求消息;服务器向客户端反馈同属第i个检测周期内的应答信息,并记录所述应答信息的发送时间;服务器若在本端当前记录的第二超时时间内,接收到客户端发送的同属第i个检测周期内的心跳确认消息,记录所述心跳确认消息的接收时间,并根据所述应答信息的发送时间和所述心跳确认消息的接收时间,更新所述第二超时时间;服务器若在所述第二超时时间内未接收到客户端发送的同属第i个检测周期内的心跳确认消息,则断开与客户端的通信连接。
- 根据权利要求6所述的方法,其特征在于,服务器按照如下方式确定更新所述 第二超时时间:计算所述应答信息的发送时间与所述心跳确认消息的接收时间的时间差,得到服务器端第i个检测周期对应的心跳往返时间;根据服务器端第i个检测周期对应的心跳往返时间,自适应匹配新的第二超时时间。
- 根据权利要求7所述的方法,其特征在于,服务器按照如下公式自适应匹配新的第二超时时间;Timeout Bi=min(max(a 2*RTT Bi,b 2*cycle),Timeout max2)式中,Timeout Bi为所述第二超时时间,cycle为相邻两次心跳请求消息的预设发送间隔,a 2为服务器设置的第三系数,b 2为服务器设置的第四系数,Timeout max2为服务器预设的最大超时时间,RTT Bi为服务器端第i个检测周期对应的心跳往返时间。
- 根据权利要求6所述的方法,其特征在于,所述方法还包括:服务器在更新所述第二超时时间之后,以第i个检测周期内心跳确认消息的接收时间为基准点,检测在更新后的第二超时时间内是否接收到客户端发送的第i+1个检测周期对应的心跳请求消息;服务器若在更新后的第二超时时间内接收到客户端发送的第i+1个检测周期对应的心跳请求消息,则向客户端反馈同属第i+1个检测周期内的应答信息;服务器若在更新后的第二超时时间内未接收到客户端发送的第i+1个检测周期对应的心跳请求消息,则断开与客户端的通信连接。
- 根据权利要求6或9所述的方法,其特征在于,所述方法还包括:服务器在断开与客户端的第j次通信连接之后,接收客户端发起的重连请求;服务器响应于所述重连请求,建立与客户端的第j+1次通信连接;在第j+1次通信连接建立成功后,服务器在接收到客户端首次发送的心跳确认消息之前,将预设的第二阈值时间作为所述第二超时时间。
- 一种电子设备,其特征在于,所述电子设备安装有客户端,所述客户端被配置为:在客户端与服务器通信连接成功之后,向服务器发送第i个检测周期对应的心跳请求消息,并记录所述心跳请求消息的发送时间;若在本端当前记录的第一超时时间内,接收到服务器反馈的同属第i个检测周期内的应答信息,记录所述应答信息的接收时间,并根据所述心跳请求消息的发送时间和所述应答信息的接收时间,更新所述第一超时时间;若在所述第一超时时间内未接收到服务器反馈的同属第i个检测周期内的应答信息,则断开与服务器的通信连接。
- 一种服务器,其特征在于,所述服务器被配置为:在服务器与客户端通信连接成功之后,接收客户端发送的第i个检测周期对应的心跳请求消息;向客户端反馈同属第i个检测周期内的应答信息,并记录所述应答信息的发送时间;若在本端当前记录的第二超时时间内,接收到客户端发送的同属第i个检测周期内的心跳确认消息,记录所述心跳确认消息的接收时间,并根据所述应答信息的发送时间和所述心跳确认消息的接收时间,更新所述第二超时时间;若在所述第二超时时间内未接收到客户端发送的同属第i个检测周期内的心跳确认消息,则断开与客户端的通信连接。
Applications Claiming Priority (2)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202110725277.0A CN113472606B (zh) | 2021-06-29 | 2021-06-29 | 一种心跳超时检测方法、服务器及电子设备 |
CN202110725277.0 | 2021-06-29 |
Publications (1)
Publication Number | Publication Date |
---|---|
WO2022105611A1 true WO2022105611A1 (zh) | 2022-05-27 |
Family
ID=77873925
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
PCT/CN2021/128489 WO2022105611A1 (zh) | 2021-06-29 | 2021-11-03 | 一种心跳超时检测方法、服务器及电子设备 |
Country Status (2)
Country | Link |
---|---|
CN (1) | CN113472606B (zh) |
WO (1) | WO2022105611A1 (zh) |
Cited By (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115134237A (zh) * | 2022-08-30 | 2022-09-30 | 建信金融科技有限责任公司 | 数据更新方法、心跳检测方法、装置、设备及介质 |
CN115529164A (zh) * | 2022-08-30 | 2022-12-27 | 中电云数智科技有限公司 | 一种waf旁路部署系统和方法 |
CN115987854A (zh) * | 2022-11-30 | 2023-04-18 | 广州广电运通信息科技有限公司 | 网络连接的调节方法、设备、系统及存储介质 |
CN116614512A (zh) * | 2023-02-21 | 2023-08-18 | 北京友友天宇系统技术有限公司 | 分布式群组通信的强一致性群组视图管理方法、装置和设备 |
CN116755464A (zh) * | 2023-05-17 | 2023-09-15 | 贵州师范学院 | 一种基于物联网的移动机器人的控制方法 |
CN117294750A (zh) * | 2023-11-27 | 2023-12-26 | 杭州涂鸦信息技术有限公司 | 调整mqtt心跳频率的方法和装置 |
CN118488089A (zh) * | 2024-07-15 | 2024-08-13 | 宁波银行股份有限公司 | 一种网络连接优化方法、装置、设备及存储介质 |
Families Citing this family (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113472606B (zh) * | 2021-06-29 | 2022-09-30 | 聚好看科技股份有限公司 | 一种心跳超时检测方法、服务器及电子设备 |
CN113821414B (zh) * | 2021-11-22 | 2022-02-08 | 成都新希望金融信息有限公司 | 一种服务器保护方法、装置、电子设备及存储介质 |
CN114143114B (zh) * | 2022-01-12 | 2024-08-06 | 福建省海峡信息技术有限公司 | 一种基于智能终端的网络安全通信方法 |
CN114553778B (zh) * | 2022-02-25 | 2024-08-30 | 杭州网易云音乐科技有限公司 | 心跳控制方法及装置、存储介质、电子设备 |
CN115103001B (zh) * | 2022-05-10 | 2024-03-08 | 航天国政信息技术(北京)有限公司 | 一种通信方法、装置及电子设备 |
CN115334141B (zh) * | 2022-08-03 | 2024-08-30 | 上海艺赛旗软件股份有限公司 | 一种基于心跳机制的双向数据传输系统、方法及装置 |
CN115460227A (zh) * | 2022-11-14 | 2022-12-09 | 成都怡康科技有限公司 | 同步数据的方法、装置、系统、计算机设备及存储介质 |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20130151714A1 (en) * | 2011-12-13 | 2013-06-13 | Motorola Mobility, Inc. | Method and apparatus for adaptive network heartbeat message for tcp channel |
CN104601376A (zh) * | 2015-01-07 | 2015-05-06 | 北京华为数字技术有限公司 | 心跳报文发送方法及装置 |
CN105553768A (zh) * | 2015-12-15 | 2016-05-04 | 浙江工商大学 | 一种基于自适应超时时间的sdn控制器故障检测方法 |
CN112235151A (zh) * | 2020-08-27 | 2021-01-15 | 北京计算机技术及应用研究所 | 一种基于时间序列预测的自适应心跳检测方法 |
CN113472606A (zh) * | 2021-06-29 | 2021-10-01 | 聚好看科技股份有限公司 | 一种心跳超时检测方法、服务器及电子设备 |
Family Cites Families (15)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US5774479A (en) * | 1995-03-30 | 1998-06-30 | Motorola, Inc. | Method and system for remote procedure call via an unreliable communication channel using multiple retransmission timers |
US6782496B2 (en) * | 2001-04-13 | 2004-08-24 | Hewlett-Packard Development Company, L.P. | Adaptive heartbeats |
US7323991B1 (en) * | 2005-05-12 | 2008-01-29 | Exavera Technologies Incorporated | System and method for locating and communicating with personnel and equipment in a facility |
CN101106535B (zh) * | 2007-08-14 | 2011-04-13 | 华为技术有限公司 | 一种实现心跳机制的方法、服务器和系统 |
KR20140032542A (ko) * | 2012-08-30 | 2014-03-17 | 삼성전자주식회사 | 무선 네트워크에서 푸시 서비스의 hearthbeat 주기 결정 방법 및 장치 |
CN104426692A (zh) * | 2013-08-26 | 2015-03-18 | 联想(北京)有限公司 | 一种信息处理方法,一种装置及网络服务器 |
EP3103218A4 (en) * | 2014-02-04 | 2017-09-06 | Distrix Networks Ltd. | Bandwidth and latency estimation in a communication network |
CN105245531B (zh) * | 2015-10-21 | 2018-12-25 | 北京捷思锐科技股份有限公司 | 一种掉线检测方法、装置及服务器 |
CN106301969B (zh) * | 2016-10-25 | 2019-07-30 | 广东亿迅科技有限公司 | Http长链接的管理方法及系统 |
CN106685755A (zh) * | 2016-12-12 | 2017-05-17 | 上海斐讯数据通信技术有限公司 | 网络竞标的控制方法、系统及网络竞标平台 |
CN109905259B (zh) * | 2017-12-08 | 2022-02-15 | 中国电信股份有限公司 | 通信连接维持方法、系统和相关设备 |
CN110661673B (zh) * | 2018-06-28 | 2021-02-26 | 网宿科技股份有限公司 | 一种心跳检测的方法及装置 |
CN110740073B (zh) * | 2018-07-20 | 2022-07-19 | 阿里巴巴集团控股有限公司 | 服务等级的计算方法、装置和系统 |
CN111698098A (zh) * | 2019-03-15 | 2020-09-22 | 北京京东尚科信息技术有限公司 | 通信方法、装置和计算机可读存储介质 |
CN113001590A (zh) * | 2021-03-22 | 2021-06-22 | 深圳市普渡科技有限公司 | 机器人故障恢复方法、装置、设备和计算机可读存储介质 |
-
2021
- 2021-06-29 CN CN202110725277.0A patent/CN113472606B/zh active Active
- 2021-11-03 WO PCT/CN2021/128489 patent/WO2022105611A1/zh unknown
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20130151714A1 (en) * | 2011-12-13 | 2013-06-13 | Motorola Mobility, Inc. | Method and apparatus for adaptive network heartbeat message for tcp channel |
CN104601376A (zh) * | 2015-01-07 | 2015-05-06 | 北京华为数字技术有限公司 | 心跳报文发送方法及装置 |
CN105553768A (zh) * | 2015-12-15 | 2016-05-04 | 浙江工商大学 | 一种基于自适应超时时间的sdn控制器故障检测方法 |
CN112235151A (zh) * | 2020-08-27 | 2021-01-15 | 北京计算机技术及应用研究所 | 一种基于时间序列预测的自适应心跳检测方法 |
CN113472606A (zh) * | 2021-06-29 | 2021-10-01 | 聚好看科技股份有限公司 | 一种心跳超时检测方法、服务器及电子设备 |
Cited By (10)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN115134237A (zh) * | 2022-08-30 | 2022-09-30 | 建信金融科技有限责任公司 | 数据更新方法、心跳检测方法、装置、设备及介质 |
CN115529164A (zh) * | 2022-08-30 | 2022-12-27 | 中电云数智科技有限公司 | 一种waf旁路部署系统和方法 |
CN115987854A (zh) * | 2022-11-30 | 2023-04-18 | 广州广电运通信息科技有限公司 | 网络连接的调节方法、设备、系统及存储介质 |
CN116614512A (zh) * | 2023-02-21 | 2023-08-18 | 北京友友天宇系统技术有限公司 | 分布式群组通信的强一致性群组视图管理方法、装置和设备 |
CN116614512B (zh) * | 2023-02-21 | 2024-04-26 | 北京友友天宇系统技术有限公司 | 分布式群组通信的强一致性群组视图管理方法、装置和设备 |
CN116755464A (zh) * | 2023-05-17 | 2023-09-15 | 贵州师范学院 | 一种基于物联网的移动机器人的控制方法 |
CN116755464B (zh) * | 2023-05-17 | 2024-04-16 | 贵州师范学院 | 一种基于物联网的移动机器人的控制方法 |
CN117294750A (zh) * | 2023-11-27 | 2023-12-26 | 杭州涂鸦信息技术有限公司 | 调整mqtt心跳频率的方法和装置 |
CN117294750B (zh) * | 2023-11-27 | 2024-03-01 | 杭州涂鸦信息技术有限公司 | 调整mqtt心跳频率的方法和装置 |
CN118488089A (zh) * | 2024-07-15 | 2024-08-13 | 宁波银行股份有限公司 | 一种网络连接优化方法、装置、设备及存储介质 |
Also Published As
Publication number | Publication date |
---|---|
CN113472606B (zh) | 2022-09-30 |
CN113472606A (zh) | 2021-10-01 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
WO2022105611A1 (zh) | 一种心跳超时检测方法、服务器及电子设备 | |
US20240031286A1 (en) | Systems, Apparatuses and Methods for Network Packet Management | |
US11558302B2 (en) | Data transmission method and apparatus | |
CN106304203B (zh) | 数据传输方法及装置 | |
JP6526825B2 (ja) | 伝送制御プロトコルtcpデータパケットを送信する方法及び装置、並びにシステム | |
WO2018133454A1 (zh) | 远程服务访问路径控制方法和相关设备 | |
US7133368B2 (en) | Peer-to-peer method of quality of service (QoS) probing and analysis and infrastructure employing same | |
US20230019976A1 (en) | Methods and systems for implementing a regionally contiguous proxy service | |
US7826363B2 (en) | Bandwidth estimation algorithm using internet control message protocol (ICMP) echo request | |
JP2006528871A (ja) | デバイスのピン負荷を制御するライブネスピンプロトコル | |
CN113783775B (zh) | 数据传输的方法和装置 | |
WO2023066125A1 (zh) | 降低通信设备休眠状态下功耗的方法、装置、设备及介质 | |
CN104683259A (zh) | Tcp拥塞控制方法及装置 | |
US6850488B1 (en) | Method and apparatus for facilitating efficient flow control for multicast transmissions | |
US20240223503A1 (en) | Data transmission control method and apparatus, computer-readable storage medium, computer device, and computer program product | |
US11528345B2 (en) | Data transmission method and system, and apparatus | |
WO2014161421A1 (zh) | 数据传输方法、设备及系统 | |
CN109600273A (zh) | 一种udp报文传输性能的优化方法及系统 | |
WO2018087721A1 (en) | Systems, apparatuses and methods for cooperating routers | |
US20070005741A1 (en) | Facilitating radio communications in a mobile device | |
Pu et al. | Enhancements on router-assisted congestion control for wireless networks | |
Jun et al. | Robust information dissemination in uncooperative environments | |
CN114205405B (zh) | 一种bfd报文发送方法、装置、电子设备及存储介质 | |
EP4231607A1 (en) | Data transmission method and communication apparatus | |
CN109039940B (zh) | 一种数据传输的负载均衡方法及装置 |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 21893758 Country of ref document: EP Kind code of ref document: A1 |
|
NENP | Non-entry into the national phase |
Ref country code: DE |
|
32PN | Ep: public notification in the ep bulletin as address of the adressee cannot be established |
Free format text: NOTING OF LOSS OF RIGHTS PURSUANT TO RULE 112(1) EPC (EPO FORM 1205A DATED 22/04/2024) |