Long connection task execution method and device and related equipment
By establishing a long-lived connection channel with identity verification between the client and the server, using a dictionary data structure to store task information, and employing a target queue thread to execute tasks, the problem of insufficient long-lived connections on the server is solved, achieving high-performance communication and low-latency request processing.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- TENCENT TECHNOLOGY (SHENZHEN) CO LTD
- Filing Date
- 2022-03-09
- Publication Date
- 2026-04-28
AI Technical Summary
In existing technologies, the number of long-lived connections that a server can support is limited, and client request timeouts are likely to occur after a certain threshold is reached.
By establishing a long-lived connection channel based on identity between the client and the server, using a dictionary data structure to store pre-stored task information, and utilizing the target queue thread to execute the target task, the granularity of the processing lock is reduced to improve the request processing rate.
It increases the number of long-lived connections that a single server can support, reduces client request latency, and provides high-performance communication.
Smart Images

Figure CN116781764B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of Internet of Things (IoT) technology, and more specifically, to a long-connection task execution method, apparatus, and related equipment. Background Technology
[0002] With the development of IoT technology, more and more clients are requesting to establish long-lived connections with servers. In existing long-lived connection-based service implementations, the number of long-lived connections that a server can support is limited, and once the number of supported long-lived connections reaches a certain threshold, client request timeouts are likely to occur. Summary of the Invention
[0003] The purpose of this application is to provide a method, apparatus, and related equipment for executing tasks using long-lived connections, which can increase the number of long-lived connections supported by the server and support high-concurrency requests from clients. To achieve this purpose, the technical solutions provided in this application are as follows:
[0004] In a first aspect, embodiments of this application provide a long-connection task execution method, including:
[0005] The system receives a target request from a first client via a long-lived connection channel; the target request carries target task information.
[0006] Based on the target task information, the target queue thread corresponding to the first client is invoked, and the target queue thread executes the target task corresponding to the target request based on the pre-stored task information; wherein, the pre-stored task information is stored using a dictionary data structure;
[0007] Based on the target request, the result information of the target queue thread executing the target task is fed back to the first client through the long connection channel;
[0008] The long connection channel is associated with the first client based on the client's identity identifier.
[0009] In one feasible embodiment, prior to receiving the target request from the first client via the long-connection channel, the method further includes:
[0010] The system receives a connection request initiated by the first client via a message queue telemetry transport protocol, establishes a long connection using a transport control protocol or secure socket layer, and transmits information through the long connection channel corresponding to the first client; the connection request carries connection information.
[0011] A verification operation is performed based on the first object information in the connection information and the preset password information;
[0012] If the first object information matches the pre-stored object information, and the target hash value matches the pre-stored hash value corresponding to the first client, then the verification is successful and the connection information is stored using a dictionary data structure with the identity identifier of the first client as the key; otherwise, the verification is failed and the long connection is disconnected; wherein, the target hash value is determined based on the preset password information through hash operation.
[0013] In a feasible embodiment, the step of invoking the target queue thread corresponding to the first client based on the target task information includes:
[0014] A hash operation is performed based on the identity identifier of the first client in the target task information and the number of queues included in the thread pool to determine the target queue identifier corresponding to the first client;
[0015] The corresponding target queue thread is invoked based on the target queue identifier, so that each request in the target queue thread is processed serially through the target queue thread.
[0016] In one feasible embodiment, the target task information includes first object information and topic information; the step of executing the target task corresponding to the target request through the target queue thread based on the pre-stored task information includes:
[0017] The target queue thread queries the pre-stored task information to see if the topic information is included in the topic list corresponding to the first object information.
[0018] If so, when executing the target task corresponding to the target request, output result information including task response information;
[0019] If not, when executing the target task corresponding to the target request, output result information including task execution failure information;
[0020] The pre-stored task information includes the correspondence between topic information and object information stored using at least one dictionary data structure; the topic information is the traversal key, and the object information is the traversal value.
[0021] In one feasible embodiment, the target request is a subscription request or a publish request;
[0022] When the target request is a subscription request and task response information is output, executing the target task includes: storing the correspondence between the first object information and the topic information in the pre-stored task information;
[0023] When the target request is a publishing request and task response information is output, executing the target task includes: obtaining publishing information and topic information from the target task information; querying the pre-stored task information for second object information that corresponds to the topic information; and sending the publishing information to the second client corresponding to the second object information.
[0024] In a feasible embodiment, when the received publishing information is encrypted information encrypted by the first client using an Advanced Encryption Standard (AES) algorithm, the step of obtaining the publishing information from the target task information includes:
[0025] Obtain the encrypted information from the target task information;
[0026] The published information is obtained by decrypting the encrypted information using the Advanced Encryption Standard algorithm.
[0027] In a feasible embodiment, when the received publishing information is compressed information compressed by the first client using a compression algorithm, the step of obtaining the publishing information from the target task information includes:
[0028] Obtain the compression information from the target task information;
[0029] The compressed information is decompressed using the compression algorithm to obtain the published information.
[0030] In one feasible embodiment, the method further includes:
[0031] Receive a disconnection request from the first client; the disconnection request carries disconnection information;
[0032] Based on the disconnection information, the long connection channel corresponding to the first client is closed, and information related to the first client is cleared.
[0033] Secondly, embodiments of this application provide a service allocation method, including:
[0034] Based on a load balancing strategy, each received client connection is distributed to a corresponding server; the server cluster contains at least one server.
[0035] The server executes the long-connection task execution method described in any embodiment of the first aspect.
[0036] Thirdly, embodiments of this application provide a long-connection task execution device, including:
[0037] The receiving module is used to receive a target request sent by a first client through a long connection channel; the target request carries target task information.
[0038] The invocation module is used to invoke the target queue thread corresponding to the first client based on the target task information, and to execute the target task corresponding to the target request through the target queue thread based on the pre-stored task information; wherein, the pre-stored task information is stored in a dictionary data structure;
[0039] The feedback module is used to feed back the result information of the target queue thread executing the target task to the first client through the long connection channel based on the target request;
[0040] The long connection channel is associated with the first client based on the client's identity identifier.
[0041] In one feasible embodiment, the receiving module is configured to, before performing the task of receiving the target request from the first client via the long connection channel, further be configured to:
[0042] The system receives a connection request initiated by the first client via a message queue telemetry transport protocol, establishes a long connection using a transport control protocol or secure socket layer, and transmits information through the long connection channel corresponding to the first client; the connection request carries connection information.
[0043] A verification operation is performed based on the first object information in the connection information and the preset password information;
[0044] If the first object information matches the pre-stored object information, and the target hash value matches the pre-stored hash value corresponding to the first client, then the verification is successful and the connection information is stored using a dictionary data structure with the identity identifier of the first client as the key; otherwise, the verification is failed and the long connection is disconnected; wherein, the target hash value is determined based on the preset password information through hash operation.
[0045] In a feasible embodiment, when the calling module is used to execute the call to the target queue thread corresponding to the first client based on the target task information to execute the target task corresponding to the target request, it is specifically used for:
[0046] A hash operation is performed based on the identity identifier of the first client in the target task information and the number of queues included in the thread pool to determine the target queue identifier corresponding to the first client;
[0047] Based on the target queue identifier, the corresponding target queue thread is invoked to execute the target task corresponding to the target request, so as to process the various requests in the target queue thread in sequence.
[0048] In one feasible embodiment, the target task information includes first object information and topic information; when the calling module is used to execute the target task corresponding to the target request, it is specifically used for:
[0049] In the pre-stored task information, check whether the topic information is included in the topic list corresponding to the first object information;
[0050] If so, when executing the target task corresponding to the target request, output result information including task response information;
[0051] If not, when executing the target task corresponding to the target request, output result information including task execution failure information;
[0052] The pre-stored task information includes the correspondence between topic information and object information stored using at least one dictionary data structure; the topic information is the traversal key, and the object information is the traversal value.
[0053] In one feasible embodiment, the target request is a subscription request or a publish request;
[0054] When the target request is a subscription request and outputs task response information, the calling module, when executing the target task, is specifically used to: store the correspondence between the first object information and the topic information in the pre-stored task information;
[0055] When the target request is a publishing request and outputs task response information, the calling module, when executing the target task, specifically performs the following: obtaining publishing information and topic information from the target task information; querying second object information that corresponds to the topic information from the pre-stored task information; and sending the publishing information to the second client corresponding to the second object information.
[0056] In a feasible embodiment, when the received publishing information is encrypted information encrypted by the first client using the Advanced Encryption Standard algorithm, the calling module, when used to obtain the publishing information in the target task information, specifically performs the following:
[0057] Obtain the encrypted information from the target task information;
[0058] The published information is obtained by decrypting the encrypted information using the Advanced Encryption Standard algorithm.
[0059] In a feasible embodiment, when the received publishing information is compressed information compressed by the first client using a compression algorithm, the calling module, when used to obtain the publishing information in the target task information, specifically performs the following:
[0060] Obtain the compression information from the target task information;
[0061] The compressed information is decompressed using the compression algorithm to obtain the published information.
[0062] In one feasible embodiment, the device further includes a disconnection module for:
[0063] Receive a disconnection request from the first client; the disconnection request carries disconnection information;
[0064] Based on the disconnection information, the long connection channel corresponding to the first client is closed, and information related to the first client is cleared.
[0065] Fourthly, embodiments of this application provide a service allocation apparatus, including:
[0066] The distribution module is used to distribute the received client connections to the corresponding servers based on a load balancing strategy; the server cluster where the servers are located includes at least one server.
[0067] The server executes the long-connection task execution method described in any embodiment of the first aspect.
[0068] Fifthly, this application also provides an electronic device including a memory and a processor, wherein the memory stores a computer program, and the processor, when running the computer program, performs the methods provided in any optional embodiment of this application.
[0069] In a sixth aspect, this application also provides a computer-readable storage medium storing a computer program that, when run in a processor, enables the processor to execute the methods provided in any optional embodiment of this application.
[0070] In a seventh aspect, this application provides a computer program product, which includes a computer program that, when executed by a processor, implements the methods provided in any optional embodiment of this application.
[0071] The beneficial effects of the technical solution provided in this application are:
[0072] This application provides a long-connection task execution method that can be applied to a server. Specifically, when a long-connection channel and a first client establish a corresponding relationship based on the client's identity identifier, that is, when a long connection is established between the first client and the server, a target request sent by the first client can be received through the long-connection channel. This target request carries target task information. Then, based on the target task information, the target queue thread corresponding to the first client is invoked, and the target queue thread executes the target task corresponding to the target request based on pre-stored task information. On this basis, based on the target request, the result information of the target task executed by the target queue thread is fed back to the first client through the long connection. Compared to existing technologies, this application adjusts the implementation of the task execution method from the overall architecture. First, the long connection between the client and the server is established based on the client's identity. Then, the target task corresponding to the target request initiated by the client is executed in the form of a call queue thread. By using a dictionary data structure to store the pre-stored task information for executing the target task, the number of long connections that a single server can support is increased by providing a high-performance architecture. Furthermore, by using a dictionary data structure to store the pre-stored task information for executing the target task, the lock granularity of processing target task information is reduced, the query rate per second of request processing is increased, thereby reducing client request latency and providing high-performance communication.
[0073] This application also provides a service allocation method that can be applied to a load balancing device managing individual servers. The server cluster managed by the load balancing device includes at least one server. Specifically, based on a load balancing strategy, each received client connection is distributed to its corresponding server, and each server can execute the long-connection task execution method shown in the above embodiment. This application allows for the deployment of multiple servers in a server cluster, corresponding to multiple backend service nodes, with each backend service node mounted on a load balancing device. The load balancing device distributes traffic according to a load balancing strategy, which helps to balance the number of long connections supported by each individual server. Furthermore, by increasing or decreasing the number of servers deployed in the server cluster, cluster expansion or contraction can be achieved, improving service scalability and availability. Attached Figure Description
[0074] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments of this application will be briefly introduced below.
[0075] Figure 1 This application provides a schematic flowchart of a long-connection task execution method according to an embodiment of the present application.
[0076] Figure 2A flowchart illustrating a service allocation method provided in an embodiment of this application;
[0077] Figure 3 A schematic diagram of an operational architecture provided for an embodiment of this application;
[0078] Figure 4 A schematic diagram of a server cluster deployment architecture provided in an embodiment of this application;
[0079] Figure 5 A schematic diagram of a server architecture provided for an embodiment of this application;
[0080] Figure 6 A schematic diagram of a data structure provided in an embodiment of this application;
[0081] Figure 7 A schematic diagram of a client request process provided in an embodiment of this application;
[0082] Figure 8 A schematic diagram of a long-connection task execution device provided in an embodiment of this application;
[0083] Figure 9 This is a schematic diagram of the structure of a service distribution device provided in an embodiment of this application;
[0084] Figure 10 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0085] The embodiments of this application are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain this application, and should not be construed as limiting the invention.
[0086] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this application means the presence of the stated features, integers, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof. It should be understood that when we say an element is “connected” or “coupled” to another element, it can be directly connected or coupled to the other element, or there may be intermediate elements. Furthermore, “connected” or “coupled” as used herein can include wireless connections or wireless coupling. The term “and / or” as used herein includes all or any units and all combinations of one or more associated listed items.
[0087] This application addresses the problems in existing long-connection-based service implementation schemes, such as the limited number of long connections supported by the server and the tendency for client requests to time out after reaching a certain threshold. It proposes a long-connection task execution method, apparatus, and related equipment. Based on the implementation of this scheme, the number of long connections that a single server can support can be increased, and client request latency can be reduced, providing high-performance communication.
[0088] The solutions in this application involve scenarios such as message push, instant messaging services, and IoT communication services. Based on the solutions in this application, high-performance long-connection publish or subscribe services can be provided.
[0089] Before providing a further detailed description of the embodiments of this application, the nouns and terms involved in the embodiments of this application will be explained, and the nouns and terms involved in the embodiments of this application shall be interpreted as follows.
[0090] The Internet of Things (IoT) refers to the use of various information sensors, RFID technology, GPS, infrared sensors, laser scanners, and other devices and technologies to collect real-time information on any object or process that needs to be connected and interacted with. This information includes sound, light, heat, electricity, mechanics, chemistry, biology, and location data, and is accessed through various possible networks to achieve ubiquitous connectivity between things and between things themselves, enabling intelligent sensing, identification, and management of objects and processes. The IoT is an information carrier based on the internet and traditional telecommunications networks, enabling all independently addressable ordinary physical objects to form an interconnected network.
[0091] Cloud IoT aims to connect the information sensed and commands received by traditional IoT devices to the Internet, truly achieving networking. It also enables massive data storage and computation through cloud computing technology. Due to the nature of IoT, which involves connecting things to each other and sensing the current operating status of each "object" in real time, a large amount of data is generated in this process. How to aggregate this information and how to sift out useful information from the massive amount of data to support decision-making for future development have become key issues affecting the development of IoT. As a result, IoT cloud based on cloud computing and cloud storage technology has become a powerful support for IoT technology and applications.
[0092] Message Queuing Telemetry Transport (MQTT) is a client-server message publish / subscribe transport protocol. It can be used in machine-to-machine (M2M) communication and the Internet of Things (IoT).
[0093] Dictionary (map) data structure: A map is a data type that binds keys and values together. It is implemented using a hash table, which allows you to quickly find the corresponding value by the key.
[0094] Transmission Control Protocol (TCP) is a connection-oriented, reliable, byte-stream-based transport layer communication protocol.
[0095] Secure Socket Layer (SSL) is a security protocol implemented on top of traditional communication protocols. It achieves information confidentiality through the use of public-key and symmetric-key cryptography. All transactions between SSL clients and servers are encrypted using the keys and algorithms established during the SSL handshake process.
[0096] Queries per second (QPS) is a measure of how much traffic a particular query server processes within a given time period.
[0097] The Advanced Encryption Standard (AES) algorithm is a symmetric encryption algorithm, meaning that the same key is used for both encryption and decryption.
[0098] Cloud Load Balancer (CLB) provides secure and fast traffic distribution services. Access traffic can be automatically distributed to multiple cloud servers in the cloud via CLB, expanding the system's service capacity and eliminating single points of failure. Load balancers support hundreds of millions of connections and tens of millions of concurrent users, easily handling high-traffic access and meeting business needs.
[0099] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.
[0100] Figure 1This illustration shows a flowchart of a long-connection task execution method according to an embodiment of this application. This method can be executed by any electronic device, such as a server. The server can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms, but is not limited to these. In this method, during the execution of the long-connection task, requests initiated by clients can be received. A client refers to a program that provides local services to an object, corresponding to the server (also known as the server-side). Except for some programs that run only locally, clients are generally installed on a terminal and work in conjunction with the server. The terminal can be a smartphone, tablet, laptop, desktop computer, smart speaker, smartwatch, in-vehicle device, etc.; the client can be a web browser, email client, instant messaging client, etc.
[0101] Specifically, such as Figure 1 As shown, the long-connection task execution method provided in this application embodiment includes the following steps S101-S103:
[0102] Step S101: Receive a target request from the first client via a long-lived connection channel; the target request carries target task information. The long-lived connection channel is associated with the first client based on the client's identity identifier.
[0103] The long-connection channel provides cloud-based data downlink capabilities encapsulated with IoT service protocols, offering clients the ability to subscribe to and publish messages, and supporting request-response models. Specifically, the long-connection channel has interfaces corresponding to connection requests, subscription requests, unsubscribe requests, and message publishing. In this embodiment, the client and server transmit data (such as requests initiated by the client and the information carried in those requests; or result information and published information sent by the server to the client) through the long-connection channel.
[0104] The target request sent by the first client can be a business request, a subscription request, an unsubscribe request, a publish request, etc. The target task information carried in the target request is related to the content of the request. For example, when the target request is a subscription request, the target task information may include object information, the client's identity identifier, topic information, etc. The content corresponding to the target task information will be described in detail for each different request scenario in subsequent embodiments. Optionally, the object can be a user using the client.
[0105] Among them, such as Figure 5As shown, the server includes a ClientManager. Specifically, the ClientManager manages client information in memory, using a dictionary map data structure for data storage. In the map, the key is the client's identity ID, and the value is client information, which may include the client ID, client connection information, client status, etc. Specifically, the correspondence between the long-lived connection channel and the first client can be determined based on the current correspondence between the server and clients stored in the ClientManager; that is, in this embodiment, the description focuses on long-lived connections between a single server and multiple clients. Specifically, after the first client establishes a long-lived connection with the server, the first client can send a target request to the server through the long-lived connection channel.
[0106] Step S102: Based on the target task information, call the target queue thread corresponding to the first client, and execute the target task corresponding to the target request through the target queue thread based on the pre-stored task information; wherein, the pre-stored task information is stored using a dictionary map data structure.
[0107] Specifically, such as Figure 5 As shown, the internal thread pool (WorkerPool) on the server side consists of n queues, where the size of n can be configured according to the cluster size, and this application does not limit it; the queues can store client request tasks, and each queue can correspond to one thread for task processing.
[0108] The target task information is sent synchronously to the server along with the target request when the first client initiates the target request. The server can determine the target queue corresponding to the first client based on the target task information (e.g., by distinguishing each queue by ID), and then put the target task corresponding to the target request into the target queue to call the corresponding target queue thread to execute the target task.
[0109] The pre-stored task information refers to information that is pre-stored during implementation. It can be information corresponding to the target task. For example, when the target request is a publish request, the pre-stored task information can include the association between pre-stored topic information and object information. Specifically, in this embodiment, a dictionary data structure is used to store the pre-stored task information. Storing information based on a map data structure helps to reduce the granularity of target task information processing, improve the QPS of request processing, and reduce client request latency.
[0110] Step S103: Based on the target request, the result information of the target queue thread executing the target task is fed back to the first client through the long connection channel.
[0111] Specifically, due to the different target tasks (which may be different processing tasks, or the same processing task but targeting different processing content; for example, the same subscription task but with different subscribed topics), the output information of the thread will be different when executing different target tasks. After the thread processes the client request task, the server can send the corresponding result information back to the first client through a long-lived connection channel.
[0112] This application's embodiments are based on a long-lived connection service implementation scheme, where a long-lived connection refers to a connection over which multiple data packets can be sent continuously. For example, after a webpage finishes loading, the connection used for data transmission between the client and the server is not closed, and when the client accesses the server again, it can use the already established connection.
[0113] To better illustrate the specific operations of the various embodiments of this application, the following will first be combined with... Figure 3 , Figure 5 and Figure 6 The overall architecture of the server in the embodiments of this application will be described.
[0114] In one embodiment, Figure 3 The overall architecture of the publish / subscribe service based on long-lived connections, as illustrated in an embodiment of this application, is shown. The first client 10 and server 20 can establish a long-lived connection via TCP or TCP SSL (a secure protocol implemented over TCP) and use the MQTT protocol for data transmission.
[0115] In one embodiment, Figure 5 The overall architecture of the server in an embodiment of this application is shown.
[0116] The server can consist of four components: ClientManager, Server-side internal thread pool (WorkerPool), TopicManager, and AuthChecker.
[0117] The client manager manages client information in memory, implemented using a map data structure. The key is the client's identifier (Client ID), and the value is client information; this information can include the client ID, client connection information, and client status. For example... Figure 5 As shown, client information can be managed through a client manager within the same server, allowing for the management of n client information entries, which is beneficial for increasing the number of long connections supported by the server in long-connection services.
[0118] The server-side internal thread pool (WorkerPool) consists of n queues of a preset length (e.g., 4096). The size of n can be configured according to the cluster size. The queues store client request tasks, and each queue has one thread to process the task. The client request can be hashed using a hash algorithm, modulo the preset value 4096 to calculate the corresponding queue, such as the queue ID. Specifically, the algorithm can be expressed as the following formula (1):
[0119] queueID=hash(ClientID)%n...Formula (1)
[0120] The server processes each client request serially by placing it into the corresponding queue based on the calculated queueID.
[0121] The TopicManager, implemented using multiple maps, manages client-side topic publishing / subscription information in memory. The server-side TopicManager optimizes the topic storage structure, using a segmented map data structure to store topics and subscribers (such as the objects initiating subscription requests). This storage scheme reduces the granularity of locks used by the server in processing subscription information, improving request processing QPS. The data structure used by the TopicManager is as follows: Figure 6 As shown. Optionally, the theme manager is implemented as follows:
[0122] (1) In each map (such as Map 0 or Map 1), the key is the string "topic" and the value is "Subscriber" (which can correspond to the object name or username), which saves the relationship between the topic and the subscriber. As shown in the figure, Map 0 currently stores the correspondence between topic 0 and subscriber 0, as well as topic 2 and subscriber 1.
[0123] (2) It can be composed of multiple maps (e.g., 1000 maps, the number of maps can be adjusted according to actual needs, and this application embodiment does not limit this). By subdividing the lock granularity through multiple maps, the processing speed of the topic manager can be improved and the request QPS can be increased.
[0124] The authorization verification module (AuthChecker) performs authorization verification on client-initiated requests (such as connection, subscription, and publish requests). Figure 5 As shown, the internal data structure of the permission verification module is a map, where the key is object information (such as object name, username), and the value is the hash value password, the list of subscribed information topics, the list of published information topics, etc. corresponding to the key.
[0125] The following describes the specific process of handling connection requests initiated by the client in this embodiment of the application.
[0126] In a feasible embodiment, before receiving the target request from the first client through the long connection channel in step S101, steps S001-S003 are further included:
[0127] Step S001: Receive the connection request initiated by the first client through the message queue telemetry transmission protocol, establish a long connection of transmission control protocol or secure socket layer, so as to transmit information through the long connection channel corresponding to the first client; the connection request carries connection information.
[0128] Specifically, the first client can initiate a connection request with the server via the MQTT protocol to establish a TCP / TCP SSL long connection. After the long connection is established, the server can use the long connection channel to transmit information with the first client.
[0129] Step S002: Perform a verification operation based on the first object information and the preset password information in the connection information.
[0130] Specifically, the server can obtain the first object information (such as the object name username) and the preset password information (such as the password password) from the connection information (such as the MQTT connection packet). Then, the server can perform a sha256 hash operation on the password to calculate the target hash value (sha256Password). The server then matches the first object information with the pre-stored object information (such as the object name dbUsername) in the backend database, and matches the target hash value (dbSha256Password) with the pre-stored hash value in the backend database. The hash algorithm can be expressed as the following formula (2):
[0131] sha256Password=sha256(password)...Formula (2)
[0132] In short, a database can be viewed as an electronic filing cabinet, a place to store electronic files, where operations such as adding, querying, updating, and deleting data can be performed. A database is a collection of data stored together in a certain way, which can be shared by multiple users, has as little redundancy as possible, and is independent of applications.
[0133] Step S003: If the first object information matches the pre-stored object information, and the target hash value matches the pre-stored hash value corresponding to the first client, then the verification is successful and the connection information is stored using a map data structure with the identity identifier of the first client as the key; otherwise, the verification is failed and the long connection is disconnected; wherein, the target hash value is determined by hashing based on the preset password information.
[0134] Specifically, if the first object information (i.e., the object name "username") exists in the database, and the corresponding pre-stored hash value password in the database is equal to the target hash value "sha256Password", then the verification is successful (i.e., the permission verification module can determine that the first client has connection permission). Otherwise, the verification fails, and the client connection is disconnected (as in step S001 above for TCP / TCP SSL long connections). After the connection permission verification is successful, the client manager on the server can save the client's identity identifier (Client ID) and connection information.
[0135] The following describes the specific operations related to the server's internal thread pool in the embodiments of this application.
[0136] In a feasible embodiment, step S102, which involves invoking the target queue thread corresponding to the first client based on the target task information, includes the following steps A1-A2:
[0137] Step A1: Perform a hash operation based on the identity identifier of the first client in the target task information and the number of queues included in the thread pool to determine the target queue identifier corresponding to the first client.
[0138] Step A2: Invoke the corresponding target queue thread based on the target queue identifier, so as to process each request in the target queue thread serially.
[0139] Specifically, such as Figure 5 As shown, the hash algorithm described in formula (1) above can be used to determine the corresponding target queue identifier based on the identity of the first client and the number of queues included in the internal thread pool. After determining the target queue identifier, the target task can be placed into the corresponding queue, and the corresponding target queue thread can execute the tasks contained in the queue in a serial processing manner.
[0140] The following describes the specific process of permission verification related to publishing or subscribing in the embodiments of this application.
[0141] In a feasible embodiment, the target task information includes first object information and topic information; step S102, in which the target queue thread executes the target task corresponding to the target request based on the pre-stored task information, includes steps B1-B3:
[0142] Step B1: The target queue thread queries the pre-stored task information to see if the topic information is included in the topic list corresponding to the first object information.
[0143] Step B2: If yes, when executing the target task corresponding to the target request, output result information including task response information.
[0144] Step B3: If not, when executing the target task corresponding to the target request, output result information including task execution failure information.
[0145] The pre-stored task information includes the correspondence between topic information and object information stored using at least one map data structure; the topic information is the traversal key, and the object information is the traversal value.
[0146] Regarding steps B1-B3 performed in the above embodiments, the specific implementation details of each operation step will be described below, taking the target requests as subscription requests and publication requests.
[0147] The target request is a subscription request:
[0148] After the first client successfully connects to the server (that is, the server can provide a long connection service for the first client), it can send the target task information (which can be subscription information) in the subscription request. The subscription information can be presented in the form of an MQTT subscription packet. The subscription packet can contain the first object information, the identity of the first client, the message ID, and the topic information that the first client requests to subscribe to.
[0149] Upon receiving the MQTT subscription packet, the server parses it to obtain the identity and topic information of the first client. Then, the server retrieves the topic permission information corresponding to the client's object name (username) from the database. If the database's list of topics that username can subscribe to contains the topic information from this subscription packet, the verification passes; otherwise, the verification fails. Optionally, the verification process can be performed in the permission verification module.
[0150] When the verification passes, step B2 outputs result information including task response information, where the task response information may be a subscription response packet. At this time, step S103 can be executed to transmit the task response information to the first client, so that the first client can realize that it has successfully subscribed to the relevant topic.
[0151] When the verification fails, step B3 outputs result information including task execution failure information, which may include subscription failure error code; at this time, step S103 can be executed to transmit the task execution failure information to the first client, so that the first client can be aware of the failure to subscribe to the relevant topic.
[0152] The target request is a publish request:
[0153] The first client can send the target task information (which can be information related to the publish request) in the publish request. The target task information can be presented in the form of an MQTT publish packet. The publish packet can contain publish information (such as message body content payload), message ID, the identity of the first client, the first object information, and the topic information requested by the first client for publishing.
[0154] Upon receiving the MQTT publish packet, the server parses it to obtain the identity and topic information of the first client. Then, the server retrieves the publishing topic permission information corresponding to the client's object name (username) from the database. If the database's list of topics that username can publish to contains the topic information from this publish packet, the verification passes; otherwise, the verification fails. Optionally, the verification process can be performed in the permission verification module.
[0155] When the verification passes, step B2 outputs result information including task response information, where the task response information may be a published response packet; at this time, step S103 can be executed to transmit the task response information to the first client, so that the first client can realize that the relevant published information has been successfully published.
[0156] When the verification fails, step B3 outputs result information including task execution failure information, which may include a publication failure error code. At this time, step S103 can be executed to transmit the task execution failure information to the first client, so that the first client can be aware of the failure to publish the information.
[0157] In one feasible embodiment, the target request is a subscription request or a publish request.
[0158] When the target request is a subscription request and task response information is output, step S102, executing the target task, includes step C1:
[0159] Step C1: Store the correspondence between the first object information and the topic information in the pre-stored task information.
[0160] Specifically, once it is determined that the first client has subscription permissions, the currently subscribed topic information and the subscription relationship between the first client can be saved in the TopicManager.
[0161] When the target request is a publish request and task response information is output, step S102, executing the target task, includes steps D1-D3:
[0162] Step D1: Obtain the publication information and topic information from the target task information.
[0163] Specifically, when it is determined that the first client has publishing permissions, the publishing information (message body content payload) and the topic information corresponding to the publishing information can be obtained from the target task information.
[0164] Step D2: Query the pre-stored task information for second object information that corresponds to the topic information.
[0165] Specifically, since the pre-stored task information (managed by the Topic Manager) contains the subscription relationships between each topic and each client, at least one second object (Subscriber) of the topic (the topic information in the target task information carried in the publishing request) can be queried in the Topic Manager.
[0166] Step D3: Send the publishing information to the second client corresponding to the second object information.
[0167] Specifically, the server can obtain the corresponding second client information based on the second object information, and then connect with the second client to send the publishing information to the second client; that is, the publishing information to be published by the first client is transmitted to at least one second client that has a subscription relationship with the topic information of the publishing information, thus completing the message publishing process.
[0168] In a feasible embodiment, this application also provides a data encryption scheme. Specifically, when the received publishing information is encrypted information encrypted by the first client using the Advanced Encryption Standard (AES) algorithm, step D1, obtaining the publishing information from the target task information, includes steps D11-D12:
[0169] Step D11: Obtain the encrypted information from the target task information.
[0170] Step D12: Decrypt the encrypted information using the AES algorithm to obtain the published information.
[0171] Specifically, during the information transmission between the server and client, the payload field in the MQTT protocol is encrypted using AES. The encrypted message can be decrypted after acquisition, effectively ensuring data transmission security. Optionally, the encryption process can occur between the first client and the server; for example, the first client encrypts the published information and transmits it to the server, or the server encrypts the published information and transmits it to the second client. The decryption process can occur between the server and the second client; for example, the server decrypts the published information encrypted by the first client, or the second client decrypts the published information encrypted by the server. Alternatively, the server can choose not to participate in the encryption or decryption process, but instead directly transmit the published information encrypted by the first client to the second client, allowing the second client to decrypt the published information encrypted by the first client.
[0172] The encryption algorithm involved can be expressed as shown in the following formula (3):
[0173] encryptedPayload=AES.Encrypt(payload)...Formula (3)
[0174] The decryption algorithm involved can be expressed as shown in the following formula (4):
[0175] decryptedPayload=AES.Decrypt(payload)...Formula (4)
[0176] In a data encryption scheme, the server in this application embodiment supports MQTT SSL connection, that is, the client connects via MQTT SSL and performs secure communication after the connection is successful; it can be understood that using SSL connection achieves end-to-end encryption, which can effectively improve data security.
[0177] In a feasible embodiment, this application also provides a data compression scheme. Specifically, when the received publishing information is compressed information compressed by the first client using a compression algorithm, step D1, which involves obtaining the publishing information from the target task information, includes steps D13-D14:
[0178] Step D13: Obtain the compressed information from the target task information.
[0179] Step D14: Decompress the compressed information using the compression algorithm to obtain the published information.
[0180] Specifically, before sending the publish request, the first client compresses the publish information (payload field content) in the target task information (PublishPacket), which can be done using the gzip compression algorithm. After receiving the target task information from the first client, the server can decompress the publish information.
[0181] The compression algorithm involved can be expressed as shown in the following formula (5):
[0182] compressedPayload=GZIP.Encode(payload)...Formula (5)
[0183] The decompression algorithm involved can be expressed as shown in the following formula (6):
[0184] payload=GZIP.Decode(compressedPayload)......Formula (6)
[0185] The embodiments of this application can effectively solve the problem of high overall bandwidth consumption of the service when there are frequent communication requests between the client and the server. By using a data compression scheme, the length of the request body can be effectively reduced, thereby reducing the bandwidth consumption ratio.
[0186] The following describes the specific process of handling client offline requests in the embodiments of this application.
[0187] In a feasible embodiment, the long-connection task execution method further includes steps E1-E2:
[0188] Step E1: Receive the offline request sent by the first client; the offline request carries disconnection information.
[0189] Step E2: Close the long connection channel corresponding to the first client based on the disconnection information, and clear the information related to the first client.
[0190] Specifically, the first client can send disconnection information (such as an MQTT DisconnectPacket) to the server in its offline request. When the server receives the disconnection information from the first client, it closes the first client's long connection, and simultaneously notifies the ClientManager component to clear information related to the first client, and notifies the TopicManager component to clear topic and subscription information related to the first client.
[0191] like Figure 2As shown in the figure, this application embodiment also provides a service allocation method, which can be applied to load balancing devices, including step S201:
[0192] Step S201: Based on the load balancing strategy, distribute the received client connections to the corresponding servers respectively; the server cluster where the servers are located includes at least one server.
[0193] The server executes the long-connection task execution method shown in any of the above embodiments.
[0194] Specifically, such as Figure 4 As shown, a relay device, such as a load balancer (CLB), can be set up between the client and the server. The client connects to the CLB, and the CLB automatically distributes traffic to the backend server.
[0195] Multiple backend service nodes (hmq_broker) can be deployed on the backend server providing the service (e.g., ...). Figure 4 The server is designated as server 1, server 2, ..., server n. All backend service nodes are mounted on the CLB, and the CLB handles traffic distribution. The CLB can also perform load balancing based on the number of connections to each backend server, ensuring a balanced number of connections across all backend servers.
[0196] It is understood that the load balancing device manages a server cluster consisting of n servers. For this server cluster, embodiments of this application provide cluster deployment schemes for cluster expansion and contraction.
[0197] Cluster expansion solution: Increase the number of backend service nodes deployed and mount the newly added service nodes to the CLB to achieve cluster expansion.
[0198] Cluster scaling down solution: Reduce the number of backend service nodes deployed. CLB can automatically identify offline service nodes to achieve cluster scaling down.
[0199] In this embodiment, the cluster deployment scheme provided supports horizontal scaling up or down of the server, which is beneficial to improving the scalability and availability of the service.
[0200] In one feasible embodiment, such as Figure 7 As shown in the embodiments of this application, a single message transmission by the client (i.e., within a single long connection) may include four processes: initiating a connection, initiating a subscription request, initiating a publish request, and disconnecting the connection; the server's processing of the above four processes can be referred to the specific description of the corresponding embodiments above.
[0201] In one feasible embodiment, the client's identity identifier referred to in this application embodiment can be determined based on the client address, or it can be determined based on other client-related information; such as... Figure 5 As shown, when the client manager saves client information via a map, it can assign a corresponding identity identifier to each client. For example, client A can be recorded as "client 1," and client B as "client 2," etc. It can be understood that each client has its own independent identity identifier. Based on this, all client-related information stored or retrieved by the server can be associated with the client's identity identifier; for example, based on the retrieved client identity identifier, the corresponding object information (such as the object name "username") can be determined.
[0202] Optionally, the data involved in the method provided in this application (such as target task information, client identity identifiers, user information, connection information, password information, topic information, etc.) can be stored in a blockchain. The blockchain referred to in this application is a novel application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. A blockchain is essentially a decentralized database, a chain of data blocks linked using cryptographic methods. Each data block contains a certain amount of processed data used to verify the validity of its information (anti-counterfeiting) and to generate the next block. A blockchain can include a blockchain underlying platform, a platform product service layer, and an application service layer.
[0203] The operation of this application's embodiments also involves cloud technology. Cloud technology refers to a hosting technology that unifies hardware, software, network, and other resources within a wide area network (WAN) or local area network (LAN) to achieve data computation, storage, processing, and sharing. Cloud technology is a general term for network technology, information technology, integration technology, management platform technology, and application technology applied to cloud computing business models. It can form resource pools, be used on demand, and is flexible and convenient. Cloud computing technology will become a crucial support. Backend services of technical network systems require substantial computing and storage resources, such as video websites, image websites, and many portal websites. With the rapid development and application of the internet industry, every item may have its own identification mark in the future, requiring transmission to a backend system for logical processing. Data at different levels will be processed separately, and various industry data will require robust system support, which can only be achieved through cloud computing.
[0204] It is understood that the calculations performed in the above embodiments (such as the calculations involved in the permission verification process, data encryption process, and data compression process) can be implemented using cloud computing. Cloud computing refers to the delivery and usage model of IT infrastructure, which means obtaining the required resources in an on-demand and easily scalable manner through the network; in a broader sense, cloud computing refers to the delivery and usage model of services, which means obtaining the required services in an on-demand and easily scalable manner through the network. Such services can be IT and software, Internet-related, or other services. Cloud computing is the product of the development and integration of traditional computer and network technologies such as grid computing, distributed computing, parallel computing, utility computing, network storage technologies, virtualization, and load balancing. With the development of the Internet, real-time data streams, and the diversification of connected devices, as well as the driving force of demands such as search services, social networks, mobile commerce, and open collaboration, cloud computing has developed rapidly. Unlike previous parallel and distributed computing, the emergence of cloud computing will, conceptually, drive a revolutionary change in the entire Internet model and enterprise management model.
[0205] Furthermore, in the above embodiments, the server-side storage of information (such as client information, subscription relationships, etc.) can be implemented using cloud storage. Cloud storage is a new concept that extends and develops from the concept of cloud computing. A distributed cloud storage system (hereinafter referred to as a storage system) refers to a storage system that uses cluster applications, grid technology, and distributed storage file systems to aggregate a large number of storage devices of various types (storage devices are also called storage nodes) in the network through application software or application interfaces to work together and jointly provide data storage and business access functions. Currently, the storage method of the storage system is as follows: create logical volumes. When creating a logical volume, allocate physical storage space for each logical volume. This physical storage space may be composed of the disks of a storage device or several storage devices. The client stores data on a logical volume, that is, stores the data on the file system. The file system divides the data into many parts, each part being an object. The object not only contains data but also additional information such as data identifiers (ID, ID entity). The file system writes each object into the physical storage space of the logical volume, and the file system records the storage location information of each object. Thus, when the client requests access to data, the file system can allow the client to access the data based on the storage location information of each object. The process by which a storage system allocates physical storage space to a logical volume is as follows: the physical storage space is pre-divided into strips according to the capacity estimate of the objects stored in the logical volume (this estimate often has a large margin relative to the actual capacity of the objects to be stored) and the grouping of Redundant Array of Independent Disks (RAID). A logical volume can be understood as a strip, thus allocating physical storage space to the logical volume.
[0206] It should be noted that, in the optional embodiments of this application, the data related to object information, etc., requires the permission or consent of the object when applied to specific products or technologies. Furthermore, the collection, use, and processing of this data must comply with the relevant laws, regulations, and standards of the relevant countries and regions. In other words, if the embodiments of this application involve data related to an object, this data must be obtained with the object's authorization and consent, and in accordance with the relevant laws, regulations, and standards of the country and region.
[0207] Based on the same principle as the above-described long-connection task execution method embodiments, this application also provides a long-connection task execution device, such as... Figure 8 As shown, the long-connection task execution device 100 may include a receiving module 101, a calling module 102, and a feedback module 103.
[0208] The receiving module 101 is used to receive a target request sent by a first client through a long connection channel; the target request carries target task information; the calling module 102 is used to call a target queue thread corresponding to the first client based on the target task information, and execute the target task corresponding to the target request through the target queue thread based on pre-stored task information; wherein the pre-stored task information is stored using a dictionary data structure; the feedback module 103 is used to feed back the result information of the target task executed by the target queue thread to the first client through the long connection channel based on the target request; wherein the long connection channel and the first client are associated based on the client's identity identifier.
[0209] In one feasible embodiment, the receiving module 101 is configured to, before performing the task of receiving the target request issued by the first client through the long connection channel, further be configured to:
[0210] The system receives a connection request initiated by the first client via a message queue telemetry transport protocol, establishes a long connection using a transport control protocol or secure socket layer, and transmits information through the long connection channel corresponding to the first client; the connection request carries connection information.
[0211] A verification operation is performed based on the first object information in the connection information and the preset password information;
[0212] If the first object information matches the pre-stored object information, and the target hash value matches the corresponding pre-stored hash value, then the verification is successful and the connection information is stored using a dictionary data structure with the identity identifier of the first client as the key; otherwise, the verification is failed and the long connection is disconnected; wherein, the target hash value is determined based on the preset password information through hash operation.
[0213] In a feasible embodiment, when the invocation module 102 is used to execute the invocation of the target queue thread corresponding to the first client based on the target task information, it is specifically used for:
[0214] A hash operation is performed based on the identity identifier of the first client in the target task information and the number of queues included in the thread pool to determine the target queue identifier corresponding to the first client;
[0215] The corresponding target queue thread is invoked based on the target queue identifier, so that each request in the target queue thread is processed serially through the target queue thread.
[0216] In a feasible embodiment, the target task information includes first object information and topic information; when the calling module 102 is used to execute the target task corresponding to the target request based on the pre-stored task information through the target queue thread, it is specifically used for:
[0217] The target queue thread queries the pre-stored task information to see if the topic information is included in the topic list corresponding to the first object information.
[0218] If so, when executing the target task corresponding to the target request, output result information including task response information;
[0219] If not, when executing the target task corresponding to the target request, output result information including task execution failure information;
[0220] The pre-stored task information includes the correspondence between topic information and object information stored using at least one dictionary data structure; the topic information is the traversal key, and the object information is the traversal value.
[0221] In one feasible embodiment, the target request is a subscription request or a publish request;
[0222] When the target request is a subscription request and the task response information is output, the calling module 102, when executing the target task, is specifically used to: store the correspondence between the first object information and the topic information in the pre-stored task information;
[0223] When the target request is a publishing request and outputs task response information, the calling module 102, when executing the target task, specifically performs the following: obtaining publishing information and topic information from the target task information; querying second object information that corresponds to the topic information from the pre-stored task information; and sending the publishing information to the second client corresponding to the second object information.
[0224] In a feasible embodiment, when the received publishing information is encrypted information encrypted by the first client using the Advanced Encryption Standard (AES) algorithm, the calling module 102, when used to obtain the publishing information in the target task information, specifically performs the following:
[0225] Obtain the encrypted information from the target task information;
[0226] The published information is obtained by decrypting the encrypted information using the Advanced Encryption Standard algorithm.
[0227] In a feasible embodiment, when the received publishing information is compressed information compressed by the first client using a compression algorithm, the calling module 102, when used to obtain the publishing information in the target task information, specifically performs the following:
[0228] Obtain the compression information from the target task information;
[0229] The compressed information is decompressed using the compression algorithm to obtain the published information.
[0230] In one feasible embodiment, the device further includes a disconnection module for:
[0231] Receive a disconnection request from the first client; the disconnection request carries disconnection information;
[0232] Based on the disconnection information, the long connection channel corresponding to the first client is closed, and information related to the first client is cleared.
[0233] Based on the same principles as the above-described service allocation method embodiments, this application also provides a service allocation apparatus, such as... Figure 9 As shown, the service distribution device 200 may include a distribution module 201.
[0234] The distribution module 201 is used to distribute the received client connections to the corresponding servers based on a load balancing strategy; the server cluster where the servers are located includes at least one server; wherein the server executes the long connection task execution method in the above embodiment.
[0235] The apparatus in this application embodiment can execute the method provided in this application embodiment, and the implementation principle is similar. The actions performed by each module in the apparatus of each embodiment of this application correspond to the steps in the method of each embodiment of this application. For detailed functional descriptions of each module of the apparatus, please refer to the descriptions in the corresponding methods shown above, which will not be repeated here.
[0236] This application provides an electronic device, including a memory, a processor, and a computer program stored in the memory. The processor executes the computer program to implement the steps of a long-connection task execution method. Compared with related technologies:
[0237] On the one hand, this application provides a long-connection task execution method that can be applied to a server. Specifically, when a long-connection channel and a first client establish a corresponding relationship based on the client's identity identifier, that is, when a long connection is established between the first client and the server, a target request sent by the first client can be received through the long-connection channel. This target request carries target task information. Then, based on the target task information, the target queue thread corresponding to the first client is invoked to execute the target task corresponding to the target request. On this basis, based on the target request, the result information of the target task executed by the target queue thread based on the pre-stored task information is fed back to the first client through the long connection. Compared to existing technologies, this application adjusts the implementation of the task execution method from the overall architecture. First, the long connection between the client and the server is established based on the client's identity. Then, the target task corresponding to the target request initiated by the client is executed in the form of a call queue thread. By using a dictionary data structure to store the pre-stored task information for executing the target task, the number of long connections that a single server can support is increased by providing a high-performance architecture. Furthermore, by using a dictionary data structure to store the pre-stored task information for executing the target task, the lock granularity of processing target task information is reduced, the query rate per second of request processing is increased, thereby reducing client request latency and providing high-performance communication.
[0238] On the other hand, this application also provides a service allocation method that can be applied to a load balancing device managing individual servers. The server cluster managed by the load balancing device includes at least one server. Specifically, based on a load balancing strategy, each received client connection is distributed to its corresponding server, and each server can execute the long-connection task execution method shown in the above embodiment. The implementation of this application can deploy multiple servers in the server cluster, corresponding to multiple backend service nodes, and mount each backend service node on the load balancing device. The load balancing device distributes traffic according to the load balancing strategy, which is beneficial for balancing the number of long connections supported by each individual server. Furthermore, by increasing or decreasing the number of servers deployed in the server cluster, cluster expansion or contraction can be achieved, which is beneficial for improving service scalability and availability.
[0239] In one alternative embodiment, an electronic device is provided, such as Figure 10 As shown, Figure 10The illustrated electronic device 4000 includes a processor 4001 and a memory 4003. The processor 4001 and the memory 4003 are connected, for example, via a bus 4002. Optionally, the electronic device 4000 may further include a transceiver 4004, which can be used for data interaction between the electronic device and other electronic devices, such as sending and / or receiving data. It should be noted that in practical applications, the transceiver 4004 is not limited to one type, and the structure of the electronic device 4000 does not constitute a limitation on the embodiments of this application.
[0240] Processor 4001 may be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It can implement or execute the various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 4001 may also be a combination that implements computational functions, such as including one or more microprocessor combinations, a combination of a DSP and a microprocessor, etc.
[0241] Bus 4002 may include a pathway for transmitting information between the aforementioned components. Bus 4002 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, etc. Bus 4002 can be divided into address bus, data bus, control bus, etc. For ease of representation, Figure 10 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0242] The memory 4003 may be ROM (Read Only Memory) or other types of static storage devices capable of storing static information and instructions, RAM (Random Access Memory) or other types of dynamic storage devices capable of storing information and instructions, or EEPROM (Electrically Erasable Programmable Read Only Memory), CD-ROM (Compact Disc Read Only Memory) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media, other magnetic storage devices, or any other medium capable of carrying or storing computer programs and capable of being read by a computer, without limitation herein.
[0243] The memory 4003 stores computer programs that execute embodiments of this application, and its execution is controlled by the processor 4001. The processor 4001 executes the computer programs stored in the memory 4003 to implement the steps shown in the foregoing method embodiments.
[0244] Electronic devices include, but are not limited to: servers, terminals, and vehicle-mounted devices.
[0245] This application provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, it can implement the steps and corresponding content of the aforementioned method embodiments.
[0246] This application also provides a computer program product, including a computer program that, when executed by a processor, can implement the steps and corresponding content of the aforementioned method embodiments.
[0247] The terms "first," "second," "third," "fourth," "1," "2," etc. (if present) in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in a sequence other than that shown in the figures or text.
[0248] It should be understood that although the flowcharts in the embodiments of this application use arrows to indicate the various operation steps, the order in which these steps are implemented is not limited to the order indicated by the arrows. Unless explicitly stated herein, in some implementation scenarios of the embodiments of this application, the implementation steps in each flowchart can be executed in other orders as required. Furthermore, some or all of the steps in each flowchart may include multiple sub-steps or multiple stages based on the actual implementation scenario. Some or all of these sub-steps or stages can be executed at the same time, and each sub-step or stage can also be executed at different times. In scenarios where the execution times are different, the execution order of these sub-steps or stages can be flexibly configured according to requirements, and the embodiments of this application do not limit this.
[0249] The above description is only an optional implementation method for some implementation scenarios of this application. It should be noted that for those skilled in the art, other similar implementation methods based on the technical concept of this application without departing from the technical concept of this application also fall within the protection scope of the embodiments of this application.
Claims
1. A method for executing tasks using long-lived connections, characterized in that, include: Receive the target request sent by the first client through a long connection channel; The target request carries target task information, which includes first object information and topic information. The long connection channel is associated with the first client based on the client's identity. Based on the identity identifier of the first client in the target task information, the target queue thread corresponding to the first client is invoked, and the target queue thread executes the target task corresponding to the target request based on the pre-stored task information; wherein, the pre-stored task information is stored in a dictionary data structure, and the pre-stored task information includes the correspondence between topic information and object information stored in a segmented dictionary data structure; Based on the target request, the result information of the target queue thread executing the target task is fed back to the first client through the long connection channel; The step of executing the target task corresponding to the target request through the target queue thread based on pre-stored task information includes: The target queue thread queries the pre-stored task information to see if the topic information is included in the topic list corresponding to the first object information, so as to perform permission verification on the target request of the first client, and execute the target task corresponding to the target request based on the verification result; The target request is either a subscription request or a publish request; When the target request is a subscription request and task response information is output, executing the target task based on the verification result includes: when it is determined that the first client has subscription permission, storing the correspondence between the first object information and the topic information in the pre-stored task information; When the target request is a publishing request and task response information is output, executing the target task based on the verification result includes: when it is determined that the first client has publishing permission, obtaining publishing information and topic information from the target task information; querying the pre-stored task information for second object information that corresponds to the topic information; and sending the publishing information to the second client corresponding to the second object information.
2. The method according to claim 1, characterized in that, Before receiving the target request from the first client via the long connection channel, the method further includes: The system receives a connection request initiated by the first client via a message queue telemetry transport protocol, establishes a long connection using a transport control protocol or secure socket layer, and transmits information through the long connection channel corresponding to the first client; the connection request carries connection information. A verification operation is performed based on the first object information in the connection information and the preset password information; If the first object information matches the pre-stored object information, and the target hash value matches the pre-stored hash value corresponding to the first client, then the verification is successful and the connection information is stored using a dictionary data structure with the identity identifier of the first client as the key; otherwise, the verification is failed and the long connection is disconnected; wherein, the target hash value is determined based on the preset password information through hash operation.
3. The method according to claim 1, characterized in that, The step of invoking the target queue thread corresponding to the first client based on the identity identifier of the first client in the target task information includes: A hash operation is performed based on the identity identifier of the first client in the target task information and the number of queues included in the thread pool to determine the target queue identifier corresponding to the first client; The corresponding target queue thread is invoked based on the target queue identifier, so that each request in the target queue thread is processed serially through the target queue thread.
4. The method according to claim 1, characterized in that, The step of executing the target task corresponding to the target request based on the verification result includes: If so, when executing the target task corresponding to the target request, output result information including task response information; If not, when executing the target task corresponding to the target request, output result information including task execution failure information.
5. The method according to claim 1, characterized in that, The subject information is the traversal key, and the object information is the traversal value.
6. The method according to claim 1, characterized in that, When the received publishing information is encrypted information encrypted by the first client using the Advanced Encryption Standard algorithm, the step of obtaining the publishing information from the target task information includes: Obtain the encrypted information from the target task information; The published information is obtained by decrypting the encrypted information using the Advanced Encryption Standard algorithm.
7. The method according to claim 1, characterized in that, When the received publishing information is compressed information obtained by the first client using a compression algorithm, the step of obtaining the publishing information from the target task information includes: Obtain the compression information from the target task information; The compressed information is decompressed using the compression algorithm to obtain the published information.
8. The method according to claim 1, characterized in that, The method also includes: Receive a disconnection request from the first client; the disconnection request carries disconnection information; Based on the disconnection information, the long connection channel corresponding to the first client is closed, and information related to the first client is cleared.
9. A long-connection task execution device, characterized in that, include: The receiving module is used to receive the target request sent by the first client through a long connection channel; The target request carries target task information, which includes first object information and topic information. The long connection channel is associated with the first client based on the client's identity. The invocation module is used to invoke the target queue thread corresponding to the first client based on the identity identifier of the first client in the target task information, and to execute the target task corresponding to the target request through the target queue thread based on the pre-stored task information; wherein, the pre-stored task information is stored in a dictionary data structure, and the pre-stored task information includes the correspondence between topic information and object information stored in a segmented dictionary data structure; The feedback module is used to feed back the result information of the target queue thread executing the target task to the first client through the long connection channel based on the target request; Specifically, when the calling module is used to execute the target task corresponding to the target request based on the pre-stored task information through the target queue thread, it is used for: The target queue thread queries the pre-stored task information to see if the topic information is included in the topic list corresponding to the first object information, so as to perform permission verification on the target request of the first client, and execute the target task corresponding to the target request based on the verification result; The target request is either a subscription request or a publish request; When the target request is a subscription request and the task response information is output, the calling module, when executing the target task based on the verification result, is specifically used to: when it is determined that the first client has subscription permission, store the correspondence between the first object information and the topic information in the pre-stored task information; When the target request is a publication request and outputs task response information, the calling module, when executing the target task based on the verification result, specifically performs the following: obtaining publication information and topic information from the target task information; querying the pre-stored task information for second object information that corresponds to the topic information; and sending the publication information to the second client corresponding to the second object information.
10. The apparatus according to claim 9, characterized in that, The receiving module is used, before receiving the target request sent by the first client through the long connection channel, to further: Receive the connection request initiated by the first client through the message queue telemetry transmission protocol, establish a long connection of transmission control protocol or secure socket layer, and transmit information through the long connection channel corresponding to the first client; The connection request carries connection information; A verification operation is performed based on the first object information in the connection information and the preset password information; If the first object information matches the pre-stored object information, and the target hash value matches the pre-stored hash value corresponding to the first client, then the verification is successful and the connection information is stored using a dictionary data structure with the identity identifier of the first client as the key; otherwise, the verification is failed and the long connection is disconnected; wherein, the target hash value is determined based on the preset password information through hash operation.
11. The apparatus according to claim 9, characterized in that, When the calling module is used to call the target queue thread corresponding to the first client based on the identity identifier of the first client in the target task information, it is specifically used for: A hash operation is performed based on the identity identifier of the first client in the target task information and the number of queues included in the thread pool to determine the target queue identifier corresponding to the first client; The corresponding target queue thread is invoked based on the target queue identifier, so that each request in the target queue thread is processed serially through the target queue thread.
12. The apparatus according to claim 9, characterized in that, When the calling module is used to execute the target task corresponding to the target request based on the verification result, it is specifically used for: If so, when executing the target task corresponding to the target request, output result information including task response information; If not, when executing the target task corresponding to the target request, output result information including task execution failure information.
13. The apparatus according to claim 9, characterized in that, The subject information is the traversal key, and the object information is the traversal value.
14. The apparatus according to claim 9, characterized in that, When the received publishing information is encrypted information encrypted by the first client using the Advanced Encryption Standard algorithm, the calling module, when used to obtain the publishing information in the target task information, is specifically used for: Obtain the encrypted information from the target task information; The published information is obtained by decrypting the encrypted information using the Advanced Encryption Standard algorithm.
15. The apparatus according to claim 9, characterized in that, When the received publishing information is compressed information obtained by the first client using a compression algorithm, the calling module, when used to obtain the publishing information in the target task information, specifically performs the following: Obtain the compression information from the target task information; The compressed information is decompressed using the compression algorithm to obtain the published information.
16. The apparatus according to claim 9, characterized in that, The device also includes a disconnection module for: Receive a disconnection request from the first client; the disconnection request carries disconnection information; Based on the disconnection information, the long connection channel corresponding to the first client is closed, and information related to the first client is cleared.
17. An electronic device, characterized in that, It includes a memory and a processor, wherein the memory stores a computer program, and the processor, when running the computer program, performs the method according to any one of claims 1 to 8.
18. A computer-readable storage medium, characterized in that, The storage medium stores a computer program, which, when run in a processor, is used to perform the method according to any one of claims 1 to 8.
19. A computer program product, characterized in that, The computer product includes a computer program that, when executed by a processor, implements the method of any one of claims 1 to 8.
Citation Information
Patent Citations
Service control method and device
CN112367297A