A connection keep-alive method and apparatus

By comparing the current idle duration with the target idle duration of the long connection at the application layer, and combining historical idle duration with a preset tolerance coefficient, the problem of long connection disconnection cannot be detected in time is solved, and long connection keep-alive is achieved in various scenarios.

CN119155339BActive Publication Date: 2025-11-21BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411275896.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-12
Publication Date
2025-11-21
Estimated Expiration
2044-09-12

AI Technical Summary

Technical Problem

Existing technologies cannot meet the requirements for long-lived connections in certain scenarios, especially the problem that long-lived connections cannot be detected and disconnected in a timely manner due to the long detection time defined by the transport layer or network layer protocol and client incompatibility.

Method used

By detecting whether the current idle duration of a long-lived connection is greater than the target idle duration at the application layer, and combining historical idle durations with a preset tolerance coefficient, the target idle duration is dynamically adjusted to achieve long-lived connection persistence.

Benefits of technology

It enables timely discovery and reconstruction of long connections in various scenarios, avoids reliance on the detection mechanisms defined by transport layer or network layer protocols, and improves the accuracy and flexibility of long connection keep-alive.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119155339B_ABST
    Figure CN119155339B_ABST
Patent Text Reader

Abstract

The embodiment of the application provides a kind of connection keep-alive method and device, it is related to communication technical field, the method is applied to electronic equipment, and electronic equipment is deployed with the application located in long connection one end, the method comprises: obtaining the current idle duration of long connection, current idle duration is: the time duration interval of the time of long connection last transmission message with current time;Determine target historical idle duration from the first number of historical idle duration of long connection, wherein, historical idle duration is: the time duration interval of long connection transmission adjacent two messages;According to target historical idle duration, determine target idle duration;Whether current idle duration is greater than target idle duration is detected;If yes, then disconnect long connection and reestablish long connection.The technical scheme provided by the embodiment of the application can meet the needs of long connection keep-alive in various scenarios.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of communication technology, and in particular to a connection keep-alive method and apparatus. Background Technology

[0002] A persistent connection refers to a connection maintained between a client and a server in network communication. This allows both parties to reuse the connection for communication over a relatively long period without establishing a new connection for each request. Keeping a persistent connection alive ensures that it can be detected promptly if it has been broken. Depending on the protocol and application scenario, common persistent connection keep-alive methods include probe and idle disconnect. Probe involves one end of the persistent connection sending data to the other end according to transport layer or network layer protocols, then determining whether a response is received. If a response is received, the connection is considered usable and maintained; otherwise, the connection is considered unusable and closed. Idle disconnect, on the other hand, refers to the persistent connection being actively closed by both ends after the duration of no data exchange reaches a certain threshold.

[0003] In practical application layer development, the management of underlying long-lived connections when accessing network resources is typically implemented through specific clients. In this case, application layer developers often rely on the detection mechanisms defined by the transport or network layer protocols themselves, as well as the specific implementation of the client at the application layer, to achieve long-lived connection persistence. However, due to the long detection times of the transport or network layer protocols and the incompatibility of clients at the application layer, it is difficult to cover all application scenarios, making it impossible to meet the long-lived connection persistence requirements in certain situations. Summary of the Invention

[0004] The purpose of this invention is to provide a connection keep-alive method and apparatus, proposing a detection mechanism that does not rely on the protocol definition of the transport layer or network layer itself, nor on the specific implementation of the client at the application layer. Instead, the uppermost application layer decides whether to actively disconnect and reconnect, thus achieving a blocking long-connection keep-alive mechanism that can promptly detect when a connection has been broken, 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 invention, a connection keep-alive method is provided, applied to an electronic device having an application deployed at one end of a long connection, the method comprising:

[0006] Obtain the current idle duration of the long connection, where the current idle duration is the time interval between the time of the last message transmission of the long connection and the current time.

[0007] The target historical idle time is determined from the first number of historical idle times of the long connection, wherein the historical idle time is the time interval between two adjacent messages transmitted by the long connection;

[0008] The target idle time is determined based on the target's historical idle time.

[0009] Detect whether the current idle time is greater than the target idle time;

[0010] If so, the long connection is disconnected and re-established.

[0011] In one embodiment of the present invention, detecting whether the current idle time is greater than the target idle time includes:

[0012] Detect whether the current idle time is greater than the target idle time, and whether the first number is greater than the 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, then the step of disconnecting the long connection and re-establishing the long connection is executed.

[0014] 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, then the step of maintaining the long connection is executed.

[0015] In one 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 a preset minimum message number, then before performing the step of maintaining the long connection, the method further includes:

[0016] Detect whether the current idle time is greater than the preset maximum idle time;

[0017] If so, then perform the steps of disconnecting the long connection and re-establishing the long connection;

[0018] If not, then perform the step of maintaining the long connection.

[0019] In one embodiment of the present invention, determining the target idle time based on the target historical idle time includes:

[0020] The target idle time is determined based on the target's 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, the order of the target historical idle durations and the ratio of the first quantity are a preset ratio.

[0022] In one embodiment of the present invention, determining the target historical idle duration from a first number of historical idle durations of the long connection includes:

[0023] The target historical idle time located at a preset percentile value is determined from the first number of historical idle times of the long connection.

[0024] In a second aspect of the invention, a connection keep-alive device is also provided, applied to an electronic device having an application deployed at one end of a long connection, the device comprising:

[0025] The current idle duration acquisition module is used to acquire the current idle duration of the long connection, wherein the current idle duration is the time interval between the time of the last message transmission of the long connection and the current time;

[0026] The target historical idle duration determination module 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;

[0027] The target idle duration determination module is used to determine the target idle duration based on the target's historical idle duration.

[0028] The detection module is 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 greater than the target idle time.

[0030] The long connection persistence 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 for:

[0032] Detect whether the current idle time is greater than the target idle time, and whether the first number is greater than the preset minimum number of messages;

[0033] When the current idle duration is greater than the target idle duration and the first number is greater than the preset minimum number of messages, the long connection disconnection module is invoked to perform the steps of disconnecting the long connection and re-establishing the long connection;

[0034] 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, the step of maintaining the long connection is executed.

[0035] In one embodiment of the present invention, the detection module is further configured to detect whether the current idle time is greater than a preset maximum idle time before performing 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;

[0036] When the current idle time exceeds the preset maximum idle time, the long connection disconnection module is invoked to perform the steps of disconnecting the long connection and re-establishing the long connection.

[0037] If the current idle time is not greater than the preset maximum idle time, the step of maintaining the long connection is executed.

[0038] In one embodiment of the present invention, when the historical idle durations are arranged in order of size, the order of the target historical idle durations and the ratio of the first quantity are a preset ratio.

[0039] In one embodiment of the present invention, the target idle time determination module is specifically used to determine the target idle time based on the target historical idle time and a preset tolerance coefficient.

[0040] In one embodiment of the present invention, the target historical idle time determination module is specifically used for:

[0041] The target historical idle time located at a preset percentile value is determined from the first number of historical idle times of the long connection.

[0042] In another aspect of the present invention, an electronic device is also provided, including 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.

[0043] Memory, used to store computer programs;

[0044] When the processor executes a program stored in memory, it implements any of the connection keep-alive methods described above.

[0045] In another aspect of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements any of the connection keep-alive methods described above.

[0046] In another aspect of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the connection keep-alive methods described above.

[0047] In the technical solution provided by this invention, whether a long-lived connection should be disconnected is determined by detecting whether the current idle duration of the long-lived connection is greater than the target idle duration. Since the current idle duration characterizes the frequency of current message transmissions by the long-lived connection, and the target idle duration is determined from a first number of historical idle durations of the long-lived connection (i.e., the target idle duration characterizes the frequency of historical information transmissions by the long-lived connection), this invention achieves long-lived connection keep-alive by comparing the frequency of current message transmissions with the frequency of historical information transmissions. This does not rely on the detection mechanisms defined by the transport or network layer protocols themselves, nor on the specific implementation by the client at the application layer, thus meeting the long-lived connection keep-alive requirements in various scenarios. Attached Figure Description

[0048] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.

[0049] Figure 1 This is a schematic diagram illustrating a method for blocking and monitoring long-lived connections, provided as an embodiment of the present invention.

[0050] Figure 2 A flowchart illustrating the first connection keep-alive method provided in an embodiment of the present invention;

[0051] Figure 3 This is a schematic diagram illustrating a message transmission based on a long connection, as provided in an embodiment of the present invention.

[0052] Figure 4 A flowchart illustrating the second connection keep-alive method provided in an embodiment of the present invention;

[0053] Figure 5 A flowchart illustrating the third connection keep-alive method provided in an embodiment of the present invention;

[0054] Figure 6 A flowchart illustrating the fourth connection keep-alive method provided in an embodiment of the present invention;

[0055] Figure 7 This is a schematic diagram of a connection and life-keeping device provided in an embodiment of the present invention;

[0056] Figure 8 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention. Detailed Implementation

[0057] The technical solutions of the present invention will now be described with reference to the accompanying drawings in the embodiments of the present invention.

[0058] A persistent connection refers to a connection maintained between a client and a server in network communication, allowing both parties to reuse the connection for communication over a relatively long period without establishing a new connection for each request. Keep-alive mechanisms for persistent connections ensure that a connection can be detected promptly if it has been broken. Depending on the protocol and application scenario, common methods for keeping persistent connections alive include probe-based methods and idle disconnection.

[0059] Probing refers to the process where one end of a long-lived connection sends data to the other end via the connection according to transport layer or network layer protocols, and then determines whether a response is received. If a response is received, the connection is considered usable and the long-lived connection is maintained; otherwise, the connection is deemed unusable and the long-lived connection is closed. For example, the Transmission Control Protocol (TCP) provides a KeepAlive message to probe whether the other end of the long-lived connection is alive. Similarly, the WebSocket protocol (a TCP-based network protocol) provides a heartbeat message to probe whether the other end of the long-lived connection is alive.

[0060] Idle disconnection refers to the deliberate closure of a long-lived connection by both ends after a threshold period of no data exchange has been reached. For example, the Hypertext Transfer Protocol (HTTP) provides a Keepalive-timeout parameter; if one end of the long-lived connection does not receive a request message from the other end within this set time, the connection is closed. Other examples include maximum idle time settings for various database connections.

[0061] In practical application layer development, just as people don't develop their own browsers to access a webpage, the management of underlying long-lived connections when accessing network resources is generally implemented through specific clients. For example, when accessing various databases and middleware, developers use existing and mature open-source clients. In this case, application layer developers often rely on the detection mechanisms defined by the transport layer or network layer protocols themselves, as well as the specific implementation of the client at the application layer, to achieve the goal of keeping long-lived connections alive.

[0062] However, the long probe times defined by the transport or network layer protocols can fail to meet the requirements for long-lived connections in certain scenarios. Taking TCP as an example, the TCP protocol provides a KeepAlive mechanism to achieve long-lived connections. When KeepAlive is enabled, the TCP layer sends a KeepAlive probe message after a set time to determine connection availability. The default time is 7200 seconds (two hours), and it retryes 10 times after a failure, with each timeout lasting 75 seconds. Clearly, this default value cannot meet the needs of mobile networks. Furthermore, the KeepAlive mechanism provided by the TCP protocol will also fail in complex situations such as router failure or unplugging the network cable.

[0063] Furthermore, incompatibility issues at the application layer can prevent the fulfillment of long-lived connection keep-alive requirements in certain scenarios. For example, common long-lived connection management clients, for security reasons, cannot write data to long-lived connections while they are being blocked and monitored. This means that long-lived connection management clients cannot perform heartbeat detection while blocking and monitoring long-lived connections. In monitoring scenarios, because blocking and listening for messages is required, the client needs to remain in a listening state and will not disconnect when idle. This can lead to long-lived connections being disconnected for a long time before being detected, failing to meet the requirements for long-lived connection protection.

[0064] To facilitate understanding, the above situation will be explained below using the Redis pub / sub (publish / subscribe based on a remote dictionary server) function as an example.

[0065] See Figure 1 , Figure 1This diagram illustrates a method for blocking and listening to long-lived connections, as provided in this invention. When using Redis's pub / sub functionality, subscribers need to block and listen to a long-lived connection to retrieve subscription messages published by the publisher. Jedis is the officially recommended Java client development package for Redis. As middleware, it integrates Redis command operations and provides connection pool management. Jedis allows connecting to and operating on Redis. In short, Jedis is a common connection management client. However, Jedis is based on thread-unsafe Socket connections. To avoid unpredictable situations, data cannot be written to the Socket connection while blocking and listening. This means Jedis cannot perform heartbeat detection while blocking and listening to the Socket connection. In a listening scenario, because the client needs to block and wait for messages, it must continuously listen and will not disconnect when idle. Therefore, when the connection is abnormally disconnected, the fact that the connection has been broken will only be discovered if the client catches it (which often doesn't happen, such as due to network interruption) or relies on a TCP protocol detection mechanism (which requires hours by default). This introduces significant uncertainty into application functionality and cannot meet the requirements for keeping long-lived connections alive.

[0066] To address the aforementioned problems, embodiments of the present invention provide a connection keeping-alive method and apparatus.

[0067] See Figure 2 This is a flowchart illustrating the first connection keep-alive method provided in this embodiment of the invention. The method is applied to an electronic device that deploys an application at one end of a long connection. Specifically, the electronic device can be a client, a server, etc. The application at one end of the long connection can be an application on a server at one end of the long connection, or an application on a client at the other end of the long connection. For ease of explanation, the following description uses an application on a client (hereinafter referred to as the client) as the execution subject, and does not constitute a limitation. The above method includes steps S201-S205.

