Communication processing method and device based on long connection, equipment and medium
By establishing a persistent connection between the server and the client and performing lock management, the problem of low communication efficiency under the HTTP request mode is solved, and efficient data transmission and resource utilization are achieved.
Patent Information
- Application Number
- CN202510914618.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-02
- Publication Date
- 2025-10-03
AI Technical Summary
In the existing technology, the HTTP question-and-answer request method requires a connection to be established for each request, resulting in significant delays during multiple rounds of continuous interaction, low communication efficiency and excessive resource usage.
A persistent connection is established between the server and the client, and persistent connection encapsulation information including the client identifier, persistent connection channel, and expiration time is generated. The target index lock is determined based on the expiration time, and a key-value pair of the persistent connection channel and encapsulation information is constructed and added to the channel mapping table and target array for lock management to ensure the correctness of data processing and the rational use of resources.
It improves the communication efficiency between the server and the client, ensures a one-to-one correspondence between long connections and clients, reduces resource usage, and improves data transmission efficiency and response speed.
Smart Images

Figure CN120750996A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a communication processing method, apparatus, device, and medium based on a persistent connection. Background Art
[0002] In actual business scenarios, intelligent materials can be generated in response to client requests, such as promotional copy for the target platform, decoration cases, etc.
[0003] In related technologies, business interactions are performed through a question-and-answer request method using HTTP (Hypertext Transfer Protocol). However, this method requires establishing a connection for each request, which results in an additional connection establishment delay. This delay is more pronounced during multiple rounds of continuous interaction, leading to technical issues such as time-consuming requests and low communication efficiency. Summary of the Invention
[0004] In order to solve the above technical problems or at least partially solve the above technical problems, the present disclosure provides a communication processing method, apparatus, device and medium based on a long connection.
[0005] An embodiment of the present disclosure provides a communication processing method based on a persistent connection. The method is applied to a server, comprising: responding to a connection request from a client, establishing a persistent connection with the client, and generating persistent connection encapsulation information; wherein the persistent connection encapsulation information includes a client identifier, a persistent connection channel, and an expiration time; calculating based on the expiration time, determining a target index lock from a preset lock array, and adding a key-value pair constructed by the persistent connection channel and the persistent connection encapsulation information to a channel mapping table; locking the target index lock, adding the key-value pair constructed by the client identifier and the persistent connection encapsulation information to a target mapping table corresponding to a target index element in a target array, and then releasing the target index lock; wherein the target index element and the target index lock have a one-to-one correspondence based on the target index; processing the persistent connection between the server and the client based on the channel mapping table and the target array to obtain a processing result; receiving a service request sent by the client based on the target persistent connection channel determined by the processing result, responding to the service request, obtaining a service result, and sending it to the client.
[0006] The present disclosure also provides a communication processing device based on a persistent connection. The device includes: a response generation module, configured to respond to a connection request from a client, establish a persistent connection with the client, and generate persistent connection encapsulation information; wherein the persistent connection encapsulation information includes a client identifier, a persistent connection channel, and an expiration time; a calculation module, configured to calculate based on the expiration time and determine a target index lock from a preset lock array; a construction module, configured to add a key-value pair constructed by the persistent connection channel and the persistent connection encapsulation information to a channel mapping table, lock the target index lock, and release the target index lock after adding the key-value pair constructed by the client identifier and the persistent connection encapsulation information to a target mapping table corresponding to a target index element in a target array; wherein the target index element and the target index lock have a one-to-one correspondence based on the target index; a processing module, configured to process the persistent connection between the server and the client based on the channel mapping table and the target array to obtain a processing result; a receiving module, configured to receive a service request sent by the client based on the target persistent connection channel determined by the processing result; and a response acquisition module, configured to respond to the service request, obtain a service result, and send it to the client.
[0007] An embodiment of the present disclosure also provides an electronic device, which includes: a processor; a memory for storing executable instructions of the processor; the processor is used to read the executable instructions from the memory and execute the instructions to implement the communication processing method based on long connection provided in an embodiment of the present disclosure.
[0008] An embodiment of the present disclosure further provides a computer-readable storage medium, wherein the storage medium stores a computer program, and the computer program is used to execute the communication processing method based on a long connection as provided in the embodiment of the present disclosure.
[0009] The embodiments of the present disclosure further provide a computer program product, including a computer program, wherein the computer program is executed by a processor as the communication processing method based on a long connection provided by the embodiments of the present disclosure.
[0010] The technical solution provided by the embodiments of the present disclosure has the following advantages over the prior art: the communication processing solution based on a persistent connection provided by the embodiments of the present disclosure generates persistent connection encapsulation information including a client identifier, a persistent connection channel, and an expiration time after a server establishes a persistent connection with a client. A target index lock is determined based on the expiration time. A key-value pair is constructed by the persistent connection channel and the persistent connection encapsulation information and added to a channel mapping table. After locking the target index lock, a key-value pair is constructed by the client identifier and the persistent connection encapsulation information and added to a target mapping table corresponding to the target index element in the target array. The target index lock is then released. The persistent connection between the server and the client is processed based on the channel mapping table and the target array to obtain a processing result. A service request sent by the client based on the target persistent connection channel determined by the processing result is received. The service result is obtained in response to the service request and sent to the client. The technical problems of low connection efficiency and resource occupation between the server and the client in the prior art are solved. The server and the client establish a persistent connection to ensure continuous input and output. At the same time, concurrent data access is restricted by locking to ensure the correctness of data processing. The persistent connection is managed to ensure a one-to-one correspondence between the persistent connection and the client, thereby improving resource occupation efficiency and further improving communication efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] The above and other features, advantages, and aspects of the various embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and that the originals and elements are not necessarily drawn to scale.
[0012] Figure 1 A schematic diagram of the structure of a communication processing system based on a long connection provided in an embodiment of the present disclosure;
[0013] Figure 2 A flowchart of a communication processing method based on a long connection provided in an embodiment of the present disclosure;
[0014] Figure 3 An example diagram of long connection management provided in an embodiment of the present disclosure;
[0015] Figure 4 A flowchart of another communication processing method based on a long connection provided in an embodiment of the present disclosure;
[0016] Figure 5 A schematic diagram of the structure of a communication processing device based on a long connection provided in an embodiment of the present disclosure;
[0017] Figure 6 A schematic structural diagram of an electronic device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0018] The following describes embodiments of the present disclosure in more detail with reference to the accompanying drawings. Although certain embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be construed as limited to the embodiments described herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are for illustrative purposes only and are not intended to limit the scope of protection of the present disclosure.
[0019] It should be understood that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present disclosure is not limited in this respect.
[0020] As used herein, the term "including" and its variations are open-ended, i.e., "including but not limited to." The term "based on" means "based, at least in part, on." The term "one embodiment" means "at least one embodiment," the term "another embodiment" means "at least one additional embodiment," and the term "some embodiments" means "at least some embodiments." Other terms are defined in the following description.
[0021] It should be noted that the concepts of "first" and "second" mentioned in this disclosure are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.
[0022] It should be noted that the modifications of "one" and "plurality" mentioned in the present disclosure are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, they should be understood as "one or more".
[0023] The names of the messages or information exchanged between multiple devices in the embodiments of the present disclosure are only used for illustrative purposes and are not used to limit the scope of these messages or information.
[0024] In actual business scenarios, smart materials are generated for users, such as promotional copy for the target platform, decoration cases, etc. The existing business scenario requests are time-consuming. The traditional HTTP method receives all results at once. For time-consuming requests, users will have to wait for a long time. Moreover, a connection must be established for each request, which will result in an additional connection establishment delay. The delay is more obvious for multiple rounds of continuous interaction, resulting in technical problems such as time-consuming requests and low communication efficiency. Maintaining a long connection to avoid connecting every time will also lead to technical problems such as resource occupation.
[0025] In response to the technical problems in the prior art that requests are time-consuming, communication efficiency is low and resources are occupied, the embodiments of the present disclosure propose a communication processing method based on long connections, which maintains a long connection through interaction between the client and the server to ensure continuous input and output. The data can be regarded as a continuous stream for block processing and transmission through streaming output, avoiding waiting for all data to be ready, thereby improving efficiency and reducing latency, and effectively managing the long connection. Each client communicates through a unique long connection, that is, the request of the same client can reuse the same long connection to improve communication efficiency, and maintain an effective long connection to release an invalid long connection, avoiding excessive occupation of device resources, further maintaining the rational use of resources, and meeting communication needs.
[0026] The present disclosure provides a communication processing method based on a long connection, which can be applied to Figure 1 In the application environment shown, the communication processing method based on a persistent connection is applied to a communication processing system based on a persistent connection. The communication processing system based on a persistent connection includes a server 101 and a client 102. The server 101 and the client 102 communicate via a network. Server 101 responds to a connection request from client 102 and establishes a persistent connection with client 102, generating persistent connection encapsulation information; wherein the persistent connection encapsulation information includes a client identifier, a persistent connection channel, and an expiration time; calculates based on the expiration time, determines a target index lock from a preset lock array, and constructs a key-value pair using the persistent connection channel and the persistent connection encapsulation information and adds it to a channel mapping table; locks the target index lock, adds the key-value pair using the client identifier and the persistent connection encapsulation information as a target index element to a target mapping table corresponding to the target index element in the target array, and then releases the target index lock; wherein the target index element and the target index lock have a one-to-one correspondence based on the target index; processes the persistent connection between server 101 and client 102 based on the channel mapping table and the target array to obtain a processing result; receives a service request sent by client 102 based on the target persistent connection channel determined by the processing result, responds to the service request, obtains a service result, and sends it to client 102. Server 101 and client 102 may be, but are not limited to, various personal computers, laptops, smartphones, tablet computers, and portable wearable devices.
[0027] Therefore, the server and client use persistent connections to ensure continuous input and output, and manage the persistent connections between the server and the client. This not only ensures a one-to-one correspondence between persistent connections and client requests, but also ensures that persistent connections that have not communicated for a long time are effectively deleted, thereby freeing up device resources for new requests and further improving communication efficiency.
[0028] Figure 2This is a flow chart of a communication processing method based on a long connection provided by an embodiment of the present disclosure. The method can be executed by a communication processing device based on a long connection, wherein the device can be implemented using software and / or hardware and can generally be integrated into an electronic device. Figure 1 As shown, the method is applied to the server and includes:
[0029] Step 201: Respond to the client's connection request and establish a persistent connection with the client, generating persistent connection encapsulation information; wherein the persistent connection encapsulation information includes the client identifier, the persistent connection channel, and the expiration time.
[0030] Specifically, the client first initiates a connection request to the server. The server verifies and processes the connection request and then establishes a long connection with the client. A long connection refers to a persistent connection. After the client and server successfully handshake at the TCP (Transmission Control Protocol) layer, they do not disconnect immediately, and exchange messages multiple times based on this connection until either party of the connection (client or server) actively disconnects. In addition, during the long connection period, if no data packet is sent, the server and client need to send link detection packets to maintain the connection and further ensure the validity of the connection.
[0031] Among them, the client identifier can uniquely identify a client and is usually only sent when sending a connection request, so that the server can obtain the client identifier and can use the client's media access control address and serial number as the client identifier. The specific setting is selected according to the actual application; the long connection channel means that after a connection is established between the client and the server, the long connection channel can be reused for multiple requests and responses without the need to re-establish a new connection before sending data each time. A long connection channel can be uniquely identified by the channel identifier.
[0032] The expiration time refers to the timestamp of a persistent connection closure. When the expiration time is less than the current time, the persistent connection channel can be automatically closed, that is, the persistent connection between the client and the server is disconnected, ensuring that the persistent connection without communication for a long time is effectively deleted, thereby freeing up device resources and improving communication efficiency.
[0033] Specifically, after the server establishes a long connection with the client, the client identifier, long connection channel and expiration time can be obtained and encapsulated to generate a long connection encapsulation information; among them, user information (such as user account identifier, communication number identifier, etc.) can also be obtained and encapsulated into the long connection encapsulation information so that in subsequent business logic processing, user information can be directly obtained from the long connection encapsulation information to record the user's online time, etc., and the client does not need to carry user information for subsequent business requests, further improving data transmission efficiency.
[0034] Among them, there are many ways to obtain the expiration time of a long connection. In some embodiments, an expiration time interval is preset, such as 5 minutes. After the server and the client establish a long connection, the current time is obtained, and the current time is added to the preset expiration time interval to obtain the expiration time; among them, the current time can be understood as a timestamp.
[0035] Step 202: Calculate based on the expiration time, determine the target index lock from the preset lock array, and construct a key-value pair of the long connection channel and the long connection encapsulation information and add it to the channel mapping table.
[0036] Step 203: Lock the target index lock, add the key-value pair constructed by the client identifier and the long connection encapsulation information to the target mapping table corresponding to the target index element in the target array, and then release the target index lock; wherein, the target index element and the target index lock correspond one-to-one based on the target index.
[0037] In the embodiment of the present disclosure, a lock array is pre-set, and one array element in the lock array is one lock. An array element and thus a lock can be uniquely identified by an array subscript.
[0038] In an embodiment of the present disclosure, the lock corresponding to the current thread is determined by the expiration time of the current long connection. Specifically, the array subscript is determined based on the expiration time and the array length of the preset lock array, and the array element corresponding to the array subscript in the preset lock array is used as the target subscript lock.
[0039] In an embodiment of the present disclosure, the array length of the target array is the same as the array length of the preset lock array, and the target subscript element and the target subscript lock correspond one-to-one based on the target subscript, so that after the target subscript lock is locked, only the current thread can process the target subscript element of the target array.
[0040] Therefore, after obtaining the target sub-index lock, the client identifier and long connection encapsulation information are used to construct a key-value pair and added to the target mapping table corresponding to the target subscript element in the target array, and then the target subscript lock is released. The locking mechanism is used to prevent multiple threads from processing the same long connection at the same time to ensure the correctness of data storage, further improve the accuracy of subsequent server and client interactions, and meet business scenario requirements.
[0041] Among them, the client identifier and the long connection encapsulation information are constructed into a key-value pair, which can be understood as one client identifier corresponding to one long connection encapsulation information, that is, one client identifier and one long connection encapsulation information have a corresponding mapping relationship, and the corresponding long connection encapsulation information can be matched in the target array according to one client identifier; one long connection encapsulation information may include the client identifier, long connection channel, expiration time, IP (Internet Protocol) address information and user information, etc., and the encapsulation processing is performed according to the actual application scenario.
[0042] It should be noted that when there are multiple clients establishing long connections with the server at the same time, if the expiration time is the same, it means that a key-value pair constructed with each client identifier and the corresponding long connection encapsulation information should be stored in the same target mapping table. That is to say, when the target mapping table stores multiple key-value pairs, a key-value pair is uniquely identified by the client identifier.
[0043] In the embodiment of the present disclosure, a key-value pair is constructed by constructing the long connection channel and the long connection encapsulation information and added to the channel mapping table, so that the subsequent requesting client does not need to send user information, client identification and other information again. The client identification, expiration time, etc. can be obtained by directly querying the channel mapping table according to the long connection channel, thereby further improving data transmission efficiency and thus improving communication efficiency.
[0044] Among them, constructing a key-value pair of the long connection channel and the long connection encapsulation information can be understood as one long connection channel corresponds to one long connection encapsulation information, that is, one long connection channel and one long connection encapsulation information have a corresponding mapping relationship, and the corresponding long connection encapsulation information can be matched in the channel mapping number table according to one long connection channel.
[0045] For example, the server and client establish a persistent connection to meet business needs. Subsequent business logic, such as recording the user's online time, requires user information. When the client sends a connection request, it only needs to obtain the user information and encapsulate it in the persistent connection. The client does not need to carry the user information in subsequent requests and can obtain the user information through the persistent connection channel.
[0046] In other words, requests from the same client can reuse the same persistent connection, binding the user information requested during the connection to the persistent connection channel. Subsequent requests can reuse the connection and user information without having to carry the full amount of information each time to improve data transmission efficiency.
[0047] Step 204: Process the long connection between the server and the client based on the channel mapping table and the target array to obtain a processing result.
[0048] In the embodiment of the present disclosure, long connections can also be managed to maintain valid connections and release invalid connections.
[0049] Specifically, there are many ways to obtain the processing result of processing the long connection between the server and the client based on the channel mapping number table and the target array. In some embodiments, in response to the heartbeat request of the client, the current long connection channel is obtained, and the current long connection encapsulation information is obtained from the channel mapping number table based on the current long connection channel; the current expiration time is obtained based on the current long connection encapsulation information, and the current expiration time is calculated, the current index lock is determined from the preset lock array, and the current index lock is locked, and the key-value pair constructed by the current client identifier and the current long connection encapsulation information in the current mapping table corresponding to the current index element is removed from the target array, and the current index lock is released; the update expiration time is calculated, and the current long connection encapsulation information is updated based on the update expiration time to obtain the updated long connection encapsulation information; the update expiration time is calculated based on the update expiration time, the update index lock is determined from the preset lock array, and the update index lock is locked, and the key-value pair constructed by the current client identifier and the updated long connection encapsulation information is added to the update mapping table corresponding to the updated index element in the target array, and the update index lock is released.
[0050] In other embodiments, in response to the client's exit request, the long connection is disconnected from the client, the long connection channel is obtained, and the long connection channel and the corresponding long connection encapsulation information are deleted from the channel mapping table; the disconnection expiration time is obtained based on the long connection encapsulation information, and the disconnection subscript lock is determined from the preset lock array, and the disconnection subscript lock is locked, and the key-value pair constructed by the disconnected client identifier and the long connection encapsulation information in the disconnection mapping table corresponding to the disconnection subscript element is removed from the target array, and then the disconnection subscript lock is released.
[0051] In some other embodiments, calculations are performed based on the current time, an expired index lock is determined from a preset lock array, the expired index lock is locked, and a candidate mapping table corresponding to the expired subscript element in the target array is obtained; the candidate mapping table is queried to obtain the target long connection encapsulation information whose candidate expiration time is less than the current time, and the key-value pairs including the target long connection encapsulation information in the channel mapping table and the target array are deleted, and then the expired index lock is released.
[0052] The above is only an example of processing a long connection between a server and a client based on a channel mapping table and a target array to obtain a processing result. The embodiment of the present disclosure does not limit the specific implementation method of processing a long connection between a server and a client based on a channel mapping table and a target array to obtain a processing result. One or more appropriate methods can be selected for processing according to the actual application scenario.
[0053] Step 105: Receive a service request sent by the client through the target persistent connection channel determined based on the processing result, respond to the service request, obtain the service result, and send it to the client.
[0054] In an embodiment of the present disclosure, after managing the persistent connection, the current persistent connection channel between the client and the server can be determined as the target persistent connection channel based on the processing results, and business requests such as query requests, translation requests, and speech-to-text conversion requests, etc. are sent through the target persistent connection channel, so that the server responds to the business request to obtain the business results and sends them to the client.
[0055] In the embodiment of the present disclosure, there are many ways to respond to a business request and obtain a business result and send it to the client. A request identifier can be obtained based on the business request, and a storage result can be obtained by querying a preset database based on the request identifier. When the storage result includes an end identifier, the storage result is sent to the client for display, indicating that the business request is ended.
[0056] It should be noted that when the storage result does not include an end identifier or the preset database does not have a storage result that matches the request identifier, the server can forward the business request to other target servers for processing, thereby receiving the processing result returned by the target server and storing it in the preset database. While converting the business request to the target server, it can continue to query in the preset database according to the request identifier through an asynchronous query method, and feed back the matching storage result to the client until the storage result includes an end identifier or the number of queries reaches a certain threshold, stops the query, and feeds back a failure message.
[0057] Among them, during the above query process, the query can be performed through the message queue. When receiving failure information, the request can be resent to the message queue and wait for retry, thereby improving the query flexibility, and the query results can be returned to the client in real time until the end mark is included, thereby realizing efficient management of business results, avoiding repeated requests, avoiding occupying network resources, and improving the response time of client requests.
[0058] The disclosed embodiment provides a communication processing solution based on a persistent connection. After establishing a persistent connection with a client, a persistent connection encapsulation information including a client identifier, a persistent connection channel, and an expiration time is generated. A target index lock is determined based on the expiration time. A key-value pair is constructed by the persistent connection channel and the persistent connection encapsulation information and added to a channel mapping table. After locking the target index lock, a key-value pair is constructed by the client identifier and the persistent connection encapsulation information and added to a target mapping table corresponding to the target index element in the target array. The target index lock is then released. Based on the channel mapping table and the target array, the persistent connection between the server and the client is processed to obtain a processing result. A service request sent by the client based on the target persistent connection channel determined by the processing result is received. In response to the service request, a service result is obtained and sent to the client. The solution solves the technical problems of low connection efficiency and resource occupation between the server and the client in the prior art. The solution ensures continuous input and output by establishing a persistent connection between the server and the client. At the same time, concurrent data access is restricted by locking to ensure the correctness of data processing. The persistent connection is managed to ensure a one-to-one correspondence between the persistent connection and the client, thereby improving resource occupation efficiency and further improving communication efficiency.
[0059] In some embodiments, a calculation is performed based on the expiration time to determine a target index lock from a preset lock array, including: determining an array index based on the expiration time and the array length of the preset lock array; wherein, after establishing a long connection with the client, the expiration time is determined based on the current time and a preconfigured expiration time interval; and determining a target index lock from the preset lock array based on the array index.
[0060] Specifically, after establishing a long connection with the client, obtain the timestamp of the current time and add the preconfigured expiration time interval to obtain the expiration time, then take the modulus of the array length of the preset lock array according to the expiration time to obtain the array subscript, and use the lock corresponding to the array subscript in the preset lock array as the target subscript lock.
[0061] It should be noted that the server can establish a long connection with one or more clients at the same time. In multiple client scenarios, there are multiple clients with the same expiration time, so the calculated array index is the same, and the obtained target index lock is the same. Therefore, the target index lock is subsequently locked to ensure that only one thread constructs a key-value pair with the client identifier and long connection encapsulation information and adds it to the target mapping table corresponding to the target index element in the target array. After one client is added, the target index lock is released so that other clients can be added in turn.
[0062] That is to say, through the locking mechanism, it is possible to add key-value pairs constructed by multiple client identifiers and corresponding long connection encapsulation information to the target mapping table corresponding to the same target subscript element in the target array, that is, there are multiple client identifiers and corresponding long connection encapsulation information key-value pairs in the target mapping table, and the corresponding long connection encapsulation information is uniquely determined by the client identifier in the target mapping table.
[0063] In the above embodiment, by setting a lock array and determining the lock corresponding to the long connection according to the expiration time of the long connection, the correctness of data processing is ensured by setting the lock, thereby improving the accuracy of subsequent communications.
[0064] Based on the description of the foregoing embodiment, there are many ways to process the long connection between the server and the client based on the channel mapping table and the target array to obtain the processing result, which can be selected according to the specific application scenario.
[0065] In some embodiments, in response to a heartbeat request from a client, the current long connection channel is obtained, and the current long connection encapsulation information is obtained from a channel mapping table based on the current long connection channel; the current expiration time is obtained based on the current long connection encapsulation information, and the current expiration time is calculated, the current index lock is determined from a preset lock array, and the current index lock is locked, and the key-value pair constructed by the current client identifier and the current long connection encapsulation information in the current mapping table corresponding to the current index element is removed from the target array, and the current index lock is released; the update expiration time is calculated, and the current long connection encapsulation information is updated based on the update expiration time to obtain the updated long connection encapsulation information; the update expiration time is calculated based on the update expiration time, the update index lock is determined from the preset lock array, and the update index lock is locked, and the key-value pair constructed by the current client identifier and the updated long connection encapsulation information is added to the update mapping table corresponding to the updated index element in the target array, and the update index lock is released.
[0066] Specifically, after the client and server are connected, the server will obtain the long connection information of all clients corresponding to the long connections, such as the client ID, long connection channel, expiration time and user information, and encapsulate it to obtain the long connection encapsulation information, and construct a key-value pair of the long connection channel and the long connection encapsulation information and add it to the channel mapping table, and construct a key-value pair of the client ID and the long connection encapsulation information and add it to the mapping table in the target array, determine a target index lock from the preset lock array according to the expiration time of each long connection, determine the target subscript element according to the target subscript lock, and then construct a key-value pair of the client ID and the long connection encapsulation information and add it to the target mapping table corresponding to the target subscript element.
[0067] For example, Figure 3As shown, the preset lock array Lock[] includes lock elements S0, S1 and S2, each of which corresponds to a mapping table Map, namely mapping table M0, mapping table M1 and mapping table M2. The locks and mapping tables correspond one to one through array subtitles, which are determined by calculating the expiration time. Among them, a mapping table Map can store one or more client identifiers and corresponding long connection encapsulation information, such as Figure 3 As shown, a mapping table Map includes a client identifier D1 and corresponding persistent connection encapsulation information C1 (including persistent connection channel, client identifier, IP address, expiration time and user information); a client identifier D2 and corresponding persistent connection encapsulation information C2.
[0068] That is to say, through the above processing, the long connection channel and the long connection encapsulation information are constructed into a key-value pair and added to the channel mapping table, and the client identifier and the corresponding long connection encapsulation information are added to the mapping table in the form of a key-value pair for long connection management, so that the client only needs to obtain the long connection channel each time it sends a request (such as a business request and a heartbeat request). The long connection encapsulation information can be obtained through the long connection channel, and the expiration time can be obtained from the long connection encapsulation information, and the mapping table can be managed.
[0069] Specifically, the heartbeat request between the client and the server is processed. The client sends a heartbeat request, retrieves the current long connection encapsulation information from the channel mapping table according to the current long connection channel, and obtains the current expiration time of the long connection from the current long connection encapsulation information. The array subscript obtained by taking the modulus of the current expiration time and the array length of the preset lock array is used as an index to determine the current subscript lock from the preset lock array and lock it. Then, the mapping relationship corresponding to the client identifier in the current mapping table corresponding to the current subscript element in the target array is removed, that is, the key-value pair constructed by the current client identifier and the current long connection encapsulation information. After processing, the lock is released. Then, a new expiration time is calculated, and the expiration time in the current long connection encapsulation information is updated. The new expiration time is taken as the modulus of the array length of the preset lock array to obtain a new array subscript as a new index to determine the updated subscript lock from the preset lock array and lock it. Then, the new mapping relationship, that is, the current client identifier and the updated long connection encapsulation information, is stored in the update mapping table corresponding to the updated subscript element in the target array. After processing, the lock is released.
[0070] Based on the above description, for the maintenance of the long connection, the client sends a heartbeat request, obtains the long connection encapsulation information from the channel mapping table according to the long connection channel, takes out the current expiration time, and updates the expiration time of the long connection (for example, the current time plus the preset expiration time interval, such as 5 minutes). The length of the array is modulo the old expiration time to obtain the array index, such as 2, and Lock[2] is locked. The mapping relationship between the current client device identifier and the long connection encapsulation information is removed from the mapping table Map[2] in the target array and Lock[2] is released. The array is modulo the latest expiration time to obtain the array index, such as 3, and Lock[3] is locked to ensure that only one thread operates the mapping table Map[3] at the same time. At the same time, the current client device identifier and the new long connection encapsulation information are written in the mapping table Map[3]. After writing, Lock[3] is released.
[0071] In the above embodiment, by processing the heartbeat request of the client, a new expiration time is updated to ensure that a long connection is maintained between the client and the server to ensure continuous input and output.
[0072] In some embodiments, in response to the client's exit request, the long connection is disconnected from the client, the long connection channel is obtained, and the long connection channel and the corresponding long connection encapsulation information are deleted from the channel mapping table; the disconnection expiration time is obtained based on the long connection encapsulation information, and the disconnection index lock is determined from the preset lock array, and the disconnection index lock is locked, and the key-value pair constructed by the disconnected client identifier and the long connection encapsulation information in the disconnection mapping table corresponding to the disconnection index element is removed from the target array, and then the disconnection index lock is released.
[0073] In the embodiment of the present disclosure, when the client normally exits the server long connection, the mapping relationship corresponding to the long connection channel stored in the mapping table in the target array is deleted, the long connection encapsulation information of the client identifier is obtained, and the expiration time is obtained. The expiration time is modulo the array length of the preset lock array to obtain the array subscript as the index to determine the lock from the preset lock array and lock it, and then remove the mapping relationship corresponding to the client identifier in the mapping table corresponding to the index in the target array, and release the lock after processing.
[0074] Specifically, to exit a long connection, the client sends an exit request, obtains the long connection encapsulation information from the channel mapping table according to the long connection channel, obtains the client device identifier and expiration time from the long connection encapsulation information, and uses the expiration time to modulo the array length. For example, if the long connection is at position 3 in the array index, Lock[3] is locked to ensure that only one thread operates the mapping table Map[3] at the same time, removes the mapping relationship between the client device identifier of this long connection and the long connection encapsulation information, releases the lock of Lock[3], and disconnects the connection.
[0075] In the above embodiment, it is ensured that the long connection that needs to be exited is effectively deleted, thereby releasing the resources of the device for new requests, further improving the communication efficiency and effect.
[0076] In some embodiments, a calculation is performed based on the current time, an expired index lock is determined from a preset lock array, the expired index lock is locked, and a candidate mapping table corresponding to the expired index element in the target array is obtained; the candidate mapping table is queried to obtain the target long connection encapsulation information whose candidate expiration time is less than the current time, and the key-value pairs including the target long connection encapsulation information in the channel mapping table and the target array are deleted, and then the expired index lock is released.
[0077] Specifically, in the case of the above-mentioned normal exit, a scheduled task can be started to perform scheduled scanning and delete expired long connection information. More specifically, the array subscript obtained by modulo the array length of the preset lock array according to the current time is used as the index to obtain the lock from the preset lock array and lock it, obtain the mapping table of the corresponding position in the target array, traverse the mapping table, and if there is an expiration time less than the current time, delete all the stored information and disconnect the connection. After the processing is completed, the lock is released.
[0078] Specifically, the detection of long connections is done by starting a detection thread and checking for expired long connections at intervals of, for example, 1 second (the time can be configured as needed). If there are any expired long connections, the connection is disconnected. More specifically, the timestamp of the current time is obtained (the expiration time in the channel mapping table is the timestamp), the remainder of the array is taken, and the array index, for example, 10, is obtained. Lock
[10] is then locked, and the mapping table Map
[10] is obtained from the target array. Map
[10] is traversed. If the expiration time of the long connection encapsulation information in the mapping relationship between the client identifier and the long connection encapsulation information is less than the current time, the mapping relationship is removed and the connection is disconnected. After traversing Map
[10] , the lock of Lock
[10] is released.
[0079] In the above embodiment, it is ensured that long connections without communication for a long time are effectively deleted, thereby releasing device resources and further improving communication efficiency and effect.
[0080] Based on the above embodiments, a reliable persistent connection management method is proposed, which can not only ensure a one-to-one correspondence between persistent connections and client requests, but also ensure the effective deletion of persistent connections that have no communication for a long time, thereby freeing up device resources for new requests and improving communication efficiency.
[0081] The disclosed embodiments also provide efficient management of the persistence of intermediate results and final results. By effectively managing the results, duplicate requests can be avoided, which not only avoids occupying network resources but also improves the response time of user requests. For details, please refer to the detailed description of the embodiments below.
[0082] In some embodiments, responding to a business request and obtaining a business result and sending it to the client includes: obtaining a request identifier based on the business request, querying a preset database based on the request identifier, and obtaining a stored result; determining whether the stored result includes an end identifier, obtaining a judgment result, and determining the business result based on the judgment result and sending it to the client.
[0083] In the disclosed embodiment, each service request has a unique request identifier or a message identifier in an IM (Instant Messenger) chat tool, such as a message identifier, which uniquely identifies a message or data record in a computer system; each message identifier is bound to a unique result.
[0084] In the embodiment of the present disclosure, a database is pre-set, and the pre-set database includes a first storage database and a second storage database, and the storage result includes a first storage result and / or a second storage result. The first storage database can be a temporary storage and the second storage result can be a persistent storage.
[0085] In some embodiments, determining whether the storage result includes an end identifier, obtaining a determination result, and determining the business result to be sent to the client based on the determination result include: when the first storage result is obtained in the first storage database, sending the first storage result to the client, and determining whether the first storage result includes an end identifier; when the first storage result does not include an end identifier, querying the first storage database according to a preset first frequency, and sending the queried storage result to the client until the queried storage result includes an end identifier; or, when the first query number is greater than or equal to the preset first number threshold, sending a first query failure message to the client; when the first storage result cannot be obtained in the first storage database and the second storage result is obtained in the second storage database, sending the second storage result to the client, and determining whether the second storage result includes an end identifier; when the second storage result does not include an end identifier, querying the second storage database according to a preset second frequency, and sending the queried storage result to the client until the queried storage result includes an end identifier; or, when the second query number is greater than or equal to the preset second number threshold, sending a second query failure message to the client.
[0086] In some embodiments, when no stored result can be found in the preset database, a long connection is established with the target server and the service request is forwarded to the target server while performing an asynchronous query in the preset database based on the request identifier; the processing result sent by the target server is received, and the processing result and the request identifier are associated and stored in the preset database; wherein, when the processing result includes multiple return results, an end identifier is added to the last received return result; each asynchronous query result is sent to the client until the asynchronous query result includes an end identifier.
[0087] Among them, the first frequency and the second frequency are pre-set and can be selected and set according to actual application needs, and can be the same or different; the first number threshold and the second number threshold are pre-set and can be selected and set according to actual application needs, and can be the same or different; the end identifier is used to uniquely identify the integrity of a query result, including the end identifier indicates that the query result is complete and the query can be ended.
[0088] Among them, the target server is a third-party server. The server can establish a long connection with the third-party server and forward the business request to the target server for query to obtain the business results.
[0089] Specifically, first obtain the result from the first storage database according to the request identifier. If the first storage database has a result, determine whether it is a complete result (as mentioned above, there is an end identifier at the end of the complete result). If it is a complete result, return it directly; if the result is incomplete, poll the first storage database to obtain the result. The polling time interval and number of times can be configured. For example, the interval is 1s, and the maximum number, that is, the first number threshold is 60 times. The result will be queried from the first storage database every 1s, and a maximum of 60 queries will be made. If the complete result is found within 60 times, the complete result will be sent to the client. If the final result has not been obtained after 60 times, a message indicating that the acquisition failed will be sent to the client.
[0090] Furthermore, if there is no result in the first storage database, the second storage database is queried, and if there is a result, it is sent to the client; if there is no result in both the first storage database and the second storage database, the result is generated asynchronously, and the query result of the first storage database is polled at the same time; among them, asynchrony can be implemented in many ways, such as using a message queue, and supporting retry on failure, and both ready-made components and custom components can be used.
[0091] Furthermore, the process of asynchronously obtaining results also establishes a long connection with the target server. If all results can be obtained at one time, an HTTP request can also be used. After obtaining the result returned by the target server, it will be compared with the result in the first storage database. If it is a new return result, the result in the first storage database will be updated (the existing content in the first storage database is added to the newly returned content, and the streaming result is returned in segments). After the request is completed, an end identifier is added to the end of the result, and the result is stored in the second storage database for persistent storage.
[0092] It should be noted that if an exception occurs during the above result request process, the request will be resent to the message queue and wait for retry. The processing of the same request identifier will be locked and the lock will be released after the request is completed.
[0093] In the above embodiment, by effectively managing the results, repeated requests can be avoided, which not only avoids occupying network resources but also improves the response time of user requests.
[0094] Based on the above description of the embodiment, the embodiment of the present disclosure takes the long connection as the link for data transmission, and takes into account the maintenance of the long connection and the preservation of the results. For example, Figure 4 As shown, it includes: step 4.1 the client sends a request to establish a persistent connection; step 4.2 returns a response that the persistent connection is successfully established; step 4.3 sends a heartbeat request; step 4.4 maintains the persistent connection; step 4.5 the client sends a business request; step 4.6 the server requests a third party (AI (Artificial Intelligence) server) to obtain the result; step 4.7 the server obtains the result, updates the cache, and if it is the final result, updates the hybrid car and the database at the same time; step 4.8 the server polls the local storage and actively pushes the result to the client; step 4.9 the client displays the result.
[0095] Specifically, the client establishes a persistent connection with the server; the server saves persistent connection related information (such as client identification, persistent connection channel, expiration time, and user information); the client sends heartbeats periodically to maintain the persistent connection; the client sends a request; among them, different request types (such as login requests, text translation requests) have different request paths (Uniform Resource Locator System).
[0096] Furthermore, the server receives the client request and processes it; if it is voice-to-text or a large model is required, it will asynchronously communicate with the third party (such as Figure 4 The server polls the storage medium, obtains the results and returns them to the client; the third party returns the results and stores them in the above storage medium. If all the results have been returned (it is possible that all the results are returned once, or the results are returned in segments, so they need to be received multiple times), an end marker is added to the end of the result (only the last result has an end marker); the server detects the end marker and notifies the client that all the results have been returned; after receiving the results, if the client does not want to disconnect, it can continue to send heartbeats. If it wants to disconnect, it notifies the server to disconnect; if the same request is requested to the server again, the server will directly obtain the last result according to the request identifier and return it directly.
[0097] In an embodiment of the present disclosure, after a long connection is established between the server and the client, long connection encapsulation information including a client identifier, a long connection channel, and an expiration time is generated; a lock array is pre-set; a target array is pre-set, and each array element in the target array is a mapping table. The mapping table can store one or more client identifiers and corresponding long connection encapsulation information, that is, the mapping table stores the mapping relationship between the client identifier and the long connection encapsulation information. The target number is the same as the array length of the lock array, and the same lock array subscript processes the mapping table corresponding to the array element in the target array at the same subscript position.
[0098] Specifically, the client sends a connection request, that is, a login request, and the server calculates the expiration time of the long connection (the current time plus the pre-configured expiration time), and obtains the array index by taking the modulus of the lock array length according to the expiration time; the client identifier, long connection channel, and expiration time are encapsulated to generate long connection encapsulation information, and at the same time, a mapping relationship between the long connection channel and the long connection encapsulation information is constructed and put into the channel mapping table. In order to avoid errors caused by concurrent operations, the target index lock corresponding to the array index is first locked, and then the mapping relationship between the client identifier and the long connection encapsulation information is stored in the target mapping table corresponding to the target subscript element in the target array. After processing, the lock is released.
[0099] For example, when a client sends a connection request, it encapsulates the long connection channel, user information, expiration time, client ID, etc. into long connection encapsulation information, stores the mapping relationship between the long connection channel and the long connection encapsulation information in the channel mapping table, and modulo the array length according to the current expiration time to obtain the position of the array, such as 2, and then locks Lock[2] to ensure that only one thread is operating the mapping table Map[2] at the same time. At the same time, the mapping relationship of this long connection, that is, the mapping relationship between the client ID and the long connection encapsulation information, is written into the mapping of the mapping table Map[2]. After writing, the lock of Lock[2] is released. Among them, the channel mapping table can realize the acquisition of long connection encapsulation information through the long connection channel, and the long connection can be managed according to the expiration time in the long connection encapsulation information, further improving the flexibility and effectiveness of management.
[0100] Among them, each array element in the preset lock array is a lock, which determines that only one thread processes the mapping table corresponding to the array element at the same time; the mapping relationship between the client identifier and the long connection encapsulation information is stored in the mapping table, and each array element in the target array is a mapping table. The same as the length of the lock array, the same lock data subscript processes the mapping table at the same subscript position, and the mapping relationship between multiple client identifiers and long connection encapsulation information can be stored in the same mapping table, thereby improving the flexibility of maintenance.
[0101] The disclosed embodiment can handle the situation where there is no request for a long time but the client is not disconnected, and can better utilize the storage space of the server, obtain the client request and request other servers to obtain the result, and store and manage the result, and consider the failure retry mechanism, which is based on long connection and includes the maintenance of long connection and the preservation of intermediate results and final results when requesting third-party speech-to-text or the currently popular large model interface, and can be pushed to the client in real time. Moreover, for the same request, the result can be quickly obtained through the request identifier without repeatedly requesting the third-party server or the server to repeatedly process the same request, and it can automatically retry in case of failure.
[0102] The communication processing method based on long connections in the embodiment of the present disclosure has more detailed management of long connections and request results. The request identifier can be used to determine whether it is a repeated request so as to quickly respond to the request result, saving server-side computing and network resources and improving the user experience; and useless long links are released in a timely manner, further improving communication efficiency and effectiveness.
[0103] The communication processing solution based on long connections provided by the embodiments of the present disclosure ensures continuous input and output by establishing long connections, manages long connections to ensure a one-to-one correspondence between long connections and client requests, and ensures effective deletion of long connections that have no communication for a long time, thereby freeing up device resources for new client requests to use and further improving communication efficiency.
[0104] Figure 5 This is a structural diagram of a communication processing device based on a long connection provided in an embodiment of the present disclosure. The device can be implemented by software and / or hardware and can generally be integrated into an electronic device.
[0105] like Figure 5 As shown, the device includes:
[0106] The response generation module 501 is used to respond to the connection request of the client to establish a persistent connection with the client and generate persistent connection encapsulation information; wherein the persistent connection encapsulation information includes the client identifier, the persistent connection channel and the expiration time.
[0107] The calculation module 502 is configured to perform calculation based on the expiration time and determine a target index lock from a preset lock array.
[0108] Construction module 503 is used to add the key-value pair constructed by the long connection channel and the long connection encapsulation information to the channel mapping table, lock the target index lock, add the key-value pair constructed by the client identifier and the long connection encapsulation information to the target mapping table corresponding to the target subscript element in the target array, and then release the target subscript lock; wherein, the target subscript element and the target subscript lock have a one-to-one correspondence based on the target subscript.
[0109] The processing module 504 is configured to process the long connection between the server and the client based on the channel mapping table and the target array to obtain a processing result.
[0110] The receiving module 505 is configured to receive a service request sent by the client via the target persistent connection channel determined based on the processing result.
[0111] The response acquisition module 506 is used to respond to the service request, obtain the service result and send it to the client.
[0112] Optionally, the calculation module 502 is specifically used to: determine the array subscript based on the expiration time and the array length of the preset lock array; wherein, after establishing a long connection with the client, determine the expiration time based on the current time and a preconfigured expiration time interval; determine the target subscript lock from the preset lock array based on the array subscript.
[0113] Optionally, the processing module 504 is specifically used to: obtain the current long connection channel in response to the heartbeat request of the client, and obtain the current long connection encapsulation information from the channel mapping number table based on the current long connection channel; obtain the current expiration time based on the current long connection encapsulation information, and calculate according to the current expiration time, determine the current index lock from the preset lock array, and lock the current index lock, remove the key-value pair constructed by the current client identifier and the current long connection encapsulation information in the current mapping table corresponding to the current index element from the target array, and then release the current index lock; calculate the update expiration time, update the current long connection encapsulation information based on the update expiration time to obtain the updated long connection encapsulation information; calculate based on the update expiration time, determine the update index lock from the preset lock array, and lock the update index lock, add the key-value pair constructed by the current client identifier and the updated long connection encapsulation information to the update mapping table corresponding to the updated index element in the target array, and then release the update index lock.
[0114] Optionally, the processing module 504 is specifically used to: disconnect the long connection with the client in response to the client's exit request, obtain the disconnected long connection channel, and delete the disconnected long connection channel and the corresponding disconnected long connection encapsulation information from the channel mapping number table; obtain the disconnection expiration time based on the disconnected long connection encapsulation information for calculation, determine the disconnection subscript lock from the preset lock array, lock the disconnection subscript lock, and release the disconnection subscript lock after removing the key-value pair constructed by the disconnected client identifier and the disconnection long connection encapsulation information in the disconnection mapping table corresponding to the disconnection subscript element from the target array.
[0115] Optionally, the processing module 504 is specifically used to: calculate according to the current time, determine the expired index lock from the preset lock array, lock the expired index lock, and obtain the candidate mapping table corresponding to the expired index element in the target array; query the candidate mapping table to obtain the target long connection encapsulation information whose candidate expiration time is less than the current time, delete the key-value pair including the target long connection encapsulation information in the channel mapping number table and the target array, and then release the expired index lock.
[0116] Optionally, the response acquisition module 506 includes: an acquisition query unit, used to obtain a request identifier based on the business request, query a preset database based on the request identifier, and obtain a storage result; a judgment unit, used to judge whether the storage result includes an end identifier, obtain a judgment result, and determine the business result to be sent to the client based on the judgment result.
[0117] Optionally, the judgment unit is specifically used to: when the first storage result is obtained by the first storage database, send the first storage result to the client, and judge whether the first storage result includes an end identifier; when the first storage result does not include the end identifier, query the first storage database according to a preset first frequency, and send the queried storage result to the client until the queried storage result includes the end identifier; or, when the first query number is greater than or equal to a preset first number threshold, send a first query failure message to the client; when the first storage database cannot obtain the first storage result and the second storage result is obtained by the second storage database, send the second storage result to the client, and judge whether the second storage result includes an end identifier; when the second storage result does not include the end identifier, query the second storage database according to a preset second frequency, and send the queried storage result to the client until the queried storage result includes the end identifier; or, when the second query number is greater than or equal to a preset second number threshold, send a second query failure message to the client.
[0118] Optionally, the judgment unit is further used to: when the stored result cannot be queried in the preset database, forward the business request to the target server after establishing a long connection with the target server, and perform an asynchronous query on the preset database based on the request identifier; receive the processing result sent by the target server, and associate the processing result and the request identifier with each other and store them in the preset database; wherein, when the processing result includes multiple return results, add the end identifier to the last received return result; send each asynchronous query result to the client until the asynchronous query result includes the end identifier.
[0119] The communication processing device based on long connection provided by the embodiment of the present disclosure can execute the communication processing method based on long connection provided by any embodiment of the present disclosure, and has the corresponding functional modules and beneficial effects of the execution method.
[0120] An embodiment of the present disclosure further provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the communication processing method based on a long connection provided by any embodiment of the present disclosure.
[0121] Figure 6 This is a schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure. Figure 6 , which shows a schematic structural diagram of an electronic device 600 suitable for implementing the embodiments of the present disclosure. The electronic device 600 in the embodiments of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. Figure 6 The electronic device shown is only an example and should not limit the functions and scope of use of the embodiments of the present disclosure.
[0122] like Figure 6 As shown, the electronic device 600 may include a processing device 601 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. Various programs and data required for the operation of the electronic device 600 are also stored in the RAM 603. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0123] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touchpad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 600 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 6 The electronic device 600 is shown with various devices, but it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed instead.
[0124] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program carried on a non-transitory computer-readable medium, the computer program including program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network via the communication device 609, or installed from the storage device 608, or installed from the ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the communication processing method based on the long connection of the embodiment of the present disclosure are performed.
[0125] It should be noted that the computer-readable medium mentioned above in the present disclosure may be a computer-readable signal medium or a computer-readable storage medium, or any combination of the two. A computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or component, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, device, or component. In the present disclosure, a computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, which carries computer-readable program code. Such a propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium may be transmitted using any suitable medium, including but not limited to wires, optical cables, RF (radio frequency), etc., or any suitable combination thereof.
[0126] In some embodiments, the client and server can communicate using any currently known or future developed network protocol, such as HTTP (HyperText Transfer Protocol), and can be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.
[0127] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.
[0128] The above-mentioned computer-readable medium carries one or more programs. When the above-mentioned one or more programs are executed by the electronic device, the electronic device: after the server establishes a long connection with the client, generates long connection encapsulation information including the client identifier, the long connection channel and the expiration time, determines the target index lock based on the expiration time, constructs a key-value pair of the long connection channel and the long connection encapsulation information and adds it to the channel mapping table, locks the target index lock, adds the key-value pair constructed by the client identifier and the long connection encapsulation information to the target mapping table corresponding to the target index element in the target array, and then releases the target index lock, processes the long connection between the server and the client based on the channel mapping table and the target array to obtain a processing result, receives a business request sent by the client based on the target long connection channel determined by the processing result, responds to the business request, obtains a business result, and sends it to the client.
[0129] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages, or a combination thereof, including, but not limited to, object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a stand-alone software package, partially on the user's computer and partially on a remote computer, or entirely on the remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0130] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, program segment, or a part of code, and the module, program segment, or a part of code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order than that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of the boxes in the block diagram and / or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0131] The units involved in the embodiments described in this disclosure may be implemented in software or hardware, wherein the name of a unit does not necessarily limit the unit itself.
[0132] The functions described above herein may be performed, at least in part, by one or more hardware logic components. For example, and without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chip (SOCs), complex programmable logic devices (CPLDs), and the like.
[0133] In the context of the present disclosure, a machine-readable medium can be a tangible medium that can contain or store a program for use by or in conjunction with an instruction execution system, device or equipment. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium can include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0134] According to one or more embodiments of the present disclosure, the present disclosure provides an electronic device, including:
[0135] processor;
[0136] a memory for storing instructions executable by the processor;
[0137] The processor is configured to read the executable instructions from the memory and execute the instructions to implement any of the long connection-based communication processing methods provided in the present disclosure.
[0138] According to one or more embodiments of the present disclosure, the present disclosure provides a computer-readable storage medium, which stores a computer program, and the computer program is used to execute any of the long connection-based communication processing methods provided by the present disclosure.
[0139] The above description is merely a preferred embodiment of the present disclosure and an illustration of the technical principles employed. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by the specific combination of the above-mentioned technical features, but also includes other technical solutions formed by any combination of the above-mentioned technical features or their equivalents without departing from the above-mentioned disclosed concepts. For example, a technical solution formed by replacing the above-mentioned features with (but not limited to) technical features with similar functions disclosed in this disclosure.
[0140] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details have been included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.
[0141] Although the subject matter has been described in language specific to structural features and / or methodological logical acts, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are merely example forms of implementing the claims.
Claims
1. A communication processing method based on a long connection, characterized in that: Applied to the server, including: Responding to a connection request from a client, establishing a persistent connection with the client, and generating persistent connection encapsulation information; wherein the persistent connection encapsulation information includes a client identifier, a persistent connection channel, and an expiration time; Calculate based on the expiration time, determine the target index lock from the preset lock array, and construct a key-value pair of the long connection channel and the long connection encapsulation information and add it to the channel mapping table; Locking the target index lock, adding a key-value pair constructed by the client identifier and the long connection encapsulation information to a target mapping table corresponding to the target index element in the target array, and then releasing the target index lock; wherein the target index element and the target index lock have a one-to-one correspondence based on the target index; Processing the long connection between the server and the client based on the channel mapping table and the target array to obtain a processing result; Receive a service request sent by the client through the target persistent connection channel determined based on the processing result, respond to the service request, obtain a service result, and send it to the client.
2. The method according to claim 1, characterized in that The calculating based on the expiration time and determining the target subscript lock from the preset lock array includes: Determining an array subscript based on the expiration time and the array length of the preset lock array; wherein, after establishing a long connection with the client, determining the expiration time based on the current time and a preconfigured expiration time interval; The target index lock is determined from the preset lock array based on the array index.
3. The method according to claim 1, characterized in that The processing of the long connection between the server and the client based on the channel mapping table and the target array to obtain a processing result includes: In response to the heartbeat request of the client, obtain the current persistent connection channel, and obtain current persistent connection encapsulation information from the channel mapping table based on the current persistent connection channel; Obtaining a current expiration time based on the current long connection encapsulation information, performing calculations based on the current expiration time, determining a current subscript lock from the preset lock array, locking the current subscript lock, and releasing the current subscript lock after removing a key-value pair constructed by a current client identifier and the current long connection encapsulation information in a current mapping table corresponding to the current subscript element from the target array; Calculating an update expiration time, and updating the current persistent connection encapsulation information based on the update expiration time to obtain updated persistent connection encapsulation information; Calculation is performed based on the update expiration time, an update index lock is determined from the preset lock array, and the update index lock is locked. A key-value pair constructed by the current client identifier and the update long connection encapsulation information is added to the update mapping table corresponding to the update index element in the target array, and then the update index lock is released.
4. The method according to claim 1, wherein The processing of the long connection between the server and the client based on the channel mapping table and the target array to obtain a processing result includes: disconnecting the persistent connection with the client in response to a logout request from the client, obtaining a disconnected persistent connection channel, and deleting the disconnected persistent connection channel and corresponding disconnected persistent connection encapsulation information from the channel mapping table; The disconnection expiration time is obtained based on the disconnection long connection encapsulation information and calculated, the disconnection subscript lock is determined from the preset lock array, and the disconnection subscript lock is locked. The disconnection mapping table corresponding to the disconnection subscript element is removed from the target array, and the key-value pair constructed by the disconnection client identifier and the disconnection long connection encapsulation information is released.
5. The method according to claim 1, wherein The processing of the long connection between the server and the client based on the channel mapping table and the target array to obtain a processing result includes: Calculate according to the current time, determine the expired index lock from the preset lock array, lock the expired index lock, and obtain the candidate mapping table corresponding to the expired index element in the target array; The candidate mapping table is queried to obtain target long connection encapsulation information whose candidate expiration time is less than the current time, and the key-value pairs including the target long connection encapsulation information in the channel mapping table and the target array are deleted, and then the expired index lock is released.
6. The method according to claim 1, characterized in that The responding to the service request, obtaining the service result and sending it to the client includes: Obtaining a request identifier based on the business request, querying a preset database based on the request identifier, and obtaining a stored result; It is determined whether the storage result includes an end identifier, a determination result is obtained, and based on the determination result, the service result is determined to be sent to the client.
7. The method according to claim 6, characterized in that The preset database includes a first storage database and a second storage database, the storage result includes a first storage result and / or a second storage result, and the determining whether the storage result includes an end identifier, obtaining a determination result, and determining the service result to be sent to the client based on the determination result includes: When the first storage database obtains the first storage result, sending the first storage result to the client, and determining whether the first storage result includes an end identifier; When the first stored result does not include the end identifier, querying the first storage database at a preset first frequency and sending the query storage results to the client until the query storage results include the end identifier; or, when the first query number is greater than or equal to a preset first query number threshold, sending a first query failure message to the client; When the first storage database cannot obtain the first storage result and the second storage database obtains the second storage result, sending the second storage result to the client and determining whether the second storage result includes an end identifier; When the second storage result does not include the end identifier, query the second storage database according to the preset second frequency, and send the query storage result to the client until the query storage result includes the end identifier; or, when the second query number is greater than or equal to the preset second number threshold, send a second query failure message to the client.
8. The method according to claim 6, characterized in that The method further comprises: When the stored result cannot be found in the preset database, establishing a persistent connection with the target server, forwarding the service request to the target server, and performing an asynchronous query on the preset database based on the request identifier; Receive the processing result sent by the target server, and associate the processing result with the request identifier and store it in the preset database; wherein, when the processing result includes multiple return results, add the end identifier to the last received return result; Each asynchronous query result is sent to the client until the asynchronous query result includes the end identifier.
9. An electronic device, characterized in that: The electronic device comprises: processor; a memory for storing instructions executable by the processor; The processor is configured to read the executable instructions from the memory and execute the instructions to implement the communication processing method based on a long connection as described in any one of claims 1 to 8.
10. A computer-readable storage medium, characterized in that The storage medium stores a computer program, and the computer program is used to execute the communication processing method based on a long connection according to any one of claims 1 to 8.
11. A computer program product, characterized in that The invention comprises a computer program, wherein the computer program is executed by a processor to perform the communication processing method based on a long connection as described in any one of claims 1 to 8.