Connection keep-alive method and device
By detecting the idle time of long connections at the application layer and dynamically adjusting the threshold, the problem of difficulty in maintaining long connections in specific scenarios in the existing technology is solved, and timely discovering and processing of long connections in various scenarios is realized, which improves the improvement of Reliability and real-time connection.
Patent Information
- Application Number
- CN202411275896.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-12
- Publication Date
- 2024-12-17
- Estimated Expiration
- 2044-09-12
AI Technical Summary
In the long-connection management, the existing technology relies on the detection mechanism defined by the transport layer or network layer protocol, which makes it impossible to meet the needs of long-connection keeping in certain scenarios, especially in mobile networks and complex situations, the detection mechanism is long. Moreover, client incompatibility makes it difficult to detect connection disconnection in time.
By implementing a long connection keep-alive mechanism at the application layer, the current idle time is obtained and compared with the target idle time is changed. If the threshold is exceeded, the connection will be disconnected and re-established. The target historical idle time and preset tolerance coefficient will be used to dynamically adjust the threshold to Meet the long-connected and maintaining needs in various scenarios.
It realizes long connection keep-aliveness without relying on the transport layer or network layer protocol, and can detect and handle disconnections in a timely manner. It is suitable for various scenarios and improves the reliability and real-timeness of the connection.
Smart Images