[0068] S201, Get the current idle duration 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 this embodiment of the invention, the client can record the current timestamp and the timestamp of the most recent message transmission of the long connection, and obtain the current idle time of the long connection based on these two timestamps. The timestamp of the most recent message transmission of the long connection can be either the timestamp of the client's most recent message sending at one end of the long connection, or the timestamp of the server's most recent message receiving at one end of the long connection.

[0071] S202, determine the target historical idle duration from the first number of historical idle durations of the long connection.

[0072] The aforementioned historical idle duration refers to the time interval between two adjacent messages transmitted over a long connection. Assuming the long connection transmitted N messages, the client can determine N-1 historical idle durations.

[0073] For ease of understanding, combined with Figure 3 The process of determining the current idle time and the historical idle time in the embodiments of the present invention will be described.

[0074] See Figure 3 This is a schematic diagram of a message transmission based on a long connection, provided by an embodiment of the present invention. Figure 3 The timing of client message sending is used as the basis for determining the current idle time and historical idle time, but it 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 after message m1. When the client sends message m2, the time is t2. Then the client can determine the historical idle time i1 = t2 - t1. i1 represents the idle time of the long connection after the client sends message m1.

[0076] Assuming a long connection has transmitted n messages (i.e., the client sent mn messages), there will be i1, i2, ..., in-1 definite historical idle times. If the current time is t, the client can determine the current idle time as i = t - tn.

[0077] The aforementioned first quantity can be set according to actual needs. The minimum value of the first quantity is 1, and the maximum value is the number of messages transmitted by the long connection minus 1. In scenarios with high real-time requirements, i.e., when it is necessary to detect the connection status of long connections in a timely manner, the larger the value of the first quantity, the better. In scenarios with moderate real-time requirements and limited computing resources, the value of the first quantity can be smaller.

[0078] In one embodiment of the present invention, when the first quantity is the maximum value, the client will use all the determined historical idle durations as the first quantity of historical idle durations.

[0079] In another embodiment of the present invention, when the value of the first quantity is not the maximum value, the client can divide all historical idle time into multiple sets based on the duration, and extract a certain number of historical idle time from each of these multiple sets to obtain the first number of historical idle time.

[0080] For example, if a long connection transmits 100 messages, the client can identify 99 historical idle durations. The first count is set to 10. Based on the duration, the client can divide these 99 historical idle durations into three sets: those between 1s and 5s are placed in set 1; those between 5s and 10s are placed in set 2; and those in other ranges are placed in set 3. Then, 3 historical idle durations are randomly selected from set 1, 3 from set 2, and 4 from set 3, resulting in 10 (the first count) historical idle durations.

[0081] Alternatively, the client can select the first number of historical idle times that have been recently obtained.

[0082] In one embodiment of the present invention, the client can select the largest historical idle time from a first number of historical idle times as the target historical idle time. This reduces the probability of the client accidentally disconnecting a long-lived connection.

[0083] In one embodiment of the present invention, the client can select the smallest historical idle time from a first set of historical idle times as the target historical idle time. This ensures that the client can promptly detect when a long-lived connection has been broken.

[0084] In one embodiment of the present invention, the client can select the median historical idle time from a first number of historical idle times as the target historical idle time. This allows the client to achieve a balance between reducing the probability of accidentally disconnecting long-lived connections and promptly detecting when a long-lived connection has been disconnected.

[0085] In one embodiment of the present invention, the client may also calculate the average of a first number of historical idle durations and use the average as the target historical idle duration.

[0086] To determine a representative target historical idle time from a first number of historical idle times, in one embodiment of the present invention, when the historical idle times are arranged in ascending order, the ratio of the order of the target historical idle times to the first number is a preset ratio. The historical idle times can be arranged in ascending order or descending order. When the historical idle times are arranged in ascending order, a preset ratio of 0.6 indicates that the ratio of the order of the target historical idle times to the first number is 0.6. A preset ratio of the minimum value (1 / first number) indicates that the target historical idle time is the smallest historical idle time among the first number of historical idle times. A preset ratio of the maximum value (1) indicates that the target historical idle time is the largest historical idle time among the first number of historical idle times. The situation is similar when the historical idle times are arranged in descending order, and will not be elaborated further here.

[0087] S203, determine the target idle time based on the target's historical idle time.

[0088] In one embodiment of the present invention, the client directly determines the target historical idle time as the target idle time.

[0089] In another embodiment of the present invention, the client determines the target idle time based on the target historical idle time and a preset tolerance coefficient.

