Service request processing method and related device
Through the processing method of object coroutine and coroutine message queue, the problem of concurrency competition and thread switching in multi-thread architecture is solved, the request processing efficiency and response speed of the server are improved, and the stability of the business system is enhanced.
Patent Information
- Application Number
- CN202310483435.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-28
- Publication Date
- 2024-10-29
AI Technical Summary
In large-scale concurrency scenarios, there are concurrency competition and thread switching problems in multi-threaded or multi-process server architectures, which affect message processing capabilities, and coroutines or asynchronous I/O are less efficient when processing highly CPU-intensive tasks.
The processing methods of object coroutine and coroutine message queue are used to isolate business requests of different target objects through object coroutine, which reduces resource occupation and improves processing efficiency. The coroutine message queue is used to realize the isolation and serialization of requests.
It effectively solves the problems of concurrency competition and thread switching, improves the server's request processing efficiency and response speed, and enhances the stability and concurrent processing capabilities of the business system.
Smart Images

Figure CN118860618A_ABST
Abstract
Description
Background Art
[0002] With the continuous development of computer technology, background servers need to process a large number of client requests. How background servers handle massive requests is the key to ensuring service quality.
[0003] In related technologies, the backend server usually adopts an architecture implemented by multi-threading, multi-process, coroutine and asynchronous I / O.
[0004] In an architecture based on multi-threading or multi-process implementation, after receiving each service request, each service request is processed by multiple threads or multiple processes. However, in large-scale concurrent scenarios, multiple processes need to access a shared memory at the same time. Therefore, there are problems such as concurrency competition and thread switching between multiple processes, which seriously affect the message processing capability of the server.
[0005] In an architecture based on coroutines or asynchronous input / output (I / O), after receiving each service request, each service request is processed by a single thread. However, coroutines or asynchronous I / O have poor processing capabilities for high central processing unit (CPU) intensive tasks. High CPU intensive tasks refer to tasks with a large number of calculations. When the service request is a high CPU intensive task, it is difficult to efficiently process a large number of service requests through a single thread. Summary of the invention
[0006] The embodiments of the present application provide a service request processing method and related devices to improve service request processing capabilities and enhance service system stability.
[0007] In a first aspect, an embodiment of the present application provides a service request processing method, including:
[0008] receiving service requests for target services, each service request carrying an object identifier of a target object;
[0009] For each of the business requests, the following operations are respectively performed: based on an object identifier carried by a business request, an object coroutine pre-created for the carried object identifier is used as the object coroutine corresponding to the business request, and the business request is added to the coroutine message queue corresponding to the object coroutine;
[0010] Using the determined object coroutines, respectively obtain and process at least one corresponding business request from the corresponding coroutine message queue.
[0011] In a second aspect, an embodiment of the present application provides a service request processing device, including:
[0012] A receiving unit, used to receive each service request for a target service, each service request carrying an object identifier of a target object;
[0013] The enqueue unit is used to perform the following operations for each of the business requests: based on an object identifier carried by a business request, use an object coroutine pre-created for the carried object identifier as the object coroutine corresponding to the business request, and add the business request to a coroutine message queue corresponding to the object coroutine;
[0014] The processing unit is used to adopt each determined object coroutine to obtain and process at least one corresponding business request from the corresponding coroutine message queue.
[0015] In a third aspect, an embodiment of the present application provides an electronic device, comprising a processor and a memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the above-mentioned service request processing method.
[0016] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, which includes a computer program. When the computer program is run on an electronic device, the computer program is used to enable the electronic device to execute the steps of the above-mentioned service request processing method.
[0017] In a fifth aspect, an embodiment of the present application provides a computer program product, which includes a computer program, and the computer program is stored in a computer-readable storage medium. The processor of an electronic device reads and executes the computer program from the computer-readable storage medium, so that the electronic device performs the steps of the above-mentioned business request processing method.
[0018] In an embodiment of the present application, after the server receives each business request for the target business, it uses the object coroutines pre-created for each object identifier carried by each business request as the corresponding object coroutine according to the object identifier carried by each business request, and adds each business request to the coroutine message queue of the corresponding object coroutine. Then, using the determined object coroutines, it obtains and processes at least one corresponding business request from the corresponding coroutine message queue.
[0019] In this way, on the one hand, since the coroutine is a lightweight thread, it can more efficiently utilize server resources and reduce server resource usage. On the other hand, combined with the coroutine message queue, the business requests of different target objects can be isolated. Each object coroutine does not need to process shared data, which effectively solves the problems of concurrent competition and thread switching, and greatly improves the request processing efficiency of the server. Especially in high-concurrency scenarios, it can greatly enhance the request processing capability of the server, thereby improving the response speed of the server and improving the business experience of the object. In addition, the request processing method of object coroutine and coroutine message queue is adopted to isolate and serialize each business request, which ensures the sequentiality of business requests, improves the concurrent processing capability of the server, and avoids mutual interference between multiple business requests, thereby improving the request processing efficiency.
[0020] Other features and advantages of the present application will be described in the following description, and partly become apparent from the description, or be understood by practicing the present application. The purpose and other advantages of the present application can be realized and obtained by the structures specifically pointed out in the written description, claims, and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0022] Figure 1 A schematic diagram of an application scenario provided in an embodiment of the present application;
[0023] Figure 2 A schematic diagram of a server architecture provided in an embodiment of the present application;
[0024] Figure 3 A flowchart of a business processing method provided in an embodiment of the present application;
[0025] Figure 4 A logical diagram of creating an owner coroutine provided in an embodiment of the present application;
[0026] Figure 5 A logical schematic diagram of selecting a second server provided in an embodiment of the present application;
[0027] Figure 6 A schematic diagram of an owner message queue provided in an embodiment of the present application;
[0028] Figure 7 A schematic diagram of another owner message queue provided in an embodiment of the present application;
[0029] Figure 8 A logical schematic diagram of a database connection pool provided in an embodiment of the present application;
[0030] Fig. 9 A logical diagram of a business processing process provided in an embodiment of the present application;
[0031] Fig.10 A logical diagram of another business processing process provided in an embodiment of the present application;
[0032] Fig.11 A schematic diagram of the structure of a service processing device provided in an embodiment of the present application;
[0033] Fig.12 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0034] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the technical solution of the present application, rather than all of the embodiments. Based on the embodiments recorded in the application documents, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the technical solution of the present application.
[0035] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the technical solution of the present application, rather than all of the embodiments. Based on the embodiments recorded in the application documents, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the technical solution of the present application.
[0036] The terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the invention described herein can be implemented in sequences other than those illustrated or described herein.
[0037] The relevant data collection and processing in this application should be strictly in accordance with the requirements of relevant national laws and regulations when applied in examples, obtain the informed consent or separate consent of the personal information subject, and carry out subsequent data use and processing within the scope of authorization of laws and regulations and the personal information subject.
[0038] First, some concepts involved in the embodiments of the present application are explained.
[0039] Coroutine: A lightweight thread that is scheduled by a cooperative scheduler in user space and can efficiently utilize system resources.
[0040] Message queue: A mechanism for asynchronous communication. Producers can send messages to the queue, and consumers can take messages from the queue for processing, thus achieving decoupling between producers and consumers.
[0041] Owner message queue: refers to a message queue based on objects (such as players), which is used to buffer business requests for a certain object.
[0042] Priority queue: A data structure that can sort elements according to their priority, ensuring that elements with high priority are processed first.
[0043] State machine: An abstract mathematical model used to describe the states of an object and the transition relationships between states.
[0044] Database connection pool: A technology for managing database connections. It can create multiple database connections when the system is initialized. When you need to access the database, you can get a connection from the connection pool. After completing the data operation, the connection is released back to the connection pool. This avoids frequent creation and closing of database connections and improves system performance.
[0045] With the continuous development of computer technology, background servers need to process a large number of client requests. How background servers handle massive requests is the key to ensuring service quality.
[0046] In related technologies, the backend server usually adopts an architecture implemented by multi-threading, multi-process, coroutine and asynchronous I / O.
[0047] In an architecture based on multi-threading or multi-process implementation, after receiving each service request, each service request is processed by multiple threads or multiple processes. However, in large-scale concurrent scenarios, multiple processes need to access a shared memory at the same time. Therefore, there are problems such as concurrency competition and thread switching between multiple processes, which seriously affect the message processing capability of the server.
[0048] In an architecture based on coroutines or asynchronous I / O, after receiving each business request, each business request is processed by a single thread. However, coroutines or asynchronous I / O have poor processing capabilities for high CPU intensive tasks. High CPU intensive tasks refer to tasks with a large number of calculations. When business requests belong to high CPU intensive tasks, it is difficult to efficiently process massive business requests through a single thread.
[0049] In an embodiment of the present application, after the server receives each business request for the target business, it uses the object coroutines pre-created for each object identifier carried by each business request as the corresponding object coroutine according to the object identifier carried by each business request, and adds each business request to the coroutine message queue of the corresponding object coroutine. Then, using the determined object coroutines, it obtains and processes at least one corresponding business request from the corresponding coroutine message queue.
[0050] In this way, on the one hand, since the coroutine is a lightweight thread, it can more efficiently utilize server resources and reduce server resource usage. On the other hand, combined with the coroutine message queue, the business requests of different target objects can be isolated. Each object coroutine does not need to process shared data, which effectively solves the problems of concurrent competition and thread switching, and greatly improves the request processing efficiency of the server. Especially in high-concurrency scenarios, it can greatly enhance the request processing capability of the server, thereby improving the response speed of the server and improving the business experience of the object. In addition, the request processing method of object coroutine and coroutine message queue is adopted to isolate and serialize each business request, which ensures the sequentiality of business requests, improves the concurrent processing capability of the server, and avoids mutual interference between multiple business requests, thereby improving the request processing efficiency.
[0051] The preferred embodiments of the present application are described below in conjunction with the drawings in the specification. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present application, and are not used to limit the present application. In addition, the embodiments of the present application and the features in the embodiments may be combined with each other if there is no conflict.
[0052] See also Figure 1 As shown, it is a schematic diagram of an application scenario provided in an embodiment of the present application. The application scenario schematic diagram includes a terminal device 110 and a server 120. The terminal device 110 and the server 120 can communicate with each other through a communication network.
[0053] The target application is pre-installed in the terminal device 110, and the target application includes but is not limited to game applications, instant messaging applications, social applications, etc. The target application can be a pre-installed client, web page, applet, etc. Among them, the client includes but is not limited to a client used by players and other objects, and can also be a virtual client for various business testing tasks in the application development process. The number of clients can be one or more, and there is no limit to this.
[0054] The terminal device 110 may include one or more processors, memory, I / O interface for interacting with the server 120, and display screen, etc. The terminal device 110 includes but is not limited to mobile phones, computers, intelligent voice interaction devices, smart home appliances, vehicle terminals, aircraft, etc.
[0055] The server is a background server corresponding to the target application, and provides corresponding services for the target application. The server 120 may include one or more processors, memories, and I / O interfaces for interacting with the terminal device 110. The server 120 may be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms.
[0056] The terminal device 110 and the server 120 may be connected directly or indirectly via wired or wireless communication, which is not limited in the embodiments of the present application.
[0057] The embodiments of the present application can be applied to various scenarios, including but not limited to cloud technology, artificial intelligence, smart transportation, assisted driving, etc.
[0058] See also Figure 2 As shown, it is a schematic diagram of a server architecture provided in an embodiment of the present application. The architecture includes: a client 201, an access component 202, a communication component 203, a business cluster 204 (also called a business server cluster), a public server cluster 205, and a storage component 206.
[0059] The client 201 includes a client 201a used by an object, multiple clients 201b used for stress testing during application development, a client simulator 201c, etc., but is not limited thereto.
[0060] The access component 202 may also be referred to as a gateway, and is used to receive each service request from the client 201 , and forward each service request to the communication component 203 .
[0061] The communication component 203 includes a proxy server 203a and a domain name resolution server 203b. The proxy server 203a is used to provide a service request transmission function. Exemplarily, the service request transmission function includes an L4 proxy and a communication bus designed for a service-oriented architecture.
[0062] In some implementations, the proxy server 203a is used to receive each service request and forward each service request to the domain name resolution server 203b, so that the domain name resolution server 203b allocates each service request to a corresponding service server in the service cluster 204 according to the service identifier carried in each service request.
[0063] In some implementations, the proxy server 203a may also be configured to receive partition results from the public server cluster 205 and return them to the corresponding client.
[0064] In some implementations, the proxy server 203a may also be configured to receive a service response from the service cluster 204 and return the service response to the corresponding client.
[0065] In some implementations, the domain name resolution server 203b may also receive each service request from a web browser, and distribute each service request to a corresponding service server in the service cluster 204 according to a service identifier carried in each service request.
[0066] In some implementations, a web browser used for stress testing during application development may also send a service request to a service server in the service cluster 204 through components such as Grafana and Prometheus.
[0067] Each business server included in the business cluster 204 is used to provide corresponding business services. In some implementations, different businesses can be assigned to corresponding business servers, and the corresponding business servers process business requests of the businesses. In the embodiment of the present application, one business server can correspond to one or more businesses, and one business can also correspond to one or more business servers, which is not limited. In game applications, business servers can also be called game logic servers.
[0068] The public service cluster 205 includes one or more public servers, and the public servers are used to determine the partition to which the target object belongs. For example, in a game architecture, multiple game partitions are usually set up, and the public servers are used to determine the game partition to which the target object belongs.
[0069] The storage component is used to provide data storage services. The storage component can be used but not limited to game databases (TencentDB for TcaplusDB, TcaplusDB), MySQL, etc. The storage component can be used to store the processing results of each business request from the business server. The storage component can also be used to store the processing results of the public server.
[0070] See also Figure 3 As shown, it is a flow chart of a service request processing method provided in an embodiment of the present application. The method is applied to a server. Taking the first server as an example, the service request processing method is described. The specific flow of the method is as follows:
[0071] S301: A first server receives service requests for a target service, each service request carrying an object identifier of a target object.
[0072] The first server is a service server in the service server cluster used for processing the target service.
[0073] It should be noted that in the embodiment of the present application, each application corresponds to one or more services, and different applications may correspond to different services. Taking a game application as an example, the services of the game application include but are not limited to game games, conversations, virtual resource transfers, etc. Taking an instant messaging application as an example, the services in the instant messaging application include but are not limited to voice conversations, video conversations, and friend circles, etc. Taking a social application as an example, the services in the social application include but are not limited to live broadcasting, posting articles, recommended browsing, etc. In the embodiment of the present application, after receiving each service request for the target service, the communication component sends each service request for the target service to a server (such as the first server) for processing the target service.
[0074] The target business includes but is not limited to game matches, chats, shopping malls, etc. The object identifier is used to uniquely identify a target object, and the object identifier can be an ID identifier, but is not limited to this.
[0075] In the following, only the service request x is taken as an example for description, and the service request x may be any service request among the service requests.
[0076] In the embodiment of the present application, the service request x may be any of the following types based on its source:
[0077] Category 1: Business requests from clients. After obtaining the business request of the target object, the client sends the business request to the first server. Specifically, as a possible implementation, if the client is a virtual client used for stress testing during development, the client can directly send the business request x to the first server. As another possible implementation, if the client is a client used by the object, the client sends the business request to the first server through the communication component.
[0078] Category 2: service requests from other servers. In the embodiment of the present application, when other servers are processing service request x, they may need the first server to assist in processing service request x. At this time, other servers will send service request x to the first server. The first server receives service request x from other servers.
[0079] In some implementations, the first server receives a network message that carries a service request x, and the first server unpacks the network message according to a predefined message protocol to obtain the service request x. The predefined message protocol includes, but is not limited to, using Protocol Buffers (protobuf), which is a set of data transmission formats and specifications similar to Extensible Markup Language (XML) or JavaScript Object Notation (JSON), and is used as transmission data when communicating between different applications or processes.
[0080] S302: The first server performs the following operations for each of the service requests:
[0081] Based on the object identifier carried by a business request, the object coroutine pre-created for the carried object identifier is used as the object coroutine corresponding to the business request, and the business request is added to the coroutine message queue corresponding to the object coroutine.
[0082] In the embodiment of the present application, for each object identifier, a corresponding object coroutine is pre-created, and the object coroutine may also be referred to as an owner coroutine. Still taking the service request x as an example, assuming that the service request x carries the object identifier of object x, and the object identifier of object x is userIDx, the first server creates the owner coroutine x corresponding to userIDx.
[0083] In the embodiment of the present application, the owner coroutine x may be created by the following two possible implementation methods:
[0084] As a first possible implementation, when it is determined that object x uses userIDx to log in to the client, the first server creates an owner coroutine corresponding to userIDx for userIDx. Correspondingly, when it is determined that object x uses userIDx to log out of the client, the first server destroys the object coroutine corresponding to userIDx for userIDx.
[0085] Exemplarily, when object x logs in to the client using userIDx, the client sends a login instruction for userIDx to the first server. When the first server receives the login instruction for userIDx from the client, it determines that object x logs in to the client using userIDx.
[0086] For example, see Figure 4As shown, when object A uses object identifier userID1 to log in to the client, the client sends a login instruction for userID1 to the first server. When the first server receives the login instruction for userID1 from the client, it determines that userID1 has logged in to the client, and creates an owner coroutine A corresponding to userID1. Similarly, when object identifiers such as userID2 of object B, userID3 of object C, and userID4 of object D log in to the client, each client sends a login instruction for account object identifiers such as userID2, userID3, and userID4 to the first server respectively. When the first server receives the login instruction for object identifiers such as userID2, userID3, and userID4 from the client, it determines that object identifiers such as userID2, userID3, and userID4 have logged in to the client, and creates owner coroutines corresponding to each of the object identifiers such as userID2, userID3, and userID4.
[0087] Through the above implementation, when the target object logs in and out of the client, the corresponding coroutine is created and destroyed, which realizes the dynamic adjustment of the coroutine, and because the target object reduces the server resource occupation, the server performance is improved. In addition, the coroutine is created and destroyed according to the target object's login and exit, and the coroutine is not created for the object that does not frequently use the target business, thereby further reducing the number of coroutines created and avoiding resource waste.
[0088] As a second possible implementation, the first server can also create an owner coroutine corresponding to userIDx when userIDx is registered. Correspondingly, when it is determined that userIDx has not logged into the client within a set period of time or userIDx is deregistered, the first server destroys the object coroutine corresponding to userIDx. Since the second possible implementation is similar to the first possible implementation, it will not be repeated here.
[0089] In some implementations, considering that the load conditions of each server in the business cluster are different, in order to avoid affecting the request processing efficiency due to excessive load on a single server, in the embodiments of the present application, the owner coroutine can be reasonably created in combination with the load conditions of the server and the account login conditions in the server. Specifically, when creating an object coroutine, the following methods can be used but are not limited to:
[0090] When the first server determines that each target object uses its own object identifier to log in to the client, the load evaluation information of the first server is obtained;
[0091] When the load evaluation information of the first server meets the local processing condition, the first server creates corresponding object coroutines in the first server for the object identifiers of the target objects.
[0092] The load evaluation information of the first server includes but is not limited to: at least one of the load information of the first server and the number of online objects in the first server, and the load information includes but is not limited to one or more of the information of the central processing unit (CPU) usage rate, memory usage rate, I / O load, etc. The I / O load can be, but is not limited to, the I / O waiting time.
[0093] Exemplarily, when the load evaluation information of the first server meets at least one of the following conditions, the load evaluation information of the first server meets the local processing condition:
[0094] Condition 1: The CPU usage of the first server does not exceed the CPU usage threshold;
[0095] Condition 2: The memory usage of the first server does not exceed the memory usage threshold;
[0096] Condition 3: The I / O waiting time of the first server does not exceed the time threshold;
[0097] Condition 4: The number of online objects does not exceed the object number threshold.
[0098] Each time the first server determines an object identifier login terminal, it can obtain the current load assessment information of the first server. Therefore, when the load assessment information of the first server meets the local processing conditions, the first server creates an object coroutine corresponding to the object identifier in the first server for the object identifier.
[0099] It should be noted that in the embodiment of the present application, the above conditions 1 to 4 can be combined arbitrarily. This article only takes condition 1 + condition 4 as an example for illustration. If condition 1 + condition 4 is adopted, when the first server determines that object A uses userIDx to log in to the client, it obtains the load assessment information of the first server. When the CPU usage in the load assessment information does not exceed the CPU usage threshold, and the number of online objects in the load assessment information does not exceed the object number threshold, the first server determines that its own load assessment information meets the local processing conditions, and for userIDx, creates an owner coroutine x corresponding to userIDx in the first server.
[0100] For example, assuming that the CPU usage threshold is 60% and the object quantity threshold is 200,000, when the first server determines that object A uses userIDx to log in to the client, it obtains the load assessment information of the first server. In the load assessment information, the CPU usage is 40% and the number of online objects is 100,000. At this time, the CPU usage does not exceed the CPU usage threshold of 60%, and the number of online objects does not exceed the object quantity threshold of 200,000. The first server determines that its own load assessment information meets the local processing conditions, and for userIDx, creates an owner coroutine x corresponding to userIDx in the first server.
[0101] Further, if the load evaluation information of the first server does not meet the local processing conditions, the first server may select a suitable server to provide business services for the target object.
[0102] Specifically, when it is determined that each target object uses its own object identifier to log in to the client, and the load evaluation information of the first server does not meet the local processing condition, the first server selects the second server from each candidate server based on the load evaluation information of each candidate server;
[0103] The object identifiers of the target objects are sent to the second server, so that the second server creates corresponding object coroutines for the target objects, and uses the created object coroutines to receive and merge the business requests.
[0104] As a possible implementation manner, the selected second server is a candidate server that meets the local processing conditions. The local processing conditions refer to conditions 1 to 4 in the above text and are not described in detail here.
[0105] If there are multiple candidate servers that meet the local processing conditions, then the second server can be selected by further combining the multiple corresponding load evaluation information. Taking CPU utilization and the number of online objects as an example, the candidate server with the lowest CPU utilization can be selected as the second server, or the candidate server with the least number of online objects can be selected as the second server. In addition, the candidate server with the best performance represented by the comprehensive evaluation results can be selected as the second server by combining CPU utilization and the number of online objects. The following describes the three methods respectively.
[0106] Method 1: The first server selects a candidate server with the lowest CPU usage rate from among the candidate servers based on the CPU usage rates of the candidate servers as the second server.
[0107] Among them, if there are multiple candidate servers with the lowest CPU usage, a candidate server can be randomly selected from the multiple candidate servers as the second server, or other information, such as the number of online objects, can be further combined to determine a candidate server from the multiple candidate servers as the second server based on other information. There is no limitation on this.
[0108] For example, see Figure 5 As shown, the first server is server 1. Assume that the candidate servers include server 2, server 3, server 4, and server 5. In the load evaluation information of each of server 2, server 3, server 4, and server 5, the CPU utilization rates are 20%, 30%, 80%, and 90%, respectively. When it is determined that object A uses object identifier userID1 to log in to the client, and the load evaluation information of the first server does not meet the local processing conditions, the first server selects server 2 with the lowest CPU utilization rate from server 2, server 3, server 4, and server 5 as the second server based on the CPU utilization rates of each of server 2, server 3, server 4, and server 5. Then, object identifier 1 is sent to server 2, so that server 2 creates a corresponding object coroutine for userID1, and uses the created object coroutine to receive and process each business request of userID1.
[0109] Since the process of creating an object coroutine in the second server is the same as the process of creating an object coroutine in the first server, it will not be repeated here.
[0110] Method 2: Based on the number of online objects of each candidate server, a candidate server with the lowest number of online objects is selected from the candidate servers as the second server.
[0111] Among them, if there are multiple candidate servers with the lowest number of online objects, a candidate server can be randomly selected from the multiple candidate servers as the second server, or other information, such as memory usage, can be further combined to determine a candidate server from the multiple candidate servers as the second server based on other information. There is no limitation on this.
[0112] For example, assuming that the candidate servers include server 2, server 3, server 4, and server 5, and in the load assessment information of each of server 2, server 3, server 4, and server 5, the number of online objects is 500,000, 500,000, 500,000, and 300,000, respectively. When it is determined that object A uses object identifier userID1 to log in to the client, and the load assessment information of the first server does not meet the local processing conditions, the first server selects server 5 with the lowest number of online objects from server 2, server 3, server 4, and server 5 as the second server based on the number of online objects of each of server 2, server 3, server 4, and server 5, and then sends object identifier 1 to server 2, so that server 2 creates a corresponding object coroutine for object identifier 1, and uses the created object coroutine to receive each business request for the merged processing object.
[0113] Method 3: Based on the CPU usage of each candidate server, the number of online objects of each candidate server, and the weights corresponding to the CPU usage and the number of online objects, obtain the evaluation value of each candidate server, and based on the evaluation value of each candidate server, select a candidate server with an evaluation value higher than the evaluation value threshold from each candidate server as the second server. It should be noted that in the embodiment of the present application, only the example that the higher the evaluation value, the better the server performance is used for explanation.
[0114] It should be noted that if there are multiple candidate servers with evaluation values higher than the evaluation value threshold, any one of the candidate servers can be selected as the second server, or the candidate server with the highest evaluation value can be selected as the second server. Other methods can also be used, which are not limited and will not be repeated here.
[0115] Since the data units of CPU usage and the number of online objects are different, in an embodiment of the present application, for each candidate server, data conversion can also be performed on the CPU usage and / or the number of online objects. For example, different ranges of CPU usage are set, each CPU usage range corresponds to a CPU evaluation value, and different ranges of online objects are set, each online object number range corresponds to a login evaluation value, and the evaluation values corresponding to each range can be the same or different, and there is no restriction on this. The first server determines the CPU evaluation value of each candidate server based on the CPU usage range where the CPU usage of each candidate server is located, and determines the login evaluation value of each candidate server based on the login number range where the number of online objects of each candidate server is located. Then, based on the login evaluation value and CPU evaluation value of each candidate server, combined with the weights corresponding to the CPU usage and the number of online objects, the evaluation value of each candidate server is obtained.
[0116] For example, the CPU usage range includes [0, 20), [20, 40), [40, 60), [60, 80), [80, 90), [90, 100], and their corresponding CPU evaluation values are 10, 9, 5, 4, 2, 1 respectively. The range of the number of online objects includes: [0, 20), [20, 30), [30, 40), [40, 50), [50, 60), and their corresponding login evaluation values are 10, 8, 6, 4, 2 respectively. Assume that the CPU usage of server 3, server 4, and server 5 is 20%, 30%, 80%, and 90%, respectively, and the number of online objects is 500,000, 500,000, 500,000, and 300,000, respectively. The first server determines the CPU evaluation values of each candidate server to be 9, 9, 2, and 1, respectively, based on the CPU usage range of each CPU usage of server 2, server 3, server 4, and server 5, and determines the login evaluation values of server 2, server 3, server 4, and server 5 to be 4, 4, 4, and 6, respectively, based on the login number range of each online object number of server 2, server 3, server 4, and server 5. Assume that the weights corresponding to the CPU usage and the number of online objects are both 50%, then the evaluation values of server 2, server 3, server 4, and server 5 are 6.5, 6.5, 3, and 3.5, respectively.
[0117] Through the above implementation method, when the load pressure of the first server is relatively large, a second server with a smaller load pressure can be selected, and the second server can provide business services for the target object, thereby reducing the load of the first server and improving the resource utilization rate of the second server to achieve a load balancing effect.
[0118] As another possible implementation method, the above-mentioned method 1, method 2, and method 3 may be directly adopted to select the second server using various information in multiple corresponding load assessment information. Since the selection process is the same, it will not be repeated here.
[0119] In the embodiment of the present application, the coroutine message queue may also be referred to as the owner message queue. After creating the owner coroutine, the first server creates a corresponding owner message queue for each owner coroutine, and the owner message queue is used to cache each service request corresponding to the owner coroutine.
[0120] The first server adds each received business request to the owner message queue of the corresponding owner coroutine, and then each owner coroutine can take out the corresponding business request from the corresponding owner message queue for processing. Through the message queue method, the decoupling between producers and consumers can be achieved, and the stability of the server architecture can be improved.
[0121] The owner message queue can adopt a first-in-first-out queue or a priority queue, but is not limited to this. Elements in a first-in-first-out queue enter the queue at the end and leave the queue from the head. Elements in a priority queue are assigned a priority, and the element with the highest priority leaves the queue first.
[0122] In a possible implementation, the owner message queue adopts a first-in-first-out queue. After the first server determines the owner coroutine corresponding to each business request, it adds each business request to the owner message queue of the corresponding owner coroutine in a certain order. Here, in a certain order can refer to the order in which each business request is received, or it can refer to the order in which the request time carried by each business request is carried, but it is not limited to this and is not limited to this. Accordingly, for each owner coroutine, the first server can obtain and process at least one business request corresponding to the owner coroutine from the owner message queue corresponding to the owner coroutine in sequence.
[0123] For example, see Figure 6 As shown, the first server receives business requests such as business request 1, business request 2, ..., business request 10 in sequence, wherein business request 1, business request 2, business request 4, business request 10 and other business requests all carry the object identifier userID1 of object A, business request 3, business request 7 and other business requests all carry the object identifier userID2 of object B, business request 5, business request 9 and other business requests all carry the object identifier userID3 of object C, and business request 6, business request 8 and other business requests all carry the object identifier userID4 of object D. The first server determines that the owner coroutines corresponding to the business requests such as business request 1, business request 2, business request 4, and business request 10 are owner coroutines A created for userID1 according to the object identifiers carried by each business request, and sequentially adds the business requests such as business request 1, business request 2, business request 4, and business request 10 to the owner message queue A corresponding to the owner coroutine A according to the order in which each business request is received. Similarly, the first server sequentially adds the business requests such as business request 3, business request 7, and business request to the owner message queue B corresponding to the owner coroutine B, sequentially adds the business requests such as business request 5, business request 9, and business request to the owner message queue C corresponding to the owner coroutine C, and sequentially adds the business requests such as business request 6, business request 8, and business request to the owner message queue D corresponding to the owner coroutine D according to the order in which each business request is received. Further, the owner coroutines A, B, C, and D are used to sequentially pull and process the business requests in the corresponding owner.
[0124] In another possible implementation, the owner message queue is implemented using a priority queue. In this case, each business request also carries a request operation type. The first server determines the execution priority corresponding to each business request based on the request operation type carried by each business request. Then, after determining the owner coroutine corresponding to each business request, for each business request, still taking business request x as an example, the execution priority of business request x is compared with the execution priority of each queued business request contained in the owner message queue x. Based on the comparison result, the position of business request x in the owner message queue x is determined, and based on the position of business request x in the owner message queue x, the business request x is added to the owner message queue x.
[0125] In the embodiment of the present application, each service corresponds to one or more request operation types. Taking the game match service as an example, the request operation types include virtual object movement request, skill use request, item purchase request, etc. Taking the resource transfer service in the game as an example, the request operation types include resource transfer request, payment password input request, lottery request, etc., but are not limited to this.
[0126] When determining the execution priority corresponding to each business request based on the request operation type carried by each business request, as an example, the first server can determine the execution priority corresponding to each business request based on the request operation type carried by each business request, combined with the preset mapping relationship between each request operation type and each execution priority.
[0127] Among them, the preset mapping relationship between each request operation type and each execution priority includes: the request operation type carried by each business request and its corresponding execution priority. Each execution priority in the mapping relationship can be expressed by, but not limited to, a level. The execution priority and the level can be positively correlated, that is, the higher the level, the higher the execution priority. The execution priority and the level can also be negatively correlated, that is, the higher the level, the lower the execution priority. The following only takes the example of the higher level and the higher execution priority as an example for explanation.
[0128] The queued business requests contained in the owner message queue x are sorted according to their respective execution priorities. In the owner message queue x, the queued business requests with higher execution priorities are located before the queued business requests with lower execution priorities. When the first server determines the position of the business request x in the owner message queue x based on the comparison result, for each of the queued business requests, if the execution priority of the business request x is higher than the execution priority of the queued business request, then the business request x is located before the queued business request in the owner message queue x; if the execution priority of the business request x is lower than the execution priority of an queued business request, then the business request x is located after the queued business request in the owner message queue x; if the execution priority of the business request x is the same as the execution priority of an queued business request, then in the owner message queue x, the business request x can be located after the queued business request or after the queued business request, without limitation.
[0129] Take business request 1, business request 2, ..., business request 10 as an example, see Figure 7 As shown, the request operation types of business request 4, business request 8, business request 9, and business request 10 are all item purchase requests, the request operation types of business request 1, business request 3, and business request 5 are all virtual object movement requests, and the request operation types of business request 2, business request 6, and business request 7 are all skill use requests. It is assumed that, in the preset mapping relationship between each request operation type and each execution priority, the execution priorities corresponding to the virtual object movement request, skill use request, and item purchase request are priority 2, priority 3, and priority 1, respectively.
[0130] Taking owner message queue A as an example, it is assumed that owner message queue A is initially an empty queue. For business request 1, owner message queue A is an empty queue, and the first server directly adds business request 1 to owner message queue A. For business request 2, business request 1 is stored in owner message queue A. The first server compares the execution priority of business request 2 and business request 1. The comparison result indicates that the execution priority of business request 2 is higher than the execution priority of business request 1. Based on the comparison result, it is determined that business request 2 is before business request 1. For business request 4, business request 1 and business request 2 are stored in owner message queue A. The first server compares the execution priority of business request 4 with business request 1 and business request 2 respectively. The comparison result indicates that the execution priority of business request 4 is higher than the execution priority of business request 1 and business request 2. Based on the comparison result, it is determined that business request 4 is located after business request 1 and business request 1. Similarly, the business requests in owner message queue A are arranged in the order of business request 2, business request 1, business request 4, and business request 10. The business requests in owner message queue B are arranged in the order of business request 7 and business request 3. The business requests in owner message queue C are arranged in the order of business request 5 and business request 9. The business requests in owner message queue D are arranged in the order of business request 6 and business request 8.
[0131] In the above implementation, by adopting a priority queue, business requests are prioritized, and business requests with high priority will be executed first, thereby improving the processing efficiency of the system. In actual business, this solution can significantly improve the efficiency of request processing. The specific efficiency ratio depends on the actual business scenario and system load.
[0132] S303: The first server uses the determined object coroutines to obtain and process at least one corresponding business request from the corresponding coroutine message queue.
[0133] Specifically, when S303 is executed, the first server adopts the owner coroutine for each owner coroutine in each owner coroutine, and sequentially obtains and processes at least one business request corresponding to the owner coroutine from the owner message queue corresponding to the owner coroutine.
[0134] For example, see Figure 6As shown, the first server adopts owner coroutine A to sequentially obtain and process business requests such as business request 1, business request 2, business request 4, business request 10, etc. from owner message queue A; adopts owner coroutine B to sequentially obtain and process business request 7, business request 3, etc. from owner message queue B; adopts owner coroutine C to sequentially obtain and process business request 5, business request 9, etc. from owner message queue C; adopts owner coroutine D to sequentially obtain and process business request 6, business request 8, etc. from owner message queue D.
[0135] For example, see Figure 7 As shown, the first server adopts owner coroutine A to sequentially obtain and process business requests such as business request 2, business request 1, business request 4, and business request 10 from owner message queue A; adopts owner coroutine B to sequentially obtain and process business request 3, business request 7, and other business requests from owner message queue B; adopts owner coroutine C to sequentially obtain and process business request 5, business request 9, and other business requests from owner message queue C; adopts owner coroutine D to sequentially obtain and process business request 6, business request 8, and other business requests from owner message queue D.
[0136] As a possible implementation method, when using each determined object coroutine to process at least one corresponding business request from the corresponding coroutine message queue, the following methods may be used but are not limited to:
[0137] For each owner coroutine in each owner coroutine, the first server may determine the processing interface corresponding to the at least one business request based on the request operation type carried by the at least one business request corresponding to the owner coroutine and in combination with the correspondence between the request operation type and the processing interface;
[0138] The owner coroutine is used to obtain the processing result of at least one business request by calling the processing interface corresponding to at least one business request.
[0139] Each processing interface is used to process a service request of a corresponding request operation type. In the embodiment of the present application, a correspondence between each request operation type and each processing interface is preset. For example, each request operation type may correspond to a processing interface, and each processing interface corresponds to a request operation type.
[0140] The processing interface can also be called a logical processing function (handler function). The owner coroutine calls the corresponding handler function according to the requested operation type. The handler function performs the corresponding business operation according to the data in the business request. In the process of the handler function performing the corresponding business operation, it may involve reading and writing data in the database (i.e., storage component), network communication with other devices, etc., which is not limited and will not be repeated here.
[0141] Still taking business request 1, business request 2, ..., business request 10 as an example, assuming that the virtual object movement request corresponds to processing interface 1, the prop purchase request corresponds to processing interface 2, the skill use request corresponds to processing interface 3, the request operation types of business request 4, business request 8, business request 9, and business request 10 are all prop purchase requests, the request operation types of business request 1, business request 3, and business request 5 are all virtual object movement requests, and the request operation types of business request 2, business request 6, and business request 7 are all skill use requests. The first server performs a processing based on the request operation types carried by business request 1, business request 2, ..., business request 10, in combination with each request operation type and each processing interface. The correspondence between the interfaces determines that the processing interface corresponding to business request 1, business request 3, and business request 5 is processing interface 1, the processing interface corresponding to business request 4, business request 8, business request 9, and business request 10 is processing interface 2, and the processing interface corresponding to business request 2, business request 6, and business request 7 is processing interface 3. Taking owner coroutine A as an example, the first server adopts owner coroutine A, obtains the processing result of business request 1 by calling processing interface 1, obtains the processing result of business request 2 by calling processing interface 3, obtains the processing result of business request 4 by calling processing interface 2, and obtains the processing result of business request 10 by calling processing interface 2.
[0142] As a possible implementation method, the owner coroutine is used to call the processing interface corresponding to a business request to obtain the processing result of the business request. The owner coroutine can be used to pass the business request as a parameter to the processing interface corresponding to the business request, and the processing result of the business request is output through the processing interface corresponding to the business request.
[0143] In some implementations, a state machine model may be used to process different message types. Different business logics may be used to process different message types. Specifically, when the owner coroutine x is used to process at least one business request, the following methods may be used but are not limited to:
[0144] When there is a start request in at least one business request, the owner coroutine x is used to set the state of the state machine of the target business to the initial state, and call the processing interface corresponding to the start request to obtain the initial execution result, and adjust the state of the state machine to the second state according to the initial execution result. Then, when a business request associated with the second state is obtained, the processing interface of the business request associated with the second state is called to obtain the second execution result; when the second state is the termination state, the target execution result is obtained according to the second execution result. Among them, the initial state can also be called the first state.
[0145] A state machine is an abstract mathematical model used to describe the state of an object and the conversion relationship between states. In the embodiment of the present application, different state machines are set for different businesses. Each state machine contains multiple states, and each state corresponds to a business logic. The business logic corresponding to each state can be saved in the local space of the first server, or saved in the blockchain to prevent data tampering.
[0146] The service initiation request refers to a service request in which the request operation type is a setting operation type in at least one service request.
[0147] It can be understood that when the second state is a non-terminal state, the current second state can be used as the new first state, the second business logic of the current second state can be used as the new initial business logic, the execution result of the current second business logic can be used as the execution result of the new initial business logic, and then the new second state can be determined based on the execution result of the new initial business logic, and so on.
[0148] The service request associated with the second state refers to a service request for executing the service logic corresponding to the second state. The service request associated with the second state may be a service request in at least one service request, or may be a newly acquired service request.
[0149] Taking the target business as the transfer of virtual resources (such as game coins) in a game application as an example, multiple states are set for the state machine according to the game coin transfer process, such as a game coin purchase state and a successful purchase state, wherein the game coin purchase state is the initial state and the successful purchase state is the termination state. When the first server receives a business request from the target object to transfer game coins, it determines that the business request is a start request, sets the state of the state machine of the target business to the initial state (game coin purchase state), and calls the processing interface corresponding to the start request (receiving the user input of the payment password) to obtain the initial execution result. Then, according to the initial execution result, the state of the state machine is adjusted to the termination state (successful purchase state). Afterwards, when a business request associated with the second state (password input request) is obtained, the processing interface of the business request associated with the second state is called (after the first server successfully verifies the password, the game coins are transferred to the object's game account) to obtain the second execution result. At this time, when the second state is the termination state, the target execution result of transferring the game coins is obtained according to the second execution result.
[0150] Through the above implementation, the state machine can be used to control the state transfer in the business, enriching the implementation method of the business, and using the state machine to make the state transition of the entire business orderly and easy to maintain, thereby better ensuring the implementation of the business.
[0151] In some embodiments, a database connection pool technology may also be used to manage data access. Specifically, the first server performs the following operations for each object coroutine in the object coroutines:
[0152] Select an available connection from a database connection pool, where the database connection pool is a shared connection pool created for multiple threads that the application needs to call;
[0153] The selected available connection is allocated to the one object coroutine for use, and the one object coroutine uses the available connection to perform data operations.
[0154] In an embodiment of the present application, a database connection pool can be created according to a target application. When the target application starts running or is initialized, a database connection pool can be established to store the application data generated by the target application. Through the database connection pool, connections are allocated in real time to all processes required by the target application, so that each owner coroutine can use the connection to complete the interaction with the database and realize data operations such as reading or writing data from the database.
[0155] There are multiple connections stored in the database connection pool, some of which may be providing services for other owner coroutines. For owner coroutine x, the connections providing services for other owner coroutines are connections in use, and these connections can no longer be allocated to owner coroutine x for use. Therefore, in an embodiment of the present application, when allocating a connection to owner coroutine x, it is necessary to filter out the available connections for owner coroutine x from the multiple connections stored in the database connection pool.
[0156] The connection available to the owner coroutine x refers to a connection among multiple connections that can be used to provide services for the owner coroutine x. The connection available to the owner coroutine x can be an idle connection or a newly created connection, and there is no limitation on this.
[0157] For example, see Figure 8 As shown, the database connection pool stores multiple connections such as connection 1, connection 2, and connection 3, among which connection 2 is providing services for owner coroutine B, and connection 3 is providing services for owner coroutine B. For owner coroutine A, the first server selects an available connection from the database connection pool: connection 1, and allocates the selected connection 1 to owner coroutine A for use, and owner coroutine A uses connection 1 to read and write data.
[0158] The management and allocation of each connection in the database connection pool can be implemented based on the thread lock mechanism, so as to ensure that after a connection in the database connection pool is allocated to a certain owner coroutine x, the owner coroutine x is equivalent to locking the connection to ensure that the connection in the database connection pool is safe and available, avoid the problem of overwriting data caused by multiple owner coroutines x being allocated to the same connection, and avoid the data format being destroyed.
[0159] Through the above implementation, when you need to access the database, get a connection from the connection pool, and release the connection back to the connection pool after completing the data operation, so as to avoid frequent creation and closing of database connections and improve system performance.
[0160] In some embodiments, after receiving each service request for the target service, the first server may perform the following operations for each service request respectively before creating an object coroutine in advance for the object identifier carried in the service request based on the object identifier carried in the service request:
[0161] When it is determined that business request x needs to be processed with the assistance of other servers based on the request operation type carried by business request x, other servers to which other objects belong are determined based on the object identifiers of other objects carried in business request x; business request x is sent to other servers so that other servers execute a business request and obtain corresponding processing results; and a processing result of a business request returned by other servers is received.
[0162] The request operation type that other servers assist in processing refers to that the business request x requires the interaction between object x and other objects to complete, and object x and other objects are located in different servers. For example, the request operation type may be a game match request sent by object A to object B.
[0163] In an embodiment of the present application, the first server can obtain the request operation type that needs to be assisted by other servers for processing, and then determine whether the business request x needs to be assisted by other servers for processing based on the request operation type carried by the business request x.
[0164] For example, business request 11 is used to invite object E to participate in a game. When it is determined that business request 11 needs to be processed with the assistance of other servers based on the request operation type carried by business request 11, the first server determines that the server to which object E belongs is server 2 based on the object identifier userID11 of object E carried in business request 11, and sends business request 11 to server 2, so that server 2 executes business request 11 to obtain the corresponding processing result, and then receives the processing result of business request 11 returned by server 2.
[0165] Below, this application is explained in conjunction with a specific application scenario.
[0166] Embodiment 1: Game match service in game application.
[0167] See also Fig. 9 As shown, it is a logical schematic diagram of a business request processing process provided in an embodiment of the present application.
[0168] The communication component receives a network message a, which includes a service request a. The service request a is used to move the target virtual object from position A to position B. The service request a carries: the object identifier userID1 of object A and the service identifier serviceIDA of the game match service.
[0169] The communication component unpacks the network message a according to the predefined message protocol, obtains the service request a, receives the service request a after performing data verification on the service request a, and allocates the service request a to the server 1 that handles the game game service according to the service identifier serviceIDA of the game game service.
[0170] See also Fig.10 As shown, server 1 receives service request a and first adds service request a to the unified message queue. The queue length of the unified message queue is fixed and is used to buffer the service requests received by server 1. The queue length of the unified message queue should not be too large to avoid occupying a large amount of memory. At the same time, the number of service requests actually cached in the unified message queue can be checked periodically to avoid queue overflow.
[0171] Server 1 obtains each service request from the unified message queue in sequence through the service thread, assigns each service request to the corresponding owner coroutine, and adds each service request to the owner message queue of the corresponding owner coroutine. Taking service request a as an example, when the service thread reads service request a from the unified message queue, based on the object identifier userID1 carried by service request a, the owner coroutine A pre-created for userID1 is used as the object coroutine corresponding to service request a, and service request a is added to the owner message queue A corresponding to owner coroutine A. For each owner coroutine, the owner coroutine obtains the corresponding service requests from the corresponding owner message queue in sequence, and based on the request operation type carried by each service request, combined with the correspondence between each request operation type and each processing interface, determines the processing interface corresponding to each service request, and then obtains the processing result of each service request by calling the processing interface corresponding to each service request. For example, server 1 uses owner message queue A, obtains business request a from owner message queue A, and determines that the processing interface corresponding to business request a is processing interface 1 according to the request operation type virtual object moving operation corresponding to business request a, and obtains the processing result of business request a by calling the processing interface corresponding to processing interface 1. The processing result of business request a can also be stored in the storage component. Afterwards, server 1 returns the processing result of business request a to the client of object A, so that the client presents the moving operation of the target virtual object in the operation interface. In the process of processing the corresponding business processing logic through the processing interface, if data update (such as data modification) is involved, the updated data can be stored in the storage component.
[0172] Embodiment 2: Voice sending service in instant messaging application.
[0173] The communication component receives a network message b, which includes a service request b, which is used by object A to send a voice message to object B. The service request b carries: the object ID userID1 of object A and the service ID serviceIDB of the voice sending service.
[0174] The communication component unpacks the network message b according to the predefined message protocol, obtains the service request b, receives the service request b after data verification, and allocates the service request b to the server 2 that processes the voice sending service according to the service identifier serviceIDB of the voice sending service.
[0175] Server 2 receives business request b, first adds business request b to the unified message queue, and then obtains each business request in the unified message queue in sequence through the business thread. When business request b is obtained, server 2 can use the owner coroutine 12 pre-created for userID12 as the object coroutine corresponding to business request b based on the object identifier userID12 carried by business request b, and add business request b to the owner message queue 12 corresponding to owner coroutine 12. Owner coroutine 12 obtains business requests from owner message queue 12 in sequence for processing. When owner message queue 12 is used to obtain business request b from owner message queue 12, and according to the request operation type voice sending operation corresponding to business request b, the processing interface (sending voice) corresponding to business request b is determined, and the processing result of business request b is obtained by calling the processing interface. Afterwards, server 2 returns the processing result of business request b to the client of object A, so that the client presents the voice message sent to object B in the operation interface.
[0176] Embodiment 3: Bullet screen service in video application.
[0177] The communication component receives the network message c, which includes a service request c, which is used to send bullet-screen information for the cartoon. The service request c carries: the object identifier userID4 of the object D, and the service identifier serviceIDC of the bullet-screen service.
[0178] The communication component unpacks the network message c according to the predefined message protocol, obtains the service request c, receives the service request c after performing data verification on the service request c, and allocates the service request b to the server 3 that processes the barrage service according to the service identifier serviceIDB of the barrage service.
[0179] Server 3 receives business request c, first adds business request c to the unified message queue, and then obtains each business request in the unified message queue in sequence through the business thread. When obtaining business request c, server 3 can use the owner coroutine 4 pre-created for userID4 as the object coroutine corresponding to business request c based on the object identifier userID4 carried by business request c, and add business request c to the owner message queue 4 corresponding to owner coroutine 4. Owner coroutine 4 obtains business requests from owner message queue 4 in sequence for processing. When owner message queue 4 is used to obtain business request c from owner message queue 4, and according to the request operation type voice sending operation corresponding to business request c, the processing interface corresponding to business request c (sending barrage) is determined, and the processing result of business request b is obtained by calling the processing interface. Afterwards, server 3 returns the processing result of business request c to the client of object D, so that the client can present the sent barrage information in the operation interface.
[0180] In the embodiment of the present application, a request processing method based on coroutines is adopted to effectively solve the technical bottlenecks such as concurrency problems and resource waste in the message processing method based on multi-threading. Specifically, by introducing the owner coroutine and message queue, the business request processing process is separated, and a large number of business requests of different objects are grouped, which greatly improves the message processing efficiency, reduces the resource occupation of the server system, and at the same time ensures the sequentiality of player requests and improves the concurrent processing capability of the game server.
[0181] Since the request is processed by coroutine and message queue, the requests of different players are isolated and serialized, thus avoiding the mutual interference between multiple requests and improving the processing efficiency of requests. In addition, the request is processed by coroutine and message queue, avoiding the thread switching overhead that may occur when multi-threaded processing requests, thus saving system resources.
[0182] Furthermore, priority queues and state machines are used to prioritize and manage the status of business requests, further improving the system's processing efficiency. In actual business, this solution can significantly improve the efficiency of request processing. The specific efficiency ratio depends on the actual business scenario and system load. In addition, the use of priority queues and state machines for request management avoids the waste of system resources due to invalid requests.
[0183] Furthermore, functions such as database connection pool, exception handling and log management can be used to further optimize the use of system resources and improve the reliability and stability of the system.
[0184] The business request processing solution in the embodiment of the present application can be applied to various business scenarios that need to process a large number of concurrent requests, such as online games, social applications, etc. Since business scenarios such as online games need to process a large number of requests and have high requirements for request processing efficiency and stability, this solution can effectively meet business needs and improve system performance and reliability.
[0185] Based on the same inventive concept, the embodiment of the present application provides a service request processing device. Fig.11 As shown, it is a schematic diagram of the structure of the service request processing device 1100, which may include:
[0186] The receiving unit 1101 is used to receive each service request for a target service, each service request carries an object identifier of a target object;
[0187] The enqueue unit 1102 is used to perform the following operations for each of the business requests: based on an object identifier carried by a business request, use an object coroutine pre-created for the carried object identifier as the object coroutine corresponding to the business request, and add the business request to a coroutine message queue corresponding to the object coroutine;
[0188] The processing unit 1103 is used to adopt each determined object coroutine to obtain and process the corresponding at least one business request from the corresponding coroutine message queue.
[0189] As a possible implementation, each business request also carries a request operation type; when adding the business request to the coroutine message queue corresponding to the object coroutine, the enqueue unit 1102 is specifically used for:
[0190] Determining an execution priority corresponding to the service request based on a request operation type carried by the service request;
[0191] Compare the execution priority of the one business request with the execution priorities of each queued business request in the corresponding coroutine message queue, and determine the position of the one business request in the corresponding coroutine message queue based on the comparison result;
[0192] Based on the position of the one business request in the corresponding coroutine message queue, the one business request is added to the corresponding coroutine message queue.
[0193] As a possible implementation manner, each of the business requests also carries a request operation type; when an object coroutine is used to execute the at least one business request in sequence based on the business request corresponding to each of the at least one business request, the processing unit 1103 is specifically used to:
[0194] Based on the request operation type carried by each of the at least one business request and in combination with the correspondence between each request operation type and each processing interface, determine the processing interface corresponding to each of the at least one business request, each processing interface being used to process the business request of the corresponding request operation type;
[0195] An object coroutine is used to obtain the processing result of each of the at least one business request by calling the processing interface corresponding to each of the at least one business request.
[0196] As a possible implementation manner, when an object coroutine is used to obtain the processing result of each of the at least one business request by calling the processing interface corresponding to each of the at least one business request, the processing unit 1103 is specifically used to:
[0197] When there is a start request in the at least one business request, using the one object coroutine, setting the state of the state machine of the target business to an initial state, calling a processing interface corresponding to the start request, obtaining an initial execution result, and adjusting the state to a second state according to the initial execution result;
[0198] When a service request associated with the second state is obtained, a processing interface of the service request associated with the second state is called to obtain a second execution result;
[0199] When the second state is a termination state, a target execution result is obtained according to the second execution result.
[0200] As a possible implementation, before receiving each service request for the target service, the queuing unit 1102 is further used for:
[0201] When it is determined that each target object uses its own object identifier to log in to the client, a corresponding object coroutine is created for each object identifier of the target object;
[0202] After each object coroutine created by the adoption obtains and processes the corresponding at least one business request from the corresponding coroutine message queue, the enqueuing unit 1102 is further used to:
[0203] When it is determined that each target object uses its own object identifier to exit the client, the corresponding object coroutines are destroyed respectively for the object identifiers of the target objects.
[0204] As a possible implementation, when it is determined that each target object uses its own object identifier to log in to the client, and when corresponding object coroutines are created for each object identifier of the target object, the processing unit 1103 is specifically used to:
[0205] When it is determined that each target object uses its own object identifier to log in to the client, obtaining load evaluation information of the first server;
[0206] When the load evaluation information of the first server meets the local processing conditions, corresponding object coroutines are created in the first server for the object identifiers of the target objects.
[0207] As a possible implementation manner, the processing unit 1103 is further configured to:
[0208] When it is determined that each target object uses its own object identifier to log in to the client, and the load evaluation information of the first server does not meet the local processing condition, selecting a second server from each candidate server based on the load evaluation information of each candidate server;
[0209] The object identifiers of the target objects are sent to the second server, so that the second server creates corresponding object coroutines for the target objects, and uses the created object coroutines to receive and merge the business requests.
[0210] As a possible implementation, when receiving each service request for the target service, the receiving unit 1101 is specifically configured to perform at least one of the following operations:
[0211] Receiving service requests for target services from various clients;
[0212] Receive service requests for target services from other servers.
[0213] As a possible implementation manner, after receiving each service request for the target service, before the object coroutine pre-created for the carried object identifier based on the object identifier carried by the service request, the processing unit 1103 is further used to:
[0214] For each of the service requests, perform the following operations:
[0215] When it is determined that a service request needs to be assisted by other servers in processing based on a request operation type carried in the service request, determining other servers to which the other objects belong based on object identifiers of other objects carried in the service request;
[0216] Sending the one service request to the other server, so that the other server executes the one service request to obtain a corresponding processing result;
[0217] Receive a processing result of the one service request returned by the other server.
[0218] As a possible implementation, when each object coroutine created by the adoption obtains and processes the corresponding at least one business request from the corresponding coroutine message queue, the processing unit 1103 is specifically used to:
[0219] For each of the object coroutines, perform the following operations respectively:
[0220] Select an available connection from a database connection pool, where the database connection pool is a shared connection pool created for multiple threads that the application needs to call;
[0221] The selected available connection is allocated to the one object coroutine for use, and the one object coroutine uses the available connection to perform data operations.
[0222] For the convenience of description, the above parts are divided into modules (or units) according to their functions and described separately. Of course, when implementing this application, the functions of each module (or unit) can be implemented in the same or multiple software or hardware.
[0223] Regarding the device in the above embodiment, the specific manner in which each unit executes the request has been described in detail in the embodiment of the method, and will not be elaborated here.
[0224] Those skilled in the art will appreciate that various aspects of the present application may be implemented as a system, method or program product. Therefore, various aspects of the present application may be specifically implemented in the following forms, namely: a complete hardware implementation, a complete software implementation (including firmware, microcode, etc.), or a combination of hardware and software, which may be collectively referred to as "circuit", "module" or "system" herein.
[0225] Based on the same inventive concept, the embodiment of the present application also provides an electronic device. In one embodiment, the electronic device can be a server or a terminal device. Fig.12 As shown, it is a schematic diagram of the structure of a possible electronic device provided in an embodiment of the present application, Fig.12 In the embodiment, the electronic device 1200 includes: a processor 1210 and a memory 1220.
[0226] The memory 1220 stores a computer program that can be executed by the processor 1210 , and the processor 1210 can execute the steps of the above-mentioned service request processing method by executing the instructions stored in the memory 1220 .
[0227] The memory 1220 may be a volatile memory, such as a random-access memory (RAM); the memory 1220 may also be a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD); or the memory 1220 may be any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 1220 may also be a combination of the above memories.
[0228] The processor 1210 may include one or more central processing units (CPU) or a digital processing unit, etc. The processor 1210 is configured to implement the above-mentioned service request processing method when executing the computer program stored in the memory 1220 .
[0229] In some embodiments, the processor 1210 and the memory 1220 may be implemented on the same chip. In some embodiments, they may also be implemented on separate chips.
[0230] The specific connection medium between the processor 1210 and the memory 1220 is not limited in the embodiment of the present application. In the embodiment of the present application, the processor 1210 and the memory 1220 are connected via a bus as an example. Fig.12 The connections between the other components are only for illustration and are not intended to be limiting. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of description, Fig.12 The diagram shows that only one thick line is used, but this does not mean that there is only one bus or one type of bus.
[0231] Based on the same inventive concept, an embodiment of the present application provides a computer-readable storage medium, which includes a computer program. When the computer program is run on an electronic device, the computer program is used to enable the electronic device to execute the steps of the above-mentioned service request processing method. In some possible implementations, various aspects of the service request processing method provided by the present application can also be implemented in the form of a program product, which includes a computer program. When the program product is run on an electronic device, the computer program is used to enable the electronic device to execute the steps in the above-mentioned service request processing method. For example, the electronic device can execute the following steps: Figure 3 Follow the steps shown in .
[0232] The program product may use any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of readable storage media (a non-exhaustive list) include: an electrical connection with one or more wires, a portable disk, a hard disk, a RAM, a ROM, an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (Compact Disk Read Only Memory, CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0233] The program product of the embodiment of the present application may adopt a CD-ROM and include a computer program, and can be run on an electronic device. However, the program product of the present application is not limited thereto, and in this document, a readable storage medium may be any tangible medium containing or storing a computer program, which may be used by or in combination with a command execution system, apparatus or device.
[0234] A readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, wherein a readable computer program is carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. A readable signal medium may also be any readable medium other than a readable storage medium, which may send, propagate, or transmit a computer program for use by or in conjunction with a command execution system, apparatus, or device.
[0235] Although the preferred embodiments of the present application have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications falling within the scope of the present application.
[0236] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A method for processing a business request, characterized in that: Applied to the first server, comprising: receiving service requests for target services, each service request carrying an object identifier of a target object; For each of the service requests, perform the following operations: Based on an object identifier carried by a business request, use an object coroutine pre-created for the carried object identifier as the object coroutine corresponding to the business request, and add the business request to a coroutine message queue corresponding to the object coroutine; Using the determined object coroutines, respectively obtain and process at least one corresponding business request from the corresponding coroutine message queue.
2. The method according to claim 1, characterized in that Each business request also carries a request operation type; then adding the business request to the coroutine message queue corresponding to the object coroutine includes: Determining an execution priority corresponding to the service request based on a request operation type carried by the service request; Compare the execution priority of the one business request with the execution priorities of each queued business request in the corresponding coroutine message queue, and determine the position of the one business request in the corresponding coroutine message queue based on the comparison result; Based on the position of the one business request in the corresponding coroutine message queue, the one business request is added to the corresponding coroutine message queue.
3. The method according to claim 1, characterized in that Each of the business requests also carries a request operation type; then the adopting of an object coroutine, based on the business request corresponding to each of the at least one business request, sequentially executing the at least one business request includes: Based on the request operation type carried by each of the at least one business request and in combination with the correspondence between each request operation type and each processing interface, determine the processing interface corresponding to each of the at least one business request, each processing interface being used to process the business request of the corresponding request operation type; An object coroutine is used to obtain the processing result of each of the at least one business request by calling the processing interface corresponding to each of the at least one business request.
4. The method according to claim 3, characterized in that The adopting an object coroutine to obtain the processing result of each of the at least one business request by calling the processing interface corresponding to each of the at least one business request includes: When there is a start request in the at least one business request, using the one object coroutine, setting the state of the state machine of the target business to an initial state, calling a processing interface corresponding to the start request, obtaining an initial execution result, and adjusting the state to a second state according to the initial execution result; When a service request associated with the second state is obtained, a processing interface of the service request associated with the second state is called to obtain a second execution result; When the second state is a termination state, a target execution result is obtained according to the second execution result.
5. The method according to any one of claims 1 to 4, characterized in that Before receiving each service request for the target service, the method further includes: When it is determined that each target object uses its own object identifier to log in to the client, a corresponding object coroutine is created for each object identifier of the target object; After each object coroutine created by the method obtains and processes at least one corresponding business request from the corresponding coroutine message queue, the method further includes: When it is determined that each target object uses its own object identifier to exit the client, the corresponding object coroutines are destroyed respectively for the object identifiers of the target objects.
6. The method according to claim 5, characterized in that When it is determined that each target object uses its own object identifier to log in to the client, a corresponding object coroutine is created for each object identifier of the target object, including: When it is determined that each target object uses its own object identifier to log in to the client, obtaining load evaluation information of the first server; When the load evaluation information of the first server meets the local processing conditions, corresponding object coroutines are created in the first server for the object identifiers of the target objects.
7. The method according to claim 6, characterized in that Also includes: When it is determined that each target object uses its own object identifier to log in to the client, and the load evaluation information of the first server does not meet the local processing condition, selecting a second server from each candidate server based on the load evaluation information of each candidate server; The object identifiers of the target objects are sent to the second server, so that the second server creates corresponding object coroutines for the target objects, and uses the created object coroutines to receive and merge the business requests.
8. The method according to any one of claims 1 to 4, characterized in that The receiving of each service request for the target service includes at least one of the following operations: Receiving service requests for target services from various clients; Receive service requests for target services from other servers.
9. The method according to any one of claims 1 to 4, characterized in that After receiving each service request for the target service, before using the object coroutine pre-created for the object identifier carried by a service request as the object coroutine corresponding to the service request based on the object identifier carried by the service request, the method further includes: For each of the service requests, perform the following operations: When it is determined that a service request needs to be assisted by other servers in processing based on a request operation type carried in the service request, determining other servers to which the other objects belong based on object identifiers of other objects carried in the service request; Sending the one service request to the other server, so that the other server executes the one service request to obtain a corresponding processing result; Receive a processing result of the one service request returned by the other server.
10. The method according to any one of claims 1 to 4, characterized in that The method of using each created object coroutine to obtain and process at least one corresponding business request from the corresponding coroutine message queue includes: For each of the object coroutines, perform the following operations respectively: Select an available connection from a database connection pool, where the database connection pool is a shared connection pool created for multiple threads that the application needs to call; The selected available connection is allocated to the one object coroutine for use, and the one object coroutine uses the available connection to perform data operations.
11. A service request processing device, characterized in that: include: A receiving unit, used to receive each service request for a target service, each service request carrying an object identifier of a target object; The enqueue unit is used to perform the following operations for each of the business requests: based on an object identifier carried by a business request, use an object coroutine pre-created for the carried object identifier as the object coroutine corresponding to the business request, and add the business request to a coroutine message queue corresponding to the object coroutine; The processing unit is used to adopt each determined object coroutine to obtain and process at least one corresponding business request from the corresponding coroutine message queue.
12. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of any one of the methods of claims 1 to 10.
13. A computer-readable storage medium, characterized in that: The method comprises a computer program. When the computer program is run on an electronic device, the computer program is used to enable the electronic device to execute the steps of any one of the methods of claims 1 to 10.
14. A computer program product, characterized in that It includes a computer program, which is stored in a computer-readable storage medium. A processor of an electronic device reads and executes the computer program from the computer-readable storage medium, so that the electronic device executes the steps of any method described in claims 1 to 10.