Figure CN119155339A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a connection keep-alive method and device. Background Art
[0002] A long connection refers to a persistent connection between the client and the server in network communication, so that both parties can reuse the connection for communication over a long period of time without establishing a new connection for each request. The purpose of keeping a long connection alive is to ensure that the long connection can be discovered in time when it has been disconnected. According to different protocols and application scenarios, the common ways to keep a long connection alive are detection and idle disconnection. Detection refers to one end of a long connection sending data to the other end through a long connection according to the transport layer or network layer protocol, and then determining whether a response can be obtained. If a response is obtained, it is determined that the connection is available and the long connection is maintained. Otherwise, the connection is unavailable and the long connection is disconnected. Idle disconnection refers to the active disconnection of both ends of a long connection after the duration of no data exchange in the connection reaches a threshold.
[0003] In the actual application layer development process, the management of underlying persistent connections when accessing network resources is generally implemented through specific clients. In this case, application layer developers often rely on the detection mechanism defined by the transport layer or network layer's own protocol, as well as the specific implementation of the client at the application layer to achieve the purpose of keeping persistent connections alive. However, due to the long detection mechanism defined by the transport layer or network layer's own protocol, as well as factors such as the incompatibility of the client at the application layer, it is difficult to take into account all application scenarios, which makes it impossible to meet the needs of keeping persistent connections alive in specific scenarios. Summary of the invention
[0004] The purpose of the embodiment of the present invention is to provide a connection keep-alive method and device, so as to propose a detection mechanism that does not rely on the transport layer or network layer's own protocol definition, and the specific implementation of the client at the application layer, but the top-level application decides whether to actively disconnect and reconnect to achieve a blocking long connection keep-alive mechanism that can promptly discover that the connection has been disconnected, thereby meeting the needs of long connection keep-alive in various scenarios. The specific technical solution is as follows:
[0005] In a first aspect of the implementation of the present invention, a connection keep-alive method is first provided, which is applied to an electronic device, wherein the electronic device is deployed with an application at one end of a long connection, and the method includes:
[0006] Obtain the current idle time of the long connection, where the current idle time is the time interval between the last time the long connection transmitted a message and the current time;
[0007] Determine a target historical idle duration from a first number of historical idle durations of the long connection, where the historical idle duration is: a duration interval between two adjacent messages transmitted by the long connection;
[0008] Determine a target idle time according to the target historical idle time;
[0009] Detecting whether the current idle time is greater than the target idle time;
[0010] If so, disconnect the persistent connection and re-establish the persistent connection.
[0011] In one embodiment of the present invention, the detecting whether the current idle time is greater than the target idle time includes:
[0012] Detecting whether the current idle time is greater than the target idle time, and whether the first number is greater than a preset minimum number of messages;
[0013] If the current idle time is greater than the target idle time, and the first number is greater than the preset minimum number of messages, the steps of disconnecting the long connection and re-establishing the long connection are performed;
[0014] If the current idle time duration is not greater than the target idle time duration, and / or the first number is not greater than a preset minimum number of messages, the step of maintaining the long connection is performed.
[0015] In one embodiment of the present invention, if the current idle duration is not greater than the target idle duration, and / or the first number is not greater than the preset minimum number of messages, before executing the step of maintaining the long connection, the step further includes:
[0016] Detecting whether the current idle time is greater than a preset maximum idle time;
[0017] If yes, executing the steps of disconnecting the persistent connection and re-establishing the persistent connection;
[0018] If not, the step of maintaining the long connection is performed.
[0019] In one embodiment of the present invention, determining the target idle duration according to the target historical idle duration includes:
[0020] The target idle time is determined according to the target historical idle time and a preset tolerance coefficient.
[0021] In one embodiment of the present invention, when the historical idle durations are arranged in order of size, a ratio of the order of the target historical idle durations to the first number is a preset ratio.
[0022] In one embodiment of the present invention, determining the target historical idle duration from the first number of historical idle durations of the long connection includes:
[0023] A target historical idle duration at a preset percentile value is determined from the first number of historical idle durations of the long connection.
[0024] In a second aspect of the implementation of the present invention, a connection keep-alive device is further provided, which is applied to an electronic device, wherein the electronic device is deployed with an application located at one end of a long connection, and the device includes:
[0025] A current idle duration acquisition module is used to acquire the current idle duration of the long connection, where the current idle duration is the time interval between the last time the long connection transmitted a message and the current time;
[0026] A target historical idle duration determination module, configured to determine a target historical idle duration from a first number of historical idle durations of the long connection, wherein the historical idle duration is: a duration interval between two adjacent messages transmitted by the long connection;
[0027] A target idle duration determination module, used to determine a target idle duration according to the target historical idle duration;
[0028] A detection module, used to detect whether the current idle time is greater than the target idle time;
[0029] The long connection disconnection module is used to disconnect the long connection and re-establish the long connection when the current idle time is longer than the target idle time.
[0030] The long connection maintaining module is used to maintain the long connection when the current idle time is not greater than the target idle time.
[0031] In one embodiment of the present invention, the detection module is specifically used to:
[0032] Detecting whether the current idle time is greater than the target idle time, and whether the first number is greater than a preset minimum number of messages;
[0033] When the current idle time is greater than the target idle time, and the first number is greater than a preset minimum number of messages, calling the persistent connection disconnection module to execute the steps of disconnecting the persistent connection and re-establishing the persistent connection;
[0034] When the current idle time duration is not greater than the target idle time duration, and / or the first number is not greater than a preset minimum number of messages, a step of maintaining the long connection is performed.
[0035] In one embodiment of the present invention, the detection module is further used to detect whether the current idle duration is greater than a preset maximum idle duration before executing the step of maintaining the long connection when the current idle duration is not greater than the target idle duration and / or the first number is not greater than a preset minimum number of messages;
[0036] When the current idle time is longer than the preset maximum idle time, calling the persistent connection disconnection module to execute the steps of disconnecting the persistent connection and re-establishing the persistent connection;
[0037] When the current idle time is not greater than the preset maximum idle time, the step of maintaining the long connection is performed.
[0038] In one embodiment of the present invention, when the historical idle durations are arranged in order of size, a ratio of the order of the target historical idle durations to the first number is a preset ratio.
[0039] In one embodiment of the present invention, the target idle duration determination module is specifically configured to determine the target idle duration according to the target historical idle duration and a preset tolerance coefficient.
[0040] In one embodiment of the present invention, the target historical idle duration determination module is specifically used to:
[0041] A target historical idle duration at a preset percentile value is determined from the first number of historical idle durations of the long connection.
[0042] In another aspect of the present invention, there is provided an electronic device, comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0043] Memory, used to store computer programs;
[0044] The processor is used to implement any of the above-mentioned connection keep-alive methods when executing the program stored in the memory.
[0045] In another aspect of the present invention, a computer-readable storage medium is provided, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, any of the above-mentioned connection keep-alive methods is implemented.
[0046] In yet another aspect of the present invention, a computer program product comprising instructions is provided, which, when executed on a computer, enables the computer to execute any of the above-mentioned connection keep-alive methods.
[0047] In the technical solution provided by the embodiment of the present invention, whether the long connection is to be disconnected is determined by detecting whether the current idle time of the long connection is greater than the target idle time. Since the current idle time can characterize the frequency of the long connection currently transmitting messages, and the target idle time is determined from the first number of historical idle times of the long connection, that is, the target idle time can characterize the frequency of the long connection transmitting historical information, that is, in the embodiment of the present invention, by comparing the frequency of the long connection currently transmitting messages with the frequency of the long connection transmitting historical information, the long connection is kept alive, which does not rely on the detection mechanism defined by the transport layer or the network layer's own protocol, and the specific implementation of the client at the application layer, so that the needs of long connection keeping alive in various scenarios can be met. BRIEF DESCRIPTION OF THE DRAWINGS
[0048] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art are briefly introduced below.
[0049] Figure 1 A schematic diagram of blocking monitoring of a long connection provided by an embodiment of the present invention;
[0050] Figure 2 A schematic diagram of a flow chart of a first connection keep-alive method provided in an embodiment of the present invention;
[0051] Figure 3 A schematic diagram of a message transmission based on a long connection provided by an embodiment of the present invention;
[0052] Figure 4 A schematic diagram of a flow chart of a second connection keep-alive method provided in an embodiment of the present invention;
[0053] Figure 5 A schematic diagram of a flow chart of a third connection keep-alive method provided in an embodiment of the present invention;
[0054] Figure 6 A schematic diagram of a fourth connection keep-alive method provided in an embodiment of the present invention;
[0055] Figure 7 A schematic diagram of the structure of a connection keep-alive device provided in an embodiment of the present invention;
[0056] Figure 8 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0057] The technical solutions in the embodiments of the present invention will be described below in conjunction with the accompanying drawings in the embodiments of the present invention.
[0058] A persistent connection is a connection between a client and a server in a network communication, so that both parties can reuse the connection for communication over a long period of time without establishing a new connection for each request. The purpose of keeping a persistent connection alive is to ensure that a persistent connection can be discovered in time when it is disconnected. Depending on different protocols and application scenarios, common ways to keep a persistent connection alive include detection and idle disconnection.
[0059] Detection refers to one end of a long connection sending data to the other end through a long connection according to the transport layer or network layer protocol, and then determining whether a response can be obtained. If a response can be obtained, the connection is determined to be available and the long connection is maintained. Otherwise, the connection is unavailable and the long connection is disconnected. For example, in the implementation of the Transmission Control Protocol (TCP), a KeepAlive message is provided to detect whether the other end of the long connection is alive. For another example, in the implementation of the WebSocket protocol (a network protocol based on TCP), a heartbeat message is provided to detect whether the other end of the long connection is alive.
[0060] Idle disconnection means that the two ends of a persistent connection actively disconnect after the duration of no data exchange in the persistent connection reaches a threshold. For example, in the implementation of the HyperText Transfer Protocol (HTTP), a Keepalive-timeout time setting parameter is provided. If one end of the persistent connection has not received a request message from the other end within the set time, the persistent connection is closed. Another example is the maximum idle time setting for various database connections.
[0061] In the actual application layer development process, just like people do not develop their own browsers when accessing a web page, the management of underlying persistent connections when accessing network resources is generally implemented through specific clients. For example, when accessing various databases and middleware, developers will use existing and mature open source clients. In this case, application layer developers often have to rely on the detection mechanism defined by the transport layer or network layer's own protocol, as well as the specific implementation of the client at the application layer to achieve the purpose of keeping persistent connections alive.
[0062] However, due to the long detection mechanism time defined by the transport layer or network layer protocol itself, it cannot meet the needs of long connection keepalive in certain scenarios. Taking TCP as an example, the TCP protocol provides a KeepAlive mechanism to implement long connection keepalive. After the Keep Alive mechanism is turned on, the TCP layer will send the corresponding KeepAlive detection message after the scheduled time to determine the connection availability. The default time is 7200s (two hours), and it will retry 10 times after failure, with a timeout of 75s each time. Obviously, this default value cannot meet the needs of mobile networks. In addition, in complex situations such as router failure and network cable unplugging, the KeepAlive mechanism provided by the TCP protocol will also fail.
[0063] In addition, due to the incompatibility of the client at the application layer, the need for keeping long connections alive cannot be met in certain scenarios. For example, due to security considerations, the common long connection management end cannot write data to the long connection while blocking the long connection for monitoring. This determines that the long connection management end cannot perform heartbeat detection while blocking the long connection for monitoring. In the monitoring scenario, because the monitoring message needs to be blocked, the client needs to keep monitoring and will not disconnect when idle. This will cause the long connection to be disconnected for a long time before it is discovered, which does not meet the need for long connection protection.
[0064] For ease of understanding, the above situation is described below by taking the Redis pub / sub (publish / subscribe based on a remote dictionary server) function as an example.
[0065] See also Figure 1 , Figure 1A schematic diagram of blocking and listening to a long connection provided for an embodiment of the present invention. When using the Redis pub / sub function, the subscriber needs to block and listen to a long connection to obtain the subscription message published by the publisher. Jedis is a Java client development kit officially recommended by Redis. As a middleware, it integrates the command operation of Redis and provides connection pool management. Through Jedis, you can connect to Redis and operate Redis. In short, Jedis is a more common connection management client. However, Jedis is based on thread-unsafe Socket (socket) connection. In order to avoid unpredictable situations, data cannot be written to the Socket connection during blocking listening, which determines that Jedis cannot do heartbeat detection while blocking and listening to the Socket connection. In the monitoring scenario, because it is necessary to block and wait for messages, the client needs to keep listening and will not disconnect idle. Therefore, when the connection is abnormally disconnected, unless the client captures it (which often does not happen, such as network interruption) or relies on the detection mechanism of the TCP protocol (the default requires hour level), the fact that the connection has been disconnected will be discovered. This has a large uncertainty for application functions and cannot meet the needs of long connection keep alive.
[0066] In order to solve the above problem, an embodiment of the present invention provides a connection keep-alive method and device.
[0067] See also Figure 2 , which is a flow chart of the first connection keep-alive method provided in an embodiment of the present invention. The method is applied to an electronic device, and the electronic device is deployed with an application located at one end of a long connection. The electronic device can specifically be a client, a server, etc., and the application at one end of the long connection can specifically be an application located on the server at one end of the long connection, or an application located on the client at the other end of the long connection. For ease of explanation, the following description is made by taking the execution subject as an application on the client (hereinafter referred to as the client) as an example, which does not serve as a limitation. The above method includes steps S201-S205.
[0068] S201, obtaining the current idle time of the long connection.
[0069] The current idle time is the time interval between the last time the long connection transmitted a message and the current time.
[0070] In the embodiment of the present invention, the client can record the timestamp of the current moment and the timestamp of the most recent message transmission of the persistent connection, and obtain the current idle time of the persistent connection based on the two timestamps. The timestamp of the most recent message transmission of the persistent connection can be the timestamp of the most recent message sent by the client at one end of the persistent connection, or the timestamp of the most recent message received by the server at one end of the persistent connection.
[0071] S202: Determine a target historical idle duration from a first number of historical idle durations of the long connection.
[0072] The above historical idle time is: the time interval between two adjacent messages transmitted by the long connection. Assuming that the long connection transmits N messages, the client can determine N-1 historical idle times.
[0073] For ease of understanding, combined Figure 3 The process of determining the current idle time and the historical idle time according to the embodiment of the present invention is described.
[0074] See also Figure 3 , which is a schematic diagram of a message transmission based on a long connection provided in an embodiment of the present invention. Figure 3 The time when the client sends the message is used as the basis for determining the current idle time and the historical idle time, and does not serve as a limitation.
[0075] Message m is continuously transmitted based on the long connection. When the client sends message m1, the time is t1. Message m2 is the next message of message m1. When the client sends message m2, the time is t2. Then the client can determine the historical idle time i1=t2-t1, where i1 represents the idle time of the long connection after the client sends message m1.
[0076] Assuming that the persistent connection transmits n messages, that is, the client sends message mn, there will be i1, i2, ... in-1 determined historical idle times. If the current time is t, the client can determine the current idle time i = t-tn.
[0077] The first number can be set according to actual needs. The minimum value of the first number is 1, and the maximum value is the number of transmission messages of the long connection minus 1. In some scenarios with high real-time requirements, that is, the connection status of the long connection needs to be detected in time, the larger the value of the first number, the better. In some scenarios with general real-time requirements and limited computing resources, the value of the first number can be a smaller value.
[0078] In one embodiment of the present invention, when the value of the first number is a maximum value, the client uses all the determined historical idle time periods as the first number of historical idle time periods.
[0079] In another embodiment of the present invention, when the value of the first number is not the maximum value, the client can divide all historical idle times into multiple sets based on the duration, and extract a certain number of historical idle times from each of the multiple sets to obtain a first number of historical idle times.
[0080] For example, if a long connection transmits 100 messages, the client can determine 99 historical idle durations, and the first number is 10. Based on the duration, the client can put the historical idle durations in the range of 1s-5s into set 1, the historical idle durations in the range of 5s-10s into set 2, and the idle durations in other intervals into set 3. Three historical idle durations are randomly selected from set 1, three historical idle durations are randomly selected from set 2, and four historical idle durations are randomly selected from set 3, so as to obtain 10 (the first number) historical idle durations.
[0081] Alternatively, the client may select the first number of historical idle times that have been most recently obtained.
[0082] In one embodiment of the present invention, the client may select the largest historical idle time from the first number of historical idle time as the target historical idle time, so as to reduce the probability of the client accidentally disconnecting the long connection.
[0083] In one embodiment of the present invention, the client may select the smallest historical idle time from the first number of historical idle time as the target historical idle time, so as to ensure that the client can find out in time that the long connection has been disconnected.
[0084] In one embodiment of the present invention, the client can select a historical idle time at the median from the first number of historical idle time as the target historical idle time. In this way, the client can strike a balance between reducing the probability of accidentally disconnecting a long connection and promptly discovering that the long connection has been disconnected.
[0085] In one embodiment of the present invention, the client may further calculate an average value of the first number of historical idle time periods, and use the average value as the target historical idle time period.
[0086] In order to determine a representative target historical idle time from the first number of historical idle times, in one embodiment of the present invention, when the historical idle times are arranged in order of size, the ratio of the order of the target historical idle time to the first number is a preset ratio. The historical idle times are arranged in order of size, which can be arranged in order from small to large or in order from large to small. When the historical idle times are arranged in order from small to large, when the preset ratio is 0.6, it means that the ratio of the order of the target historical idle time to the first number is 0.6. When the preset ratio is the minimum value (1 / first number), it means that the target historical idle time is the smallest historical idle time among the first number of historical idle times. When the preset ratio is the maximum value (1), it means that the target historical idle time is the largest historical idle time among the first number of historical idle times. When the historical idle times are arranged in order from large to small, it is similar to the case where the historical idle times are arranged in order from small to large, and will not be repeated here.
[0087] S203: Determine a target idle time duration according to the target historical idle time duration.
[0088] In one embodiment of the present invention, the client directly determines the target historical idle duration as the target idle duration.
[0089] In another embodiment of the present invention, the client determines the target idle time according to the historical idle time of the target and a preset tolerance coefficient.
[0090] Among them, the above-mentioned preset tolerance coefficient can be specifically set according to actual needs. Specifically, the preset tolerance coefficient can be a preset tolerance multiple. When the preset tolerance multiple takes a value less than 1, the calculated target idle time will become smaller, making it more likely that the current idle time will be greater than the target idle time, which can ensure that the client can promptly discover that the long connection has been disconnected. When the preset tolerance multiple takes a value greater than 1, the calculated target idle time will become larger, making it less likely that the current idle time will be greater than the target idle time, which can reduce the probability of the client mistakenly disconnecting the long connection. The preset tolerance coefficient can also be a weighted coefficient.
[0091] In one embodiment of the present invention, when the preset tolerance coefficient is a preset tolerance multiple, the client can determine the target idle time according to formula (1).
[0092] I = Ip * L (1)
[0093] Among them, I is the target idle time, Ip is the target historical idle time, and L is the preset tolerance multiple.
[0094] In one embodiment of the present invention, when the preset tolerance coefficient is a weighted coefficient, the client can determine the target idle time according to formula (2).
[0095] I = Ip * L + b (2)
[0096] Among them, I is the target idle time, Ip is the target historical idle time, L is the weighting coefficient, and b is a constant.
[0097] It can be seen from the above embodiments that in the embodiments of the present invention, the target idle time can be dynamically adjusted according to the preset tolerance coefficient. Therefore, the technical solution provided by the embodiments of the present invention can be applied to scenarios with various message transmission frequencies, and can meet the needs of long connection keep-alive in various scenarios.
[0098] S204, detecting whether the current idle time is greater than the target idle time.
[0099] In an embodiment of the present invention, the client detects whether the current idle time is greater than the target idle time. If the current idle time is greater than the target idle time, step S205 is executed. If the current idle time is not greater than the target idle time, that is, the client detects that the connection state of the long connection is normal, the long connection is maintained.
[0100] S205, disconnect the persistent connection and re-establish the persistent connection.
[0101] In the embodiment of the present invention, when the client detects that the current idle time is greater than the target idle time, that is, the client does not receive any message transmitted by the persistent connection within the set time period, the persistent connection may be considered as a dead connection and disconnected.
[0102] In one embodiment of the present invention, the applications at both ends of the long connection can disconnect the long connection through a process of waving four times. Specifically, the application on the client sends a FIN (close connection) message to the application on the server to close the connection between the client and the server, and the application on the server sends an ACK (acknowledgement) message to the client application. The application on the server closes the connection with the application on the client and sends a FIN message to the application on the client. The application on the client returns an ACK message to the application on the server.
[0103] In order to ensure that the applications at both ends of the long connection can communicate normally, in one embodiment of the present invention, after disconnecting the long connection, the client re-establishes the long connection with the application on the server. Specifically, the applications at both ends of the long connection can re-establish the long connection through a three-way handshake process. When establishing a connection, the client sends a SYN (synchronization) message to the server and waits for confirmation from the server; after receiving the SYN message, the application on the server sends an ACK message to the client and a SYN message at the same time; after receiving the SYN+ACK message from the server, the client sends an ACK message to the server. After this message is sent, the client and the server complete the three-way handshake, re-establish the long connection, and restart data transmission.
[0104] In the technical solution provided by the embodiment of the present invention, whether the long connection is to be disconnected is determined by detecting whether the current idle time of the long connection is greater than the target idle time. Since the current idle time can characterize the frequency of the current message transmission of the long connection, and the target idle time is determined from the first number of historical idle times of the long connection, that is, the target idle time can characterize the frequency of the long connection transmission of historical information, that is, in the embodiment of the present invention, the application at one end of the long connection compares the frequency of the current message transmission of the long connection with the frequency of the historical information transmission of the long connection to keep the long connection alive, and does not rely on the detection mechanism defined by the transport layer or the network layer's own protocol, and the specific implementation of the client at the application layer, so as to meet the needs of long connection keep-alive in various scenarios.
[0105] In another embodiment of the present invention, see Figure 4 , which is a flow chart of a second long connection keep-alive method provided in an embodiment of the present invention, the method includes steps S401-S406, wherein steps S401-S403, S405 are the same as the above steps S201-S203, S205, step S404 is a detailed step of the above step S204, and step S406 is a newly added step.
[0106] S404, detecting whether the current idle time is greater than the target idle time, and whether the first number is greater than a preset minimum number of messages.
[0107] The above-mentioned preset minimum number of messages can be understood as the preset minimum number of messages transmitted by the long connection, and its specific value can be set based on actual needs.
[0108] When the first number is too small, that is, there are too few samples in the historical idle duration, it is considered that the target historical idle duration determined from the first number of historical idle durations of the long connection is not representative. In order to determine a representative target historical idle duration, the embodiment of the present invention may execute step S404.
[0109] In one embodiment of the present invention, when the client determines that the current idle time is greater than the target idle time, and the first number is greater than the preset minimum number of messages, step S405 is executed to disconnect the long connection and re-establish the long connection. When the client determines that the current idle time is not greater than the target idle time, and / or the first number is not greater than the preset minimum number of messages, step S406 is executed to maintain the long connection.
[0110] S406, maintain a long connection.
[0111] In one embodiment of the present invention, when the client determines that the current idle time is greater than the target idle time and the first number is not greater than the preset minimum number of messages, step S406 is executed to maintain the long connection.
[0112] In one embodiment of the present invention, when the client determines that the current idle time is not greater than the target idle time and the first number is greater than the preset minimum number of messages, step S406 is executed to maintain the long connection.
[0113] In one embodiment of the present invention, when the client determines that the current idle time is not greater than the target idle time, and the first number is not greater than the preset minimum number of messages, step S406 is executed to maintain the long connection.
[0114] It can be seen from the above embodiments that in the embodiments of the present invention, on the basis of comparing the frequency of the current transmission message of the long connection with the frequency of the historical transmission information of the long connection to achieve long connection keep-alive, the dimension of the number of long connection transmission messages is added, so that a more representative target historical idle time can be determined, thereby improving the accuracy of long connection keep-alive.
[0115] In another embodiment of the present invention, see Figure 5 , is a flow chart of a third connection keep-alive method provided by an embodiment of the present invention. The method comprises steps S501-S507, steps S501-S504 are the same as steps S401-S404, steps S506-S507 are the same as steps S405-S406, and Figure 4 Compared with the provided connection keep-alive method, when the current idle time is not greater than the target idle time, and / or the first number is not greater than the preset minimum number of messages, before executing the step of maintaining the long connection, step S505 is also included.
[0116] S505: Detect whether the current idle time is greater than a preset maximum idle time.
[0117] The above preset maximum idle time can be set according to actual needs.
[0118] When the number of messages transmitted by the long connection is too small, that is, the number of historical idle times of the long connection is too small, the above-mentioned first number may not be greater than the preset minimum number of messages. In this way, the client will always believe that the connection status of the long connection is normal, resulting in the failure of the long connection keep-alive mechanism. In order to avoid this situation, the embodiment of the present invention executes step S505.
[0119] In one embodiment of the present invention, when the client detects that the current idle time is not greater than the target idle time, and / or the first number is not greater than the preset minimum number of messages, but the current idle time is greater than the preset maximum idle time, step S506 is executed to disconnect the long connection and re-establish the long connection.
[0120] In one embodiment of the present invention, when the client detects that the current idle time is not greater than the above-mentioned target idle time, and / or the first number is not greater than the preset minimum number of messages, and the current idle time is not greater than the preset maximum idle time, step S507 is executed to maintain a long connection.
[0121] It can be seen from the above embodiments that in the embodiments of the present invention, a fallback strategy is set, that is, when the current idle time is not greater than the target idle time, and / or the first number is not greater than the preset minimum number of messages, it is further detected whether the current idle time is greater than the preset maximum idle time, thereby avoiding the situation where the number of messages transmitted by the long connection is too small, resulting in the failure of the long connection keep-alive mechanism.
[0122] In another embodiment of the present invention, see Figure 6 , is a flow chart of the fourth connection keep-alive method provided in the embodiment of the present application. The method includes steps S601-S606, step S601 is the same as the above step S201, steps S603-S605 are the same as the above steps S203-S205, step S602 is a refinement of the above step S202, and step S606 is a newly added step, which is the same as the above step S406, and the description of the above step S406 can be referred to.
[0123] S602: Determine a target historical idle duration at a preset percentile value from a first number of historical idle durations of the long connection.
[0124] The percentile value mentioned above is a term in statistics. If a set of data is sorted from small to large and the corresponding cumulative percentile is calculated, the value of the data corresponding to a certain percentile is called the percentile value of this percentile, which can be expressed as: a set of n observations are arranged according to the numerical value. For example, the value at the P% position is called the Pth percentile value.
[0125] The above-mentioned preset percentile values can be set based on actual needs, such as the preset percentile values can be P1, P50, P95, P99, P99.9, P100, etc.
[0126] In one embodiment of the present invention, the client arranges the first number of historical idle durations in ascending order, and determines the historical idle duration at a preset percentile value as the target historical idle duration.
[0127] For example, assuming that the first number is 1000 and the preset percentile value is P1, the client will arrange the 1000 historical idle times in order from small to large, and determine the historical idle time at the first place as the target historical idle time, that is, the minimum value of the 1000 historical idle times is used as the target historical idle time. This can ensure that the frequency of all messages transmitted by the long connection is less than the target historical idle time. Then, the client obtains the target idle time based on the target historical idle time, and determines the survival status of the long connection by detecting the size of the current idle time and the target idle time. When the long connection is disconnected, it can be detected in time, thereby ensuring the timeliness of the long connection survival to the greatest extent.
[0128] For example, assuming that the first number is 1000 and the preset percentile value is P50, the client arranges the 1000 historical idle times in ascending order, and determines the historical idle time at the 500th place as the target historical idle time. That is, the median value of the 1000 historical idle times is used as the target idle time. This ensures that a balance is achieved between accuracy and timeliness during long connection keep-alive detection in long clients.
[0129] For another example, assuming that the first quantity is 1000 and the preset percentile value is P99.9, the client arranges the 1000 historical idle times in ascending order, and determines the historical idle time at the 999th position as the target historical idle time. That is, among the 1000 historical idle times, 999 idle times are no greater than the target historical idle time. This ensures that the frequency of most messages transmitted by the long connection is within the target historical idle time. Then, the client obtains the target idle time based on the target historical idle time, and determines the survival status of the long connection by detecting the size of the current idle time and the target idle time, thereby further improving the accuracy of keeping the long connection alive.
[0130] For another example, assuming that the first number is 1000 and the preset percentile value is P100, the client arranges the 1000 historical idle times in ascending order, and determines the historical idle time at the 1000th place as the target historical idle time, that is, the maximum value of the 1000 historical idle times is used as the target historical idle time. This ensures that the frequency of all messages transmitted by the long connection is within the target historical idle time. Then, the client obtains the target idle time based on the target historical idle time, and determines the survival status of the long connection by detecting the size of the current idle time and the target idle time, thereby ensuring the accuracy of the long connection keep-alive to the greatest extent.
[0131] It can be seen from the above embodiments that in the embodiments of the present invention, based on the preset percentile value, a more representative target historical idle time is determined from the first number of historical idle time periods, so that the application at one end of the long connection can timely and accurately detect the survival status of the long connection, further improve the accuracy of the long connection keep-alive, and can meet the needs of long connection keep-alive in various scenarios.
[0132] In the following, the connection keep-alive method provided by the embodiment of the present invention is described by taking the video playback scenario as an example in combination with a specific application scenario, but it does not serve as a limitation.
[0133] In the video playback and recording system, the system server needs to send local cache update notifications to the video playback applications on each client (hereinafter referred to as clients) based on a long connection. However, some clients cannot update the local cache for a long time due to abnormal disconnection of the long connection, which causes functional abnormality. The client can effectively solve this technical problem by using the connection keep-alive method provided in the embodiment of the present invention. The client specifically performs the following steps:
[0134] Step 1: Get the current idle time of the long connection.
[0135] Step 2: determine a target historical idle duration from a first number of historical idle durations of the long connection.
[0136] In the embodiment of the present invention, the client may determine a target historical idle duration at a preset percentile value from a first number of historical idle durations of the long connection.
[0137] Step three: determine the target idle time according to the target historical idle time and the preset tolerance factor.
[0138] Step 4: Detect whether the current idle time is greater than the target idle time, and whether the first number is greater than a preset minimum number of messages.
[0139] In the embodiment of the present invention, if the current idle time is not greater than the target idle time, and / or the first number is not greater than the preset minimum number of messages, the client executes step 5. If the current idle time is greater than the target idle time, and the first number is greater than the preset minimum number of messages, the client executes step 6.
[0140] Step 5: Check whether the current idle time is greater than the preset maximum idle time.
[0141] In the embodiment of the present invention, when the current idle time is greater than the preset maximum idle time, the client executes step 6. When the current idle time is not greater than the preset maximum idle time, the client executes step 7.
[0142] Step 6: Disconnect the long connection.
[0143] In the embodiment of the present invention, after disconnecting the persistent connection, in order to ensure that the applications at both ends of the persistent connection can communicate normally, the client may execute step eight.
[0144] Step 7: Maintain a long connection.
[0145] Step 8: Re-establish the persistent connection.
[0146] Since the above steps are basically similar to the above method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments.
[0147] In the technical solution provided by the embodiment of the present invention, whether the long connection is to be disconnected is determined by detecting whether the current idle time of the long connection is greater than the target idle time. Since the current idle time can represent the frequency of the long connection currently transmitting messages, and the target idle time is determined from the first number of historical idle times of the long connection, that is, the target idle time can represent the frequency of the long connection transmitting historical information, that is, in the embodiment of the present invention, the application at one end of the long connection compares the frequency of the long connection currently transmitting messages with the frequency of the long connection transmitting historical information to keep the long connection alive, and does not rely on the detection mechanism defined by the transport layer or network layer protocol itself, and the specific implementation of the client at the application layer.
[0148] In addition, in the embodiment of the present invention, the target idle time can be dynamically adjusted according to the preset tolerance coefficient. Therefore, the technical solution provided by the embodiment of the present invention can be applied to scenarios with various message transmission frequencies and can meet the needs of long connection keep-alive in various scenarios.
[0149] See also Figure 7 , is a schematic diagram of the structure of a connection keep-alive device provided in an embodiment of the present invention, the device is applied to an electronic device, the electronic device is deployed with an application located at one end of a long connection, the device includes:
[0150] The current idle duration acquisition module 701 is used to acquire the current idle duration of the long connection, where the current idle duration is the time interval between the last time the long connection transmitted a message and the current time.
[0151] The target historical idle duration determining module 702 is used to determine the target historical idle duration from the first number of historical idle durations of the long connection, wherein the historical idle duration is: the duration interval between two adjacent messages transmitted by the long connection;
[0152] A target idle duration determination module 703 is used to determine a target idle duration according to the target historical idle duration;
[0153] A detection module 704 is used to detect whether the current idle time is greater than the target idle time;
[0154] The long connection disconnection module 705 is used to disconnect the long connection and re-establish the long connection when the current idle time is longer than the target idle time.
[0155] In the technical solution provided by the embodiment of the present invention, whether the long connection is to be disconnected is determined by detecting whether the current idle time of the long connection is greater than the target idle time. Since the current idle time can characterize the frequency of the current message transmission of the long connection, and the target idle time is determined from the first number of historical idle times of the long connection, that is, the target idle time can characterize the frequency of the long connection transmission of historical information, that is, in the embodiment of the present invention, the application at one end of the long connection compares the frequency of the current message transmission of the long connection with the frequency of the historical information transmission of the long connection to keep the long connection alive, and does not rely on the detection mechanism defined by the transport layer or the network layer's own protocol, and the specific implementation of the client at the application layer, so as to meet the needs of long connection keep-alive in various scenarios.
[0156] In one embodiment of the present invention, the detection module is specifically used to:
[0157] Detecting whether the current idle time is greater than the target idle time, and whether the first number is greater than a preset minimum number of messages;
[0158] When the current idle time is greater than the target idle time, and the first number is greater than a preset minimum number of messages, calling the persistent connection disconnection module to execute the steps of disconnecting the persistent connection and re-establishing the persistent connection;
[0159] When the current idle time duration is not greater than the target idle time duration, and / or the first number is not greater than a preset minimum number of messages, a step of maintaining the long connection is performed.
[0160] It can be seen from the above embodiments that in the embodiments of the present invention, on the basis of comparing the frequency of the current transmission message of the long connection with the frequency of the historical transmission information of the long connection to achieve long connection keep-alive, the dimension of the number of long connection transmission messages is added, so that a more representative target historical idle time can be determined, thereby improving the accuracy of long connection keep-alive.
[0161] In one embodiment of the present invention, the detection module is further used to detect whether the current idle time is greater than a preset maximum idle time before executing the step of maintaining the long connection when the current idle time is not greater than the target idle time and / or the first number is not greater than a preset minimum number of messages;
[0162] When the current idle time is longer than the preset maximum idle time, calling the persistent connection disconnection module to execute the steps of disconnecting the persistent connection and re-establishing the persistent connection;
[0163] When the current idle time is not greater than the preset maximum idle time, the step of maintaining the long connection is performed.
[0164] It can be seen from the above embodiments that in the embodiments of the present invention, a fallback strategy is set, that is, when the current idle time is not greater than the target idle time, and / or the first number is not greater than the preset minimum number of messages, it is further detected whether the current idle time is greater than the preset maximum idle time, thereby avoiding the situation where the number of messages transmitted by the long connection is too small, resulting in the failure of the long connection keep-alive mechanism.
[0165] In one embodiment of the present invention, the target idle duration determination module is specifically configured to determine the target idle duration according to the target historical idle duration and a preset tolerance coefficient.
[0166] It can be seen from the above embodiments that in the embodiments of the present invention, the target idle time can be dynamically adjusted according to the preset tolerance coefficient. Therefore, the technical solution provided by the embodiments of the present invention can be applied to scenarios with various message transmission frequencies, and can meet the needs of long connection keep-alive in various scenarios.
[0167] In one embodiment of the present invention, when the historical idle durations are arranged in order of size, a ratio of the order of the target historical idle durations to the first number is a preset ratio.
[0168] In one embodiment of the present invention, the target historical idle duration determination module is specifically used to:
[0169] A target historical idle duration at a preset percentile value is determined from the first number of historical idle durations of the long connection.
[0170] It can be seen from the above embodiments that in the embodiments of the present invention, based on the preset percentile value, a more representative target historical idle time is determined from the first number of historical idle time periods, so that the application at one end of the long connection can timely and accurately detect the survival status of the long connection, further improving the accuracy of keeping the long connection alive.
[0171] The embodiment of the present invention further provides an electronic device, such as Figure 8 As shown, it includes a processor 801, a communication interface 802, a memory 803 and a communication bus 804, wherein the processor 801, the communication interface 802, and the memory 803 communicate with each other through the communication bus 804.
[0172] Memory 803, used for storing computer programs;
[0173] The processor 801 is configured to implement the long connection keep-alive method described in any one of the above embodiments when executing the program stored in the memory 803.
[0174] The communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0175] The communication interface is used for communication between the above terminal and other devices.
[0176] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0177] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0178] In another embodiment of the present invention, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is executed by a processor, the long connection keep-alive method described in any one of the above embodiments is implemented.
[0179] In another embodiment of the present invention, a computer program product including instructions is provided. When the computer program product is executed on a computer, the computer executes the long connection keep-alive method described in any one of the above embodiments.
[0180] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.
[0181] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0182] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0183] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. A connection keep-alive method, characterized in that: Applied to an electronic device, the electronic device is deployed with an application located at one end of a long connection, the method includes: Obtain the current idle time of the long connection, where the current idle time is the time interval between the last time the long connection transmitted a message and the current time; Determine a target historical idle duration from a first number of historical idle durations of the long connection, where the historical idle duration is: a duration interval between two adjacent messages transmitted by the long connection; Determine a target idle time according to the target historical idle time; Detecting whether the current idle time is greater than the target idle time; If so, disconnect the persistent connection and re-establish the persistent connection.
2. The method according to claim 1, characterized in that The detecting whether the current idle time is greater than the target idle time includes: Detecting whether the current idle time is greater than the target idle time, and whether the first number is greater than a preset minimum number of messages; If the current idle time is greater than the target idle time, and the first number is greater than the preset minimum number of messages, the steps of disconnecting the long connection and re-establishing the long connection are performed; If the current idle time duration is not greater than the target idle time duration, and / or the first number is not greater than a preset minimum number of messages, the step of maintaining the long connection is performed.
3. The method according to claim 2, characterized in that If the current idle duration is not greater than the target idle duration, and / or the first number is not greater than a preset minimum number of messages, before executing the step of maintaining the long connection, the method further includes: Detecting whether the current idle time is greater than a preset maximum idle time; If yes, executing the steps of disconnecting the persistent connection and re-establishing the persistent connection; If not, the step of maintaining the long connection is performed.
4. The method according to any one of claims 1 to 3, characterized in that: The determining the target idle duration according to the target historical idle duration includes: The target idle time is determined according to the target historical idle time and a preset tolerance coefficient.
5. The method according to any one of claims 1 to 3, characterized in that: When the historical idle durations are arranged in order of size, a ratio of the order of the target historical idle durations to the first number is a preset ratio.
6. The method according to claim 5, characterized in that The determining a target historical idle duration from the first number of historical idle durations of the long connection includes: A target historical idle duration at a preset percentile value is determined from the first number of historical idle durations of the long connection.
7. A connection keep-alive device, characterized in that: Applied to an electronic device, the electronic device is deployed with an application located at one end of a long connection, and the device includes: A current idle duration acquisition module is used to acquire the current idle duration of the long connection, where the current idle duration is the time interval between the last time the long connection transmitted a message and the current time; A target historical idle duration determination module, configured to determine a target historical idle duration from a first number of historical idle durations of the long connection, wherein the historical idle duration is: a duration interval between two adjacent messages transmitted by the long connection; A target idle duration determination module, used to determine a target idle duration according to the target historical idle duration; A detection module, used to detect whether the current idle time is greater than the target idle time; The long connection disconnection module is used to disconnect the long connection and re-establish the long connection when the current idle time is longer than the target idle time.
8. The device according to claim 7, characterized in that The detection module is specifically used for: Detecting whether the current idle time is greater than the target idle time, and whether the first number is greater than a preset minimum number of messages; When the current idle time is greater than the target idle time, and the first number is greater than a preset minimum number of messages, calling the persistent connection disconnection module to execute the steps of disconnecting the persistent connection and re-establishing the persistent connection; When the current idle time duration is not greater than the target idle time duration, and / or the first number is not greater than a preset minimum number of messages, a step of maintaining the long connection is performed.
9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, for implementing the method steps described in any one of claims 1 to 6 when executing a program stored in a memory.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Control method, device and equipment for communication equipment
CN116170900A
Connection switching method, server shutdown method, terminal equipment and server
CN116708533A
Optimization method for obtaining data in different time periods and terminal
CN117675604A
Connection control method and device
US20210258386A1