[0090] The preset tolerance coefficient can be set according to actual needs. Specifically, the preset tolerance coefficient can be a preset tolerance multiple. When the preset tolerance multiple is less than 1, the calculated target idle time will be smaller, making it more likely that the current idle time will exceed the target idle time, ensuring that the client can promptly detect that the long connection has been broken. When the preset tolerance multiple is greater than 1, the calculated target idle time will be larger, making it less likely that the current idle time will exceed the target idle time, reducing 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] Where 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] Where I is the target idle time, Ip is the target historical idle time, L is the weighting coefficient, and b is a constant.

[0097] As can be seen from the above embodiments, 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 in various scenarios with different message transmission frequencies, and can meet the long connection keep-alive requirements in various scenarios.

[0098] S204, Detect whether the current idle time is greater than the target idle time.

[0099] In this embodiment of the 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 status of the long connection is normal, and the long connection is maintained.

[0100] S205, Disconnect the long connection and re-establish the long connection.

[0101] In this embodiment of the invention, when the client detects that the current idle time is longer than the target idle time, that is, the client has not received any messages transmitted through the long connection within the set time period, the long connection can be considered dead and the long connection can be disconnected.

[0102] In one embodiment of the present invention, the two ends of a long connection can disconnect the long connection through a four-way handshake process. Specifically, the application on the client side sends a FIN (close connection) packet to the application on the server side to close the connection between the client and the server. The application on the server side sends an ACK (acknowledgment) packet to the application on the client side. The application on the server side closes the connection with the application on the client side and sends a FIN packet to the application on the client side. The application on the client side then returns an ACK packet to the application on the server side.

[0103] To ensure normal communication between applications at both ends of a long connection, in one embodiment of the present invention, after the long connection is closed, 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. During connection establishment, the client sends a SYN (synchronization) packet to the server and waits for server confirmation; upon receiving the SYN packet, the application on the server sends an ACK packet to the client, and simultaneously sends a SYN packet; after receiving the SYN+ACK packet from the server, the client sends an ACK packet to the server. After this packet transmission is complete, the client and server complete the three-way handshake, re-establish the long connection, and resume data transmission.

[0104] In the technical solution provided by this invention, whether a long-lived connection should be disconnected is determined by detecting whether the current idle duration of the long-lived connection is greater than the target idle duration. Since the current idle duration characterizes the frequency of current message transmissions by the long-lived connection, and the target idle duration is determined from a first number of historical idle durations of the long-lived connection (i.e., the target idle duration characterizes the frequency of historical information transmissions by the long-lived connection), in this embodiment, the application at one end of the long-lived connection maintains the connection by comparing the frequency of current message transmissions with the frequency of historical information transmissions. This does not rely on the detection mechanisms defined by the transport or network layer protocols themselves, nor on the specific implementation of the client at the application layer, thus meeting the long-lived connection maintenance requirements in various scenarios.

[0105] In another embodiment of the present invention, see Figure 4 The above is a flowchart illustrating the second long connection keep-alive method provided in the embodiment of the present invention. The method includes steps S401-S406, wherein steps S401-S403 and S405 are the same as steps S201-S203 and S205, step S404 is a refinement of step S204, and step S406 is a newly added step.

[0106] S404, Detect whether the current idle time is greater than the target idle time, and whether the first quantity is greater than the preset minimum number of messages.

[0107] The aforementioned minimum message count can be understood as the minimum number of messages that can be transmitted over a long connection. Its specific value can be set based on actual needs.

[0108] When the first quantity is too small, that is, when there are too few samples in the historical idle durations, the target historical idle duration determined from the first quantity of historical idle durations in the long connection is considered not representative. In order to determine a representative target historical idle duration, this embodiment of the 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 quantity is greater than the preset minimum message quantity, 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 quantity is not greater than the preset minimum message quantity, step S406 is executed to maintain the long connection.

[0110] S406, maintain a persistent 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, then 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 quantity is greater than the preset minimum message quantity, 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, then step S406 is executed to maintain the long connection.

[0114] As can be seen from the above embodiments, in this embodiment of the invention, in addition to achieving long connection keep-alive by comparing the frequency of current long connection message transmission with the frequency of long connection historical information transmission, the dimension of the number of long connection message transmissions is added, thereby enabling the determination of a more representative target historical idle time and thus improving the accuracy of long connection keep-alive.

[0115] In another embodiment of the present invention, see Figure 5 This is a flowchart illustrating the third connection keep-alive method provided in an embodiment of the present invention. The method includes steps S501-S507, where steps S501-S504 are the same as steps S401-S404, and steps S506-S507 are the same as steps S405-S406. Figure 4 Compared to the provided connection keep-alive method, before performing the step of maintaining a long connection 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, step S505 is also included.

[0116] S505, detects whether the current idle time is greater than the preset maximum idle time.

[0117] The preset maximum idle time can be set according to actual needs.

[0118] When the number of messages transmitted by a long connection is too small, that is, the number of historical idle times of the long connection is too small, it may cause the first number mentioned above to never be greater than the preset minimum number of messages. In this case, the client will always think that the connection status of the long connection is normal, causing the long connection keep-alive mechanism to fail. 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, then 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 target idle time, and / or the first quantity is not greater than the preset minimum message quantity, and the current idle time is not greater than the preset maximum idle time, then step S507 is executed to maintain the long connection.

[0121] As can be seen from the above embodiments, in this embodiment of the invention, by setting a fallback strategy, 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, causing the long connection keep-alive mechanism to fail.

[0122] In another embodiment of the present invention, see Figure 6 This is a flowchart illustrating the fourth connection keep-alive method provided in this application embodiment. The method includes steps S601-S606. Step S601 is the same as step S201 above, steps S603-S605 are the same as steps S203-S205 above, step S602 is a refinement of step S202 above, and step S606 is a newly added step, the same as step S406 above, which can be referred to in the description of step S406 above.

[0123] S602, determine the target historical idle time located at a preset percentile value from the first number of historical idle times of the long connection.

[0124] The percentile value mentioned above is a statistical term. If a set of data is sorted from smallest to largest and the corresponding cumulative percentiles are calculated, the value of the data corresponding to a certain percentile is called the percentile value of that percentile. It can be expressed as: a set of n observations arranged in numerical order. For example, the value at the P% position is called the Pth percentile value.

[0125] The preset percentile values ​​mentioned above can be set based on actual needs. For example, 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 a first number of historical idle durations in ascending order, and determines the historical idle duration located at a preset percentile value as the target historical idle duration.

[0127] For example, assuming the first quantity is 1000 and the preset percentile value is P1, the client arranges these 1000 historical idle durations in ascending order and determines the historical idle duration at the first position as the target historical idle duration. That is, the minimum value among these 1000 historical idle durations is taken as the target historical idle duration. This ensures that the frequency of all messages transmitted by the long connection is less than the target historical idle duration. Then, the client obtains the target idle duration based on the target historical idle duration. By detecting the difference between the current idle duration and the target idle duration, the client determines the liveness status of the long connection. When the long connection is disconnected, it can be detected in time, maximizing the timeliness of keeping the long connection alive.

[0128] For example, assuming the first quantity is 1000 and the preset percentile value is P50, the client will arrange these 1000 historical idle durations in ascending order and determine the historical idle duration at the 500th position as the target historical idle duration. That is, the median value among these 1000 historical idle durations is used as the target idle duration. This can ensure that the long client achieves a balance between accuracy and timeliness in the long connection keep-alive detection process.

[0129] For example, assuming the first quantity is 1000 and the preset percentile value is P99.9, the client will arrange these 1000 historical idle durations in ascending order and determine the historical idle duration at the 999th position as the target historical idle duration. That is, among the 1000 historical idle durations, 999 idle durations are not greater than the target historical idle duration. This can ensure that the frequency of most messages transmitted by the long connection is within the target historical idle duration. Then, the client obtains the target idle duration based on the target historical idle duration and determines the liveness status of the long connection by detecting the difference between the current idle duration and the target idle duration, further improving the accuracy of long connection keep-alive.

[0130] For example, assuming the first quantity is 1000 and the preset percentile value is P100, the client arranges these 1000 historical idle durations in ascending order and determines the historical idle duration at the 1000th position as the target historical idle duration. That is, the maximum value among these 1000 historical idle durations is taken as the target historical idle duration. This ensures that the frequency of all messages transmitted by the long connection is within the target historical idle duration. Then, the client obtains the target idle duration based on the target historical idle duration. By detecting the difference between the current idle duration and the target idle duration, the client determines the liveness status of the long connection, maximizing the accuracy of long connection keep-alive.

[0131] As can be seen from the above embodiments, in the embodiments of the present invention, based on the preset percentile value, a more representative target historical idle time is determined from a first number of historical idle times, so that the application at one end of the long connection can detect the liveness status of the long connection in a timely and accurate manner, further improving the accuracy of long connection keep-alive and meeting the needs of long connection keep-alive in various scenarios.

[0132] The following description, using video playback as an example, illustrates the connection keep-alive method provided in this embodiment of the invention, without limiting its scope.

[0133] In a video playback recording system, the system server needs to send local cache update notifications to the video playback applications (hereinafter referred to as clients) on each client via persistent connections. However, some clients are unable to update their local cache for an extended period due to abnormal disconnection of the persistent connection, leading to functional malfunctions. The connection keep-alive method provided in this embodiment of the invention can effectively solve this technical problem. The client specifically performs the following steps:

[0134] Step 1: Obtain the current idle duration of the long connection.

[0135] Step 2: Determine the target historical idle duration from the first number of historical idle durations of the long connection.

[0136] In this embodiment of the invention, the client can determine the target historical idle time located at a preset percentile value from the first number of historical idle times of the long connection.

[0137] Step 3: Determine the target idle time based on the target's historical idle time and the preset tolerance coefficient.

[0138] Step 4: Check whether the current idle time is greater than the target idle time and whether the first number is greater than the preset minimum number of messages.

[0139] In this embodiment of the 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 five. 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 six.

[0140] Step 5: Check if the current idle time is greater than the preset maximum idle time.

[0141] In this embodiment of the invention, if the current idle time is greater than the preset maximum idle time, the client executes step six. If the current idle time is not greater than the preset maximum idle time, the client executes step seven.

[0142] Step 6: Disconnect the long connection.

[0143] In this embodiment of the invention, after the long connection is disconnected, in order to ensure that the applications at both ends of the long connection can communicate normally, the client can execute step eight.

[0144] Step 7: Maintain a persistent connection.

[0145] Step 8: Re-establish the long connection.

[0146] Since the above steps are basically similar to the above method embodiments, the description is relatively simple. For relevant details, please refer to the description of the method embodiments.

[0147] In the technical solution provided by this invention, whether a long-lived connection should be disconnected is determined by detecting whether the current idle duration of the long-lived connection is greater than the target idle duration. Since the current idle duration characterizes the frequency of current message transmissions by the long-lived connection, and the target idle duration is determined from a first number of historical idle durations of the long-lived connection (i.e., the target idle duration characterizes the frequency of historical information transmissions by the long-lived connection), in this embodiment, the application at one end of the long-lived connection maintains the connection by comparing the frequency of current message transmissions with the frequency of historical information transmissions. This does not rely on the detection mechanisms defined by the transport or network layer protocols themselves, nor on the specific implementation of the client at the application layer.

[0148] In addition, in this embodiment of the invention, the target idle time can be dynamically adjusted according to a preset tolerance coefficient. Therefore, the technical solution provided by this embodiment of the invention can be applied to various scenarios with different message transmission frequencies and can meet the long connection keep-alive requirements in various scenarios.

[0149] See Figure 7 This is a schematic diagram of a connection keep-alive device provided in an embodiment of the present invention. The device is applied to an electronic device, which has an application deployed 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, wherein the current idle duration is the time interval between the time of the last message transmission of the long connection and the current time.

[0151] The target historical idle duration determination 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] The target idle duration determination module 703 is used to determine the target idle duration based on the target's historical idle duration.

[0153] 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 greater than the target idle time.

[0155] In the technical solution provided by this invention, whether a long-lived connection should be disconnected is determined by detecting whether the current idle duration of the long-lived connection is greater than the target idle duration. Since the current idle duration characterizes the frequency of current message transmissions by the long-lived connection, and the target idle duration is determined from a first number of historical idle durations of the long-lived connection (i.e., the target idle duration characterizes the frequency of historical information transmissions by the long-lived connection), in this embodiment, the application at one end of the long-lived connection maintains the connection by comparing the frequency of current message transmissions with the frequency of historical information transmissions. This does not rely on the detection mechanisms defined by the transport or network layer protocols themselves, nor on the specific implementation of the client at the application layer, thus meeting the long-lived connection maintenance requirements in various scenarios.

[0156] In one embodiment of the present invention, the detection module is specifically used for:

[0157] Detect whether the current idle time is greater than the target idle time, and whether the first number is greater than the preset minimum number of messages;

[0158] When the current idle duration is greater than the target idle duration and the first number is greater than the preset minimum number of messages, the long connection disconnection module is invoked to perform the steps of disconnecting the long connection and re-establishing the long connection;

[0159] 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, the step of maintaining the long connection is executed.

[0160] As can be seen from the above embodiments, in this embodiment of the invention, in addition to achieving long connection keep-alive by comparing the frequency of current long connection message transmission with the frequency of long connection historical information transmission, the dimension of the number of long connection message transmissions is added, thereby enabling the determination of a more representative target historical idle time and thus improving the accuracy of long connection keep-alive.

[0161] In one embodiment of the present invention, the detection module is further configured to detect whether the current idle time is greater than a preset maximum idle time before performing 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 exceeds the preset maximum idle time, the long connection disconnection module is invoked to perform the steps of disconnecting the long connection and re-establishing the long connection.

[0163] If the current idle time is not greater than the preset maximum idle time, the step of maintaining the long connection is executed.

[0164] As can be seen from the above embodiments, in this embodiment of the invention, by setting a fallback strategy, 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, causing the long connection keep-alive mechanism to fail.

[0165] In one embodiment of the present invention, the target idle time determination module is specifically used to determine the target idle time based on the target historical idle time and a preset tolerance coefficient.

[0166] As can be seen from the above embodiments, 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 in various scenarios with different message transmission frequencies, and can meet the long connection keep-alive requirements in various scenarios.

[0167] In one embodiment of the present invention, when the historical idle durations are arranged in order of size, the order of the target historical idle durations and the ratio of the first quantity are a preset ratio.

[0168] In one embodiment of the present invention, the target historical idle time determination module is specifically used for:

[0169] The target historical idle time located at a preset percentile value is determined from the first number of historical idle times of the long connection.

[0170] As can be seen from the above embodiments, in the embodiments of the present invention, based on the preset percentile value, a more representative target historical idle time is determined from a first number of historical idle times, so that the application located at one end of the long connection can detect the liveness status of the long connection in a timely and accurate manner, thereby further improving the accuracy of keeping the long connection alive.

[0171] This invention also 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. The processor 801, communication interface 802, and memory 803 communicate with each other via the communication bus 804.

[0172] Memory 803 is used to store computer programs;

[0173] When the processor 801 executes the program stored in the memory 803, it implements any of the long connection keep-alive methods described in the above embodiments.

[0174] The communication bus mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.

[0175] The communication interface is used for communication between the aforementioned terminal and other devices.

[0176] The memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.

[0177] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.

[0178] In another embodiment of the present invention, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements any of the long connection keep-alive methods described in the above embodiments.

[0179] In another embodiment of the present invention, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the long connection keep-alive methods described in the above embodiments.

[0180] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially 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, all or part of the processes or functions described in the embodiments of the present invention are generated. 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. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. 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 integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid state disk (SSD)).

[0181] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0182] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the apparatus embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.

[0183] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention are included within the scope of protection of the present invention.

Claims

1. A connection keep-alive method, characterized in that, Applied to an electronic device having an application deployed at one end of a long connection, the method includes: Obtain the current idle duration of the long connection, where the current idle duration is the time interval between the time of the last message transmission of the long connection and the current time. The target historical idle time is determined from the first number of historical idle times of the long connection, wherein the historical idle time is the time interval between two adjacent messages transmitted by the long connection; The target idle time is determined based on the target's historical idle time. Detect whether the current idle time is greater than the target idle time; If so, the long connection is disconnected and re-established.

2. The method according to claim 1, characterized in that, The step of detecting whether the current idle time is greater than the target idle time includes: Detect whether the current idle time is greater than the target idle time, and whether the first number is greater than the 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, then the step of disconnecting the long connection and re-establishing the long connection is executed. 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, then the step of maintaining the long connection is executed.

3. The method according to claim 2, characterized in that, 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 message number, then before performing the step of maintaining the long connection, the method further includes: Detect whether the current idle time is greater than the preset maximum idle time; If so, then perform the steps of disconnecting the long connection and re-establishing the long connection; If not, then perform the step of maintaining the long connection.

4. The method according to any one of claims 1-3, characterized in that, The step of determining the target idle time based on the target's historical idle time includes: The target idle time is determined based on the target's historical idle time and a preset tolerance coefficient.

5. The method according to any one of claims 1-3, characterized in that, When the historical idle durations are arranged in order of magnitude, the order of the target historical idle durations and the ratio of the first quantity are a preset ratio.

6. The method according to claim 5, characterized in that, Determining the target historical idle duration from the first number of historical idle durations of the long connection includes: The target historical idle time located at a preset percentile value is determined from the first number of historical idle times of the long connection.

7. A connection and liveness-preserving device, characterized in that, Applied to an electronic device having an application deployed at one end of a long connection, the device includes: The current idle duration acquisition module is used to acquire the current idle duration of the long connection, wherein the current idle duration is the time interval between the time of the last message transmission of the long connection and the current time; The target historical idle duration determination module 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; The target idle duration determination module is used to determine the target idle duration based on the target's historical idle duration. The detection module is 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 greater than the target idle time.

8. The apparatus according to claim 7, characterized in that, The detection module is specifically used for: Detect whether the current idle time is greater than the target idle time, and whether the first number is greater than the preset minimum number of messages; When 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 long connection disconnection module is invoked to perform the steps of disconnecting the long connection and re-establishing 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 the preset minimum number of messages, the step of maintaining the long connection is executed.

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, when executing a program stored in memory, implements the steps of the method described in any one of claims 1-6.

10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the method described in any one of claims 1-6.

Citation Information

Patent Citations

  • Control method, device and equipment for communication equipment

    CN116170900A

  • Optimization method for obtaining data in different time periods and terminal

    CN117675604A