Connection pool processing method, device, storage medium and equipment
By dynamically determining and adjusting the number of connections that need to be renewed in the connection pool, the problem of unstable response time in elastic TPS scenarios is solved, and the performance optimization of the connection pool and the stability of response time in the case of TPS fluctuations are achieved.
Patent Information
- Application Number
- CN202211050306.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-30
- Publication Date
- 2024-09-03
- Estimated Expiration
- 2042-08-30
AI Technical Summary
In the elastic TPS scenario, the existing connection pool management solution cannot guarantee the stability of the response time, especially when TPS shows peaks and troughs, resulting in insufficient performance of the connection pool when TPS is high, and repeated link establishment and handshakes increase performance when TPS is low. loss.
By determining the number of idle connections and waiting threads in the current host connection pool, dynamically determine the number of current connections that need to be renewed, and perform renewal processing based on this number. Adjust the number of idle connections in the connection pool to adapt to TPS fluctuations and ensure response time. stability.
In elastic TPS scenarios, the number of idle connections in the connection pool can be effectively adjusted to ensure the stability of response times. In low TPS scenarios, the time required to establish a connection between the client and the server can be reduced to improve system performance.
Smart Images

Figure CN115396486B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of big data technology, and in particular to a connection pool processing method, device, storage medium and equipment. Background Art
[0002] In distributed high-concurrency scenarios, connections from the client to the server and between multiple nodes on the server are often managed using a connection pool.
[0003] In one technical solution, the idle connections created in advance are saved in the connection pool. When an HTTP (HyperText Transfer Protocol) request comes, the idle connections in the connection pool are used to access the server, eliminating the overhead of creating and destroying connections. However, for business characteristics such as elastic TPS (Transaction Per Second) scenarios, such as scenarios where TPS presents peaks and troughs, this technical solution cannot guarantee the stability of response time.
[0004] Therefore, how to ensure the stability of response time in elastic TPS scenarios has become a technical problem that needs to be solved urgently. Summary of the invention
[0005] This specification provides a connection pool processing method, apparatus, storage medium and device, which can ensure the stability of response time in elastic TPS scenarios.
[0006] In a first aspect, an embodiment of the present specification provides a connection pool processing method, including:
[0007] Determine the number of idle connections and waiting threads corresponding to the current host connection pool;
[0008] Determine the number of connections that need to be renewed corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads;
[0009] Renew the idle connections in the current host connection pool according to the number of the idle connections and the number of the current connections that need to be renewed.
[0010] In a second aspect, an embodiment of the present specification provides a connection pool processing device, including:
[0011] A business volume determination module is used to determine the number of idle connections and the number of waiting threads corresponding to the current host connection pool;
[0012] A renewal quantity determination module, used to determine the number of connections currently requiring renewal corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads;
[0013] The renewal processing module is used to renew the idle connections in the current host connection pool according to the number of the idle connections and the number of the current connections to be renewed.
[0014] In a third aspect, an embodiment of the present specification provides a computer storage medium, wherein the computer storage medium stores a plurality of instructions, wherein the instructions are suitable for being loaded by a processor and executing the steps of the above method.
[0015] In a fourth aspect, an embodiment of the present specification provides a computer program product comprising instructions, which, when executed on a computer or a processor, enables the computer or the processor to execute the steps of the above-mentioned method.
[0016] In a fifth aspect, an embodiment of the present specification provides an electronic device, comprising: a processor and a memory; wherein the memory stores a computer program, and the computer program is suitable for being loaded by the processor and executing the steps of the above method.
[0017] According to the technical solution of the embodiments of the present specification, on the one hand, by determining the number of idle connections and the number of waiting threads corresponding to the current host connection pool, the number of connections currently requiring renewal corresponding to the current host connection pool is determined, and the number of connections currently requiring renewal corresponding to the current host connection pool can be dynamically determined in an elastic TPS scenario; on the other hand, according to the number of idle connections and the number of connections currently requiring renewal, the idle connections of the current host connection pool are renewed, and the number of idle connections of the host connection pool can be efficiently adjusted based on the current TPS situation, thereby ensuring the stability of the response time in an elastic TPS scenario; on another hand, since it can be ensured that the current host connection pool has a certain long connection persistence, the time for the client to establish a connection with the server can also be reduced for low TPS scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the embodiments of this specification or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this specification. For those skilled in the art, other drawings can be obtained based on these drawings without paying any creative work.
[0019] Figure 1 A system architecture diagram of a connection pool processing device provided in an embodiment of this specification;
[0020] Figure 2 A flow chart of a connection pool processing method provided in an embodiment of this specification;
[0021] Figure 3 A schematic diagram of a process for dynamically determining the number of connections that currently need to be renewed provided in an embodiment of this specification;
[0022] Figure 4 A flowchart of another connection pool processing method provided in an embodiment of this specification;
[0023] Figure 5 A schematic diagram of various modules of a connection pool processing device provided in an embodiment of this specification;
[0024] Figure 6 A schematic diagram of the structure of a connection pool processing device provided in an embodiment of this specification;
[0025] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of this specification. DETAILED DESCRIPTION
[0026] In order to make the features and advantages of this specification more obvious and easy to understand, the technical solutions in the embodiments of this specification will be clearly and completely described below in conjunction with the drawings in the embodiments of this specification. Obviously, the described embodiments are only part of the embodiments of this specification, not all of the embodiments. Based on the embodiments in this specification, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this specification.
[0027] First, the terms involved in one or more embodiments of this specification are explained.
[0028] TPS: The number of transactions processed per second. A transaction is the process in which a client sends a request to a server and the server responds. The client starts timing when it sends a request and ends timing when it receives a response from the server. This is used to calculate the time used and the number of transactions completed, and then the number of transactions processed per second is calculated.
[0029] Elastic TPS scenario: refers to the situation where TPS shows peaks and troughs. For example, after product iteration, TPS shows a low state during the whitelist verification phase; in the review scenario, TPS shows obvious peaks and troughs, etc.
[0030] Connection: used for communication between the client and the server.
[0031] Long Connection: After the client completes the request, it does not actively close the connection and continues to reuse the connection.
[0032] Short Connection: It is used for a client request and the connection will be closed after the request is completed.
[0033] Connection Pool: A centralized pool for storing connections.
[0034] Bidirectional queue: A data structure that allows elements to be inserted and deleted at both the head and tail of the queue.
[0035] RT (Response Time): The time it takes for the client to request the server.
[0036] Free Connection: refers to an available connection that is in an idle state.
[0037] Renew: refers to the client and server maintaining the connection after the connection expires, for example, maintaining the heartbeat connection.
[0038] The current number of connections that need to be renewed (minActiveConnections): The minimum number of idle connections in the connection pool that need to maintain a heartbeat connection with the peer server.
[0039] In one technical solution, in the face of high TPS scenarios, a connection pool management solution based on long connections is provided, but the connection pool management solution cannot dynamically adapt to low TPS scenarios, which will cause repeated chain handshakes, resulting in additional performance loss and increased RT time consumption; in the face of low TPS scenarios, a connection pool management solution based on short connections is provided, but the connection pool management solution does not change the RT time consumption problem. At the same time, once the TPS becomes high, the performance of the connection pool is difficult to meet.
[0040] Based on the above content, the embodiments of this specification provide a connection pool processing method and a connection pool processing device. On the one hand, by determining the number of idle connections and the number of waiting threads corresponding to the current host connection pool, the number of current connections that need to be renewed corresponding to the current host connection pool is determined, and the number of current connections that need to be renewed corresponding to the current host connection pool can be dynamically determined in an elastic TPS scenario; on the other hand, according to the number of idle connections and the number of current connections that need to be renewed, the idle connections of the current host connection pool are renewed, and the number of idle connections of the host connection pool can be efficiently adjusted based on the current TPS situation, thereby ensuring the stability of the response time in an elastic TPS scenario; on the other hand, since it can be ensured that the current host connection pool has a certain long connection persistence, the time for the client to establish a connection with the server can also be reduced for low TPS scenarios.
[0041] The technical solutions of the embodiments of this specification will be described in detail below with reference to the accompanying drawings.
[0042] Figure 1A system architecture diagram of a connection pool processing device provided in an embodiment of this specification.
[0043] like Figure 1 As shown, the system architecture includes a global connection pool 110 and a connection pool processing device 120. The global connection pool 110 includes a plurality of host connection pools, each of which stores at least one connection;
[0044] The connection pool processing device 120 includes a processor 112, and the connection pool processing device 120 maintains a connection pool for each HTTP route. The connection pool manager 120 detects the state information of at least one connection in the connection pool 110, processes the detection result according to the preset connection configuration rule, and obtains the connection configuration strategy for the global connection pool 110; configures the global connection pool 110 according to the connection configuration strategy, so that at least one available idle connection is configured in the global connection pool, and then when the connection pool receives a connection acquisition request, the available idle connection is allocated to the requester of the connection acquisition request.
[0045] The connection with the server is established in advance in the global connection pool 110. Thus, when a connection acquisition request from a client is received, the global connection pool 110 can allocate an idle connection to the client.
[0046] It should be noted that Figure 1 The connection pool processing device in the embodiment can be deployed on the server side, and the connection pool processing is performed by the server. The server can be a physical server including an independent host, or a virtual server carried by a host cluster, or a cloud server.
[0047] based on Figure 1 The system architecture shown below will be combined Figure 2-Figure 4 , the connection pool processing method provided in the embodiments of this specification is introduced in detail.
[0048] See also Figure 2 , is a flow chart of a connection pool processing method provided in an embodiment of this specification, and the connection pool processing method can be executed by a server. Figure 2 As shown, the method of the embodiment of this specification may include the following steps S210 to S230.
[0049] In step S210, the number of idle connections and the number of waiting threads corresponding to the current host connection pool are determined.
[0050] In an example embodiment, the current host connection pool may be a single connection pool or a connection pool among multiple connection pools. The current host connection pool is provided with multiple idle connections, and the idle connections are used for communication connection between the client and the server. For example, for the host server of www.XXX.com:80, the current host connection pool is provided with 80 idle connections.
[0051] The waiting thread corresponding to the current host connection pool is a thread waiting to obtain an idle connection from the current host connection pool. For example, when a new HTTP request to the host Host arrives, if the current host connection pool has no available idle connection, the thread processing the HTTP request waits to form a waiting thread. When the current host connection pool has an available idle connection, the available idle connection is obtained from the current host connection pool.
[0052] Furthermore, the current host connection pool may use a linked list to manage idle connections and waiting thread queues waiting for connections, and obtain the number of idle connections and the number of waiting threads corresponding to the current host connection pool from the linked list.
[0053] It should be noted that other data structures such as queues or bidirectional queues can also be used to manage idle connections and waiting threads corresponding to the current host connection pool, and the number of idle connections and waiting threads corresponding to the current host connection pool can be obtained from the queue or bidirectional queue.
[0054] In step S220, the number of connections currently requiring renewal corresponding to the current host connection pool is determined according to the number of idle connections and the number of waiting threads.
[0055] In the example embodiment, renewal refers to maintaining a connection between the client and the server after the connection expires, such as maintaining a heartbeat connection. The number of connections that currently need to be renewed refers to the number of minimum active connections or minimum idle connections in the connection pool that need to maintain a heartbeat connection with the peer server, i.e., minActiveConnection. Furthermore, the number of connections that currently need to be renewed corresponding to the current host connection pool can be dynamically determined based on the number of idle connections corresponding to the current host connection pool and the number of waiting threads.
[0056] For example, if the current number of waiting threads is greater than a predetermined threshold, such as greater than zero, it is determined that the current number of connections that need to be renewed in the current host connection pool is equal to the number of idle connections in the current host connection pool; if the current number of waiting threads is equal to zero and the number of idle connections is equal to zero, it is determined that the current number of connections that need to be renewed in the current host connection pool remains unchanged; if the number of waiting threads is equal to zero and the number of idle connections is greater than zero, the current number of connections that need to be renewed in the current host connection pool is scaled down according to a predetermined reduction factor.
[0057] For example, assuming that the number of idle connections in the current host connection pool is 10, if the number of waiting threads is greater than zero; then determine that the current number of connections that need to be renewed in the current host connection pool is equal to the number of idle connections, that is, 10, that is, the minimum number of idle connections in the current host connection pool that need to maintain a heartbeat connection with the peer server is 10; if the number of waiting threads is equal to zero and the number of idle connections is 10, then the current number of connections that need to be renewed in the current host connection pool is scaled down according to a predetermined scale-down factor, such as 0.5.
[0058] In step S230, the idle connections in the current host connection pool are renewed according to the number of idle connections and the number of connections that currently need to be renewed.
[0059] In an example embodiment, if there are expired idle connections in the current host connection pool, the number of connections that currently need to be renewed is determined; if the number of idle connections is less than the number of connections that currently need to be renewed, the expired idle connections are taken out from the current host connection pool for renewal processing, for example, the expired idle connections are taken out from the current host connection pool, and the heartbeat connection between the idle connection and the peer server is continued to be maintained; if the number of idle connections is greater than or equal to the number of connections that currently need to be renewed, it is determined that renewal processing is not required, for example, the expired idle connection is deleted from the host connection pool.
[0060] For example, assume that the current host connection pool maintains idle connections through a two-way queue, obtain the head element from the two-way queue, determine whether the head element is expired based on the survival time of the head element, if the head element has expired, determine the number of connections that currently need to be renewed, if the number of idle connections in the current host connection pool is less than the number of connections that currently need to be renewed, that is, minActiveConnections, then it is determined that the expired head element needs to be renewed, that is, it is necessary to maintain the heartbeat connection between the idle connection of the head element and the peer server; if the number of idle connections is greater than or equal to the number of connections that currently need to be renewed, it is determined that no renewal processing is required, and the head element is deleted from the two-way queue.
[0061] according to Figure 2According to the technical solution in the example embodiment, on the one hand, by determining the number of idle connections and the number of waiting threads corresponding to the current host connection pool, the number of connections currently requiring renewal corresponding to the current host connection pool is determined, so that the number of connections currently requiring renewal corresponding to the current host connection pool can be dynamically determined in an elastic TPS scenario; on the other hand, according to the number of idle connections and the number of connections currently requiring renewal, the idle connections in the current host connection pool are renewed, so that the number of idle connections in the host connection pool can be efficiently adjusted based on the current TPS situation, thereby ensuring the stability of the response time in an elastic TPS scenario; on the other hand, since it can be ensured that the current host connection pool has a certain long connection persistence, the time for the client to establish a connection with the server can also be reduced for low TPS scenarios.
[0062] Figure 3 A schematic diagram of a process for dynamically determining the number of connections that currently need to be renewed provided in an embodiment of this specification.
[0063] Reference Figure 3 As shown, in step S310, the number of idle connections and the number of waiting threads corresponding to the current host connection pool are obtained.
[0064] In an example embodiment, the current host connection pool may use a linked list to manage idle connections and waiting thread queues waiting for connections, and obtain the number of idle connections and the number of waiting threads corresponding to the current host connection pool from the linked list.
[0065] It should be noted that other data structures such as queues or bidirectional queues can also be used to manage idle connections and waiting threads corresponding to the current host connection pool, and the number of idle connections and waiting threads corresponding to the current host connection pool can be obtained from the queue or bidirectional queue.
[0066] In step S320, if the number of waiting threads is greater than a predetermined threshold, it is determined that the number of connections currently requiring renewal in the current host connection pool is equal to the number of idle connections in the current host connection pool.
[0067] In an example embodiment, if the number of waiting threads is greater than a predetermined threshold, such as zero, it indicates that all idle connections in the current host connection pool need to remain connected, and therefore, the number of current connections to be renewed in the current host connection pool is equal to the number of idle connections in the current host connection pool.
[0068] Further, in the example embodiment, if the number of waiting threads is greater than a predetermined threshold, such as zero, it indicates that the connection capacity of the current host connection pool is small and it is difficult to meet the processing requirements of the current client requests. Therefore, it is necessary to expand the connection capacity of the current host connection pool, for example, according to a predetermined expansion factor.
[0069] For example, let minActiveConnections be the number of connections that need to be renewed in the current host connection pool, freeConnections.size be the number of idle connections, that is, the queue length of the idle queue of the host connection pool, maxConnections be the maximum number of connections, that is, the connection capacity, and waitingThreads.size be the number of waiting threads, that is, the queue length of the waiting thread queue. The following rules can be used to determine the number of connections that need to be renewed in the current host connection pool:
[0070] If waitingThreads.size>0, then minActiveConnections=freeConnections.size, and maxConnections=maxConnections*1.5, where the maximum connection capacity of the current host connection pool is a fixed value, the default is 50, and 1.5 is the expansion factor.
[0071] In step S330, if the number of current waiting threads is equal to zero and the number of idle connections is equal to zero, it is determined that the number of current connections to be renewed in the current host connection pool remains unchanged.
[0072] In the example embodiment, if the number of currently waiting threads is equal to zero and the number of idle connections is equal to zero, it indicates that the connection capacity of the current host connection pool just meets the processing requirements of the current client request and does not need to be expanded or reduced. Therefore, it is determined that the current number of connections to be renewed in the current host connection pool remains unchanged. Further, the current number of connections to be renewed in the current host connection pool can be determined by the following rules:
[0073] If freeConnections.size==0 and waitingThreads.size==0, then minActiveConnections=minActiveConections, where the minimum number of idle connections in the current host connection pool is a fixed value, and the default value is 2.
[0074] In step S340, if the number of waiting threads is equal to zero and the number of idle connections is greater than zero, the number of connections that currently need to be renewed in the current host connection pool is scaled down according to a predetermined scaling factor.
[0075] In the example embodiment, if the number of waiting threads is equal to zero and the number of idle connections is greater than zero, it indicates that the number of idle connections in the current host connection pool is large, and the idle connections in the current host connection pool need to be reduced. Further, the number of connections that need to be renewed in the current host connection pool can be determined by the following rules:
[0076] If freeConnections.size>0and waitingThreads.size==0, then the number of connections that need to be renewed is minActiveConnections=0.5*minActiveConnections, where 0.5 is the reduction factor and the minimum value of idle connections in the current host connection pool is 1.
[0077] according to Figure 3 The technical solution in the example embodiment, on the one hand, dynamically determines the data volume of the current host connection pool that needs to be renewed based on the dynamic expansion and contraction connection pool processing rules, and can efficiently adjust the capacity of the current host connection pool based on the current TPS situation to ensure the stability of the connection pool system performance, so as to cope with sudden traffic peaks; on the other hand, since the capacity of the current host connection pool can be adjusted based on the current TPS situation, the stability of the response time can be ensured in the elastic TPS scenario, avoiding the waste of connection pool system performance overhead.
[0078] Figure 4 A flowchart of another connection pool processing method provided in an embodiment of this specification.
[0079] Reference Figure 4 As shown, in step S410 , the head element is taken from the idle connection queue of the host connection pool 110 .
[0080] In an example embodiment, multiple host connection pools under the global connection pool ConnectionPool are traversed to determine the available bidirectional queue FreeConnections of idle connections corresponding to the traversed current host connection pool 110. For the FreeConnections bidirectional queue, the first element of the queue is taken, and the survival time of the first element of the queue is analyzed based on the idle connection handler idleConnectionHandler.
[0081] In step S420, it is determined whether the head element of the queue is expired.
[0082] In an exemplary embodiment, if the survival time has exceeded the expiration time, it is determined that the idle connection corresponding to the first element has expired, and step S430 is executed; if the survival time has not exceeded the expiration time, it is determined that the idle connection corresponding to the first element has not expired, and the first element is put back into the host connection pool 110.
[0083] In step S430, it is determined whether the number of idle connections is greater than or equal to the number of connections currently requiring renewal.
[0084] In an example embodiment, if the number of idle connections is greater than or equal to the number of connections that currently need to be renewed, that is, the number of FreeConnections exceeds the number of minActiveConnections, it is determined that renewal is not required and the process proceeds to step S440; if the number of idle connections is less than the number of connections that currently need to be renewed, it is determined that renewal is required and the process proceeds to step S450.
[0085] In step S440, the head element is removed.
[0086] In an example embodiment, if the number of idle connections is greater than or equal to the number of connections that currently need to be renewed, it means that the head element does not need to be renewed, and the head element is removed from the idle connection queue of the host connection pool. Further, the global connection pool includes an idle connection linked list corresponding to the idle connections of multiple host connection pools, and the head element, i.e., the head idle connection, is removed from the idle connection linked list of the global connection pool.
[0087] For example, for an expired idle connection, if renewal is not required, the idle connection is removed from the queue, that is, removed from the idle connection queue of the host connection pool and removed from the idle connection linked list of the global connection pool.
[0088] In step S450, the head element of the queue is renewed.
[0089] In an exemplary embodiment, a heartbeat connection is established between the idle connection corresponding to the head element and the peer server 320 .
[0090] For example, for an expired idle connection, if renewal is required, the idle connection is taken out from the bidirectional queue of the host connection pool, a heartbeat connection is established with the peer server 320 (if the peer service has an expiration time), and then the releaseConnection operation is executed.
[0091] In step S460, the head element is removed and added to the end of the queue.
[0092] In the example embodiment, for an expired idle connection, after releasing the idle connection, the idle connection is removed from the bidirectional queue and added to the end of the bidirectional queue, that is, the idle connection is taken out from the bidirectional queue and removed, and the idle connection is released after establishing a heartbeat connection with the peer server 320, and the idle connection is added to the end of the bidirectional queue. Further, the idle connection is added to the end of the idle connection linked list of the global connection pool. The bidirectional queue of the host connection pool is cached in the cache 330.
[0093] In step S470, continue traversing the next connection in the connection pool.
[0094] according to Figure 4 The technical solution in the example embodiment, on the one hand, comprehensively utilizes the bidirectional queue and LRU (Least Recently Used) algorithm to renew, close, and acquire the idle connections of the connection pool, which can ensure that the connection pool has a certain number of valid long connections, and for low TPS scenarios, it can also reduce the time for the client to establish a connection with the server; on the other hand, it can automatically clean up expired connections to ensure the validity of idle connections.
[0095] In addition, in some example embodiments, if the number of idle connections is greater than or equal to the number of connections that currently need to be renewed, the number of idle connections that currently need to be renewed is taken from the end of the bidirectional queue for renewal, that is, the connections that exceed the number of connections that currently need to be renewed are removed from the head of the bidirectional queue, and the remaining idle connections are renewed. For example, if the number of idle connections is 100 and the number of connections that currently need to be renewed is 30, 30 idle connections can be taken from the end of the bidirectional queue for renewal. If none of the 30 idle connections have expired, no renewal is required, or 70 connections can be removed from the head of the bidirectional queue, and the remaining 30 idle connections are renewed.
[0096] According to the technical solution in the above embodiment, when the number of idle connections is much larger than the number of connections currently requiring renewal, the connections exceeding the number currently requiring renewal are removed, so that the size of the connection pool can be dynamically reduced based on the current TPS scenario, thereby reducing the system performance overhead of the connection pool.
[0097] Figure 5 A schematic diagram of various modules of a connection pool processing device provided in an embodiment of this specification.
[0098] Reference Figure 5 As shown, the connection pool processing device includes a client request management class 510, which is used to process the connection acquisition request received from the client. The client request management class 510 includes: an idle renewal processing class 512, an idle connection processing class 514 and a global connection pool 516.
[0099] The idle renewal processing class 512, namely idleRenewWorker, is used to process the expiration and renewal of the idle connection of the connection pool, for example, through the renewal and expiration function renewAndExpire(), to process whether the idle connection of the connection pool expires and renews. The idle connection processing class 514, namely idleConnectionHandler, includes idle connections maintained by a Map data structure, wherein the keyword key is the idle connection HttpConnection, and the value value is the survival time corresponding to the idle connection.
[0100] The global connection pool 516 includes idle connections maintained by a linked list, namely Linkdedlist <httpconnection>And multiple host connection pools, the global connection pool 516 and the host connection pool are in a one-to-many relationship, that is, a 1:N relationship. The host connection pool corresponding to the global connection pool 516 is maintained through a Map data structure, in which the keyword key is the host configuration 518, that is, HostConfiguration, and the value is the host connection pool HostConnectionPool. The host configuration 518 includes the host HttpHost and the proxy host ProxyHost.
[0101] The host connection pool 520 includes an idle connection queue Deque and a waiting thread list LinkedList. The renewal calculation processing class 522, namely ActiveCalcWorker, is used to calculate the number of connections that need to be renewed and whether the idle connections in the idle connection queue need to be renewed. For example, the calculation function calc() is used to calculate the number of connections that need to be renewed and whether the idle connections in the idle connection queue need to be renewed.
[0102] Further, in the example embodiment, (1) the host connection pool 520 under the current global connection pool 516ConnectionPool is traversed to obtain the bidirectional queue of available idle connections FreeConnections of the host connection pool 520. (2) For the bidirectional queue Deque of idle connections, the head element is taken, and the survival time of the idle connection corresponding to the head element is analyzed based on the idle connection processing class 514, i.e., idleConnectionHandler. If the survival time has exceeded the expiration time, steps (3) and (4) are performed. (3) In the case where the number of idle connections is greater than or equal to the current number of connections to be renewed minActiveConnections, it is determined that the head element does not need to be renewed; in the case where the number of idle connections is less than the current number of connections to be renewed minActiveConnections, it is determined that the head element needs to be renewed. (4) If the idle connection corresponding to the first element of the queue has expired, if renewal is required, the first element of the queue in the host thread pool will be removed, and the heartbeat connection of the peer service will be performed (if the peer service has an expiration time), and then the release connection releaseConnection operation will be executed, and the first element of the queue connection will be added to the end of the bidirectional queue again (that is, the element will be placed at the end of the queue after normal use and release), and the first element of the queue will be added to the end of the spatial connection linked list LinkedList. (5) If the idle connection corresponding to the first element of the queue has expired, if renewal is not required, the idle connection corresponding to the first element of the queue will be directly removed from the queue, that is, removed from the linked list of the global connection pool ConnectionPool and removed from the bidirectional queue of the host connection pool HostConnectionPool.
[0103] Furthermore, in an exemplary embodiment, the number of connections currently requiring renewal corresponding to the current host connection pool may be dynamically determined according to the number of idle connections corresponding to the current host connection pool and the number of waiting threads.
[0104] For example, if the current number of waiting threads is greater than a predetermined threshold, such as greater than zero, it is determined that the current number of connections that need to be renewed in the current host connection pool is equal to the number of idle connections in the current host connection pool; if the current number of waiting threads is equal to zero and the number of idle connections is equal to zero, it is determined that the current number of connections that need to be renewed in the current host connection pool remains unchanged; if the number of waiting threads is equal to zero and the number of idle connections is greater than zero, the current number of connections that need to be renewed in the current host connection pool is scaled down according to a predetermined reduction factor.
[0105] For example, let minActiveConnections be the number of connections that need to be renewed in the current host connection pool, freeConnections.size be the number of idle connections, that is, the queue length of the idle queue of the host connection pool, maxConnections be the maximum number of connections, that is, the connection capacity, and waitingThreads.size be the number of waiting threads, that is, the queue length of the waiting thread queue. The following rules can be used to determine the number of connections that need to be renewed in the current host connection pool:
[0106] (1) If waitingThreads.size>0, then minActiveConnections=freeConnections.size, and maxConnections=maxConnections*1.5, where the maximum connection capacity of the current host connection pool is a fixed value, the default is 50, and 1.5 is the expansion factor.
[0107] (2) If freeConnections.size==0 and waitingThreads.size==0, then minActiveConnections=minActiveConections, where the minimum number of idle connections in the current host connection pool is a fixed value, and the default value is 2.
[0108] (3) If freeConnections.size>0and waitingThreads.size==0, then the number of connections that need to be renewed is minActiveConnections=0.5*minActiveConnections, where 0.5 is the shrinkage factor and the minimum value of idle connections in the current host connection pool is 1.
[0109] according to Figure 5 According to the technical solution in the example embodiment, on the one hand, a bidirectional queue and an LRU algorithm are used to renew, close, and acquire idle connections in the connection pool, thereby ensuring that the connection pool has a certain number of valid long connections and reducing the time it takes to establish a connection between the client and the server for low TPS scenarios; on the other hand, since the capacity of the current host connection pool can be adjusted based on the current TPS situation, the stability of the response time can be ensured in an elastic TPS scenario, thereby avoiding the waste of performance overhead of the connection pool system; on the other hand, based on the dynamic expansion and contraction connection pool processing rules, the data volume of the current host connection pool that needs to be renewed is dynamically determined in real time, and the capacity of the current host connection pool can be adjusted based on the current TPS situation efficiently, ensuring the stability of the connection pool system performance, thereby being able to cope with sudden traffic peaks.
[0110] based on Figure 1 The system architecture shown below will be combined Figure 6 , the connection pool processing device provided in the embodiment of this specification is introduced in detail. It should be noted that, Figure 6 The connection pool handler in the Figure 2-Figure 4 For the convenience of explanation, only the part related to the embodiment of this specification is shown. For the specific technical details not disclosed, please refer to this specification. Figure 2-Figure 4 The embodiment shown.
[0111] See also Figure 6 , is a schematic diagram of the structure of a connection pool processing device provided in the embodiment of this specification. Figure 6 As shown, the connection pool processing device 600 of the embodiment of this specification may include: a business volume determination module 610, a renewal quantity determination module 620 and a renewal processing module 630.
[0112] The traffic volume determination module 610 is used to determine the number of idle connections and the number of waiting threads corresponding to the current host connection pool;
[0113] A renewal quantity determination module 620 is used to determine the number of connections currently requiring renewal corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads;
[0114] The renewal processing module 630 is used to renew the idle connections in the current host connection pool according to the number of the idle connections and the number of the current connections to be renewed.
[0115] In some example embodiments, based on the above solution, the renewal quantity determination module 620 is configured to:
[0116] If the number of the waiting threads is greater than a predetermined threshold, it is determined that the number of current connections to be renewed in the current host connection pool is equal to the number of idle connections in the current host connection pool.
[0117] In some example embodiments, based on the above solution, the apparatus 600 further includes:
[0118] The expansion processing module is used to expand the connection capacity of the current host connection pool according to a predetermined expansion factor.
[0119] In some example embodiments, based on the above solution, the capacity expansion processing module includes:
[0120] A connection capacity determination module, used to determine the connection capacity of the current host connection pool according to the number of service domain names in the current host connection pool and the current number of transactions processed per second (TPS);
[0121] The expansion unit is used to expand the connection capacity of the current host connection pool according to a predetermined expansion factor.
[0122] In some example embodiments, based on the above solution, the renewal quantity determination module 620 is configured to:
[0123] If the number of waiting threads is equal to zero and the number of idle connections is equal to zero, it is determined that the number of current connections to be renewed in the current host connection pool remains unchanged.
[0124] In some example embodiments, based on the above solution, the renewal quantity determination module 620 is configured to:
[0125] If the number of waiting threads is equal to zero and the number of idle connections is greater than zero, the number of connections currently requiring renewal in the current host connection pool is scaled down according to a predetermined scaling-down factor.
[0126] In some example embodiments, based on the above solution, the renewal processing module 630 includes:
[0127] A renewal quantity acquisition unit, configured to acquire the number of connections currently requiring renewal if there are any expired idle connections in the current host connection pool;
[0128] The renewal processing unit is used to take out the expired idle connection from the current host connection pool for renewal processing if the number of the idle connections is less than the number of the current connections to be renewed.
[0129] In some example embodiments, based on the above solution, the current host connection pool maintains the idle connection through a bidirectional queue, and the renewal quantity acquisition unit is configured as follows:
[0130] Taking out the idle connection at the head of the queue from the bidirectional queue, and determining the survival time of the idle connection at the head of the queue;
[0131] If it is determined based on the survival time that the idle connection at the head of the queue has expired, the number of the current connections that need to be renewed is obtained,
[0132] The renewal processing unit is configured to:
[0133] Establishing a heartbeat connection between the idle connection at the head of the queue and the opposite server;
[0134] Remove the idle connection at the head of the queue from the bidirectional queue;
[0135] The idle connection at the head of the queue is added to the tail of the bidirectional queue.
[0136] In some example embodiments, based on the above solution, the traffic volume determination module 610 is configured to:
[0137] Traverse multiple host connection pools under the global connection pool to determine the number of idle connections and the number of waiting threads corresponding to the current host connection pool traversed.
[0138] In some example embodiments, based on the above solution, the global connection pool includes an idle connection linked list corresponding to the idle connections of the multiple host connection pools, and after adding the idle connection at the head of the queue to the tail of the bidirectional queue, the device further includes:
[0139] The element adding unit is used to add the idle connection at the head of the queue to the end of the idle connection linked list of the global connection pool after adding the idle connection at the head of the queue to the end of the bidirectional queue.
[0140] In some example embodiments, based on the above scheme, the device also includes: a removal module, which is used to determine that renewal processing is not required if the number of idle connections is greater than or equal to the number of connections currently requiring renewal; remove the idle connection at the head of the queue from the bidirectional queue; and remove the idle connection at the head of the queue from the idle connection linked list of the global connection pool.
[0141] According to this manual Figure 6 The technical solution of the embodiment of the present invention, on the one hand, determines the number of idle connections and the number of waiting threads corresponding to the current host connection pool, and determines the number of connections currently requiring renewal corresponding to the current host connection pool, so that the number of connections currently requiring renewal corresponding to the current host connection pool can be dynamically determined in an elastic TPS scenario; on the other hand, the idle connections of the current host connection pool are renewed according to the number of idle connections and the number of connections currently requiring renewal, so that the number of idle connections of the host connection pool can be efficiently adjusted based on the current TPS situation, thereby ensuring the stability of the response time in an elastic TPS scenario; on the other hand, since a certain long connection persistence can be ensured, the time for establishing a connection between the client and the server can also be reduced for low TPS scenarios.
[0142] The above is a schematic scheme of a connection pool processing device of an embodiment of this specification. It should be noted that the technical scheme of the connection pool processing device and the technical scheme of the connection pool processing method described above belong to the same concept, and the details not described in detail in the technical scheme of the connection pool processing device can be referred to the description of the technical scheme of the connection pool processing method described above.
[0143] The present specification also provides a computer storage medium, which can store a plurality of program instructions, which are suitable for being loaded and executed by a processor as described above. Figure 2 to Figure 4 The method steps of the embodiment shown in the figure can be found in the specific implementation process. Figure 2 to Figure 4 The specific description of the illustrated embodiment will not be repeated here.
[0144] The embodiment of the present specification also provides a computer program product, which stores at least one instruction, and the at least one instruction is loaded and executed by a processor as described above. Figure 2 to Figure 4 The connection pool processing method of the embodiment shown in the figure can be found in the specific execution process. Figure 2 to Figure 4 The specific description of the illustrated embodiment will not be repeated here.
[0145] Please refer to Figure 7 , which shows a schematic diagram of the structure of an electronic device provided by an exemplary embodiment of this specification. The electronic device in this specification may include one or more of the following components: a processor 710, a memory 720, an input device 730, an output device 740, and a bus 750. The processor 710, the memory 720, the input device 730, and the output device 740 may be connected via a bus 750.
[0146] The processor 710 may include one or more processing cores. The processor 710 uses various interfaces and lines to connect various parts of the entire electronic device, and executes various functions and processes data of the electronic device 700 by running or executing instructions, programs, code sets or instruction sets stored in the memory 720, and calling data stored in the memory 720. Optionally, the processor 710 can be implemented in at least one hardware form of digital signal processing (DSP), field-programmable gate array (FPGA), and programmable logic array (PLA). The processor 710 can integrate one or a combination of a central processing unit (CPU), a graphics processing unit (GPU), and a modem. Among them, the CPU mainly processes the operating system, user interface, and application programs; the GPU is responsible for rendering and drawing display content; and the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 710, but may be implemented separately through a communication chip.
[0147] The memory 720 may include a random access memory (RAM) or a read-only memory (ROM). Optionally, the memory 720 includes a non-transitory computer-readable storage medium. The memory 720 may be used to store instructions, programs, codes, code sets or instruction sets. The memory 720 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (e.g., a touch function, a sound playback function, an image playback function, etc.), instructions for implementing the following various method embodiments, etc. The operating system may be an Android system, including a system deeply developed based on the Android system, an IOS system developed by Apple, including a system deeply developed based on the IOS system or other systems.
[0148] In order for the operating system to distinguish the specific application scenarios of third-party applications, it is necessary to open up data communication between third-party applications and the operating system so that the operating system can obtain the current scenario information of third-party applications at any time, and then perform targeted system resource adaptation based on the current scenario.
[0149] The input device 730 is used to receive input instructions or data, and the input device 730 includes but is not limited to a keyboard, a mouse, a camera, a microphone or a touch device. The output device 740 is used to output instructions or data, and the output device 740 includes but is not limited to a display device and a speaker. In one example, the input device 730 and the output device 740 can be combined, and the input device 730 and the output device 740 are touch screen displays.
[0150] In addition, those skilled in the art will appreciate that the structure of the electronic device shown in the above drawings does not constitute a limitation on the electronic device, and the electronic device may include more or fewer components than shown, or combine certain components, or arrange the components differently. For example, the electronic device also includes a radio frequency circuit, an input unit, a sensor, an audio circuit, a wireless fidelity (WiFi) module, a power supply, a Bluetooth module and other components, which will not be described in detail here.
[0151] exist Figure 7 In the electronic device shown, the processor 710 can be used to call the connection pool processing application stored in the memory 720, and specifically perform the following operations:
[0152] Determine the number of idle connections and waiting threads corresponding to the current host connection pool;
[0153] Determine the number of connections that need to be renewed corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads;
[0154] Renew the idle connections in the current host connection pool according to the number of the idle connections and the number of the current connections that need to be renewed.
[0155] In some example embodiments, when the processor 710 determines the number of current connections to be renewed corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads, the processor 710 specifically performs the following operations:
[0156] If the number of the waiting threads is greater than a predetermined threshold, it is determined that the number of current connections to be renewed in the current host connection pool is equal to the number of idle connections in the current host connection pool.
[0157] In some example embodiments, the processor 710 further performs the following operations:
[0158] The connection capacity of the current host connection pool is expanded according to a predetermined expansion factor.
[0159] In some example embodiments, when the processor 710 performs the process of expanding the connection capacity of the current host connection pool according to the predetermined expansion factor, the processor 710 specifically performs the following operations:
[0160] Determine the connection capacity of the current host connection pool according to the number of service domain names in the current host connection pool and the current number of transactions processed per second (TPS);
[0161] The connection capacity of the current host connection pool is expanded according to the predetermined expansion factor.
[0162] In some example embodiments, when the processor 710 determines the number of current connections to be renewed corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads, the processor 710 specifically performs the following operations:
[0163] If the number of waiting threads is equal to zero and the number of idle connections is equal to zero, it is determined that the number of current connections to be renewed in the current host connection pool remains unchanged.
[0164] In some example embodiments, when the processor 710 determines the number of current connections to be renewed corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads, the processor 710 specifically performs the following operations:
[0165] If the number of waiting threads is equal to zero and the number of idle connections is greater than zero, the number of connections currently requiring renewal in the current host connection pool is scaled down according to a predetermined scaling-down factor.
[0166] In some example embodiments, when the processor 710 performs the renewal process on the idle connections in the current host connection pool according to the number of the idle connections and the number of connections currently requiring renewal, the processor 710 specifically performs the following operations:
[0167] If there are expired idle connections in the current host connection pool, the number of the current connections that need to be renewed is obtained;
[0168] If the number of the idle connections is less than the number of the current connections to be renewed, the expired idle connections are taken out from the current host connection pool for renewal.
[0169] In some example embodiments, the processor 710 performs the following operations when executing the current host connection pool maintaining the idle connection through a bidirectional queue and obtaining the number of the current connections to be renewed if there are expired idle connections in the current host connection pool:
[0170] Taking out the idle connection at the head of the queue from the bidirectional queue, and determining the survival time of the idle connection at the head of the queue;
[0171] If it is determined based on the survival time that the idle connection at the head of the queue has expired, the number of the current connections that need to be renewed is obtained,
[0172] The step of taking out an idle connection from the current host connection pool for renewal processing includes:
[0173] Establishing a heartbeat connection between the idle connection at the head of the queue and the opposite server;
[0174] Remove the idle connection at the head of the queue from the bidirectional queue;
[0175] The idle connection at the head of the queue is added to the tail of the bidirectional queue.
[0176] In some example embodiments, when the processor 710 performs the step of determining the number of idle connections and the number of waiting threads corresponding to the current host connection pool, the processor 710 specifically performs the following operations:
[0177] Traverse multiple host connection pools under the global connection pool to determine the number of idle connections and the number of waiting threads corresponding to the current host connection pool traversed.
[0178] In some example embodiments, the global connection pool includes an idle connection linked list corresponding to the idle connections of the plurality of host connection pools, and after adding the idle connection at the head of the queue to the tail of the bidirectional queue, the processor 710 further performs the following operations:
[0179] The idle connection at the head of the queue is added to the end of the idle connection linked list of the global connection pool.
[0180] In some example embodiments, the processor 710 further performs the following operations:
[0181] If the number of idle connections is greater than or equal to the number of connections currently requiring renewal, it is determined that renewal processing is not required;
[0182] Remove the idle connection at the head of the queue from the bidirectional queue;
[0183] The head idle connection is removed from the idle connection linked list of the global connection pool.
[0184] According to this manual Figure 7 The technical solution of the embodiment of the present invention, on the one hand, determines the number of idle connections and the number of waiting threads corresponding to the current host connection pool, and determines the number of connections currently requiring renewal corresponding to the current host connection pool, so that the number of connections currently requiring renewal corresponding to the current host connection pool can be dynamically determined in an elastic TPS scenario; on the other hand, the idle connections of the current host connection pool are renewed according to the number of idle connections and the number of connections currently requiring renewal, so that the number of idle connections of the host connection pool can be efficiently adjusted based on the current TPS situation, thereby ensuring the stability of the response time in an elastic TPS scenario; on the other hand, since a certain long connection persistence can be ensured, the time for establishing a connection between the client and the server can also be reduced for low TPS scenarios.
[0185] The above is a schematic scheme of an electronic device in an embodiment of this specification. It should be noted that the technical scheme of the electronic device and the technical scheme of the connection pool processing method described above are of the same concept, and the details not described in detail in the technical scheme of the electronic device can be referred to the description of the technical scheme of the connection pool processing method described above.
[0186] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing related hardware through a computer program, and the computer program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. The storage medium of the computer program can be a disk, an optical disk, a read-only storage memory, or a random access memory, etc.
[0187] The above disclosure is only the preferred embodiment of this specification, which certainly cannot be used to limit the scope of rights of this specification. Therefore, equivalent changes made according to the claims of this specification are still within the scope covered by this specification.
[0188] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.< / httpconnection>
Claims
1. A connection pool processing method, comprising: Determine the number of idle connections and waiting threads corresponding to the current host connection pool; Determine the number of connections that need to be renewed corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads; Renew the idle connections in the current host connection pool according to the number of the idle connections and the number of the current connections to be renewed; The determining, according to the number of the idle connections and the number of the waiting threads, the number of connections currently requiring renewal corresponding to the current host connection pool includes: If the number of waiting threads is equal to zero and the number of idle connections is greater than zero, the number of connections currently requiring renewal in the current host connection pool is scaled down according to a predetermined scaling-down factor.
2. The method according to claim 1, wherein: The determining, according to the number of the idle connections and the number of the waiting threads, the number of connections currently requiring renewal corresponding to the current host connection pool includes: If the number of the waiting threads is greater than a predetermined threshold, it is determined that the number of current connections to be renewed in the current host connection pool is equal to the number of idle connections in the current host connection pool.
3. The method according to claim 2, wherein: The method further comprises: The connection capacity of the current host connection pool is expanded according to a predetermined expansion factor.
4. The method according to claim 3, wherein: The step of expanding the connection capacity of the current host connection pool according to a predetermined expansion factor includes: Determine the connection capacity of the current host connection pool according to the number of service domain names in the current host connection pool and the current number of transactions processed per second (TPS); The connection capacity of the current host connection pool is expanded according to the predetermined expansion factor.
5. The method according to claim 1, wherein: The determining, according to the number of the idle connections and the number of the waiting threads, the number of connections currently requiring renewal corresponding to the current host connection pool includes: If the number of waiting threads is equal to zero and the number of idle connections is equal to zero, it is determined that the number of current connections to be renewed in the current host connection pool remains unchanged.
6. The method according to any one of claims 1 to 5, wherein: The renewing process of the idle connections in the current host connection pool according to the number of the idle connections and the number of connections currently requiring renewal includes: If there are expired idle connections in the current host connection pool, the number of the current connections that need to be renewed is obtained; If the number of the idle connections is less than the number of the current connections to be renewed, the expired idle connections are taken out from the current host connection pool for renewal processing.
7. The method according to claim 6, wherein: The current host connection pool maintains the idle connection through a bidirectional queue, and if the current host connection pool has expired idle connections, the number of the current connections to be renewed is obtained, including: Taking out the idle connection at the head of the queue from the bidirectional queue, and determining the survival time of the idle connection at the head of the queue; If it is determined based on the survival time that the idle connection at the head of the queue has expired, the number of the current connections that need to be renewed is obtained, The step of taking out an idle connection from the current host connection pool for renewal processing includes: Establishing a heartbeat connection between the idle connection at the head of the queue and the opposite server; Remove the idle connection at the head of the queue from the bidirectional queue; The idle connection at the head of the queue is added to the tail of the bidirectional queue.
8. The method according to claim 7, wherein: Determining the number of idle connections and the number of waiting threads corresponding to the current host connection pool includes: Traverse multiple host connection pools under the global connection pool to determine the number of idle connections and the number of waiting threads corresponding to the current host connection pool traversed.
9. The method according to claim 8, wherein: The global connection pool includes an idle connection linked list corresponding to the idle connections of the plurality of host connection pools. After adding the idle connection at the head of the queue to the tail of the bidirectional queue, the method further includes: The idle connection at the head of the queue is added to the end of the idle connection linked list of the global connection pool.
10. The method according to claim 9, wherein: The method further comprises: If the number of idle connections is greater than or equal to the number of connections currently requiring renewal, it is determined that renewal processing is not required; Remove the idle connection at the head of the queue from the bidirectional queue; The head idle connection is removed from the idle connection linked list of the global connection pool.
11. A connection pool processing device, comprising: A business volume determination module is used to determine the number of idle connections and the number of waiting threads corresponding to the current host connection pool; A renewal quantity determination module, used to determine the number of connections currently requiring renewal corresponding to the current host connection pool according to the number of idle connections and the number of waiting threads; A renewal processing module, used for renewing the idle connections in the current host connection pool according to the number of the idle connections and the number of the current connections to be renewed; The renewal quantity determination module is configured to: If the number of waiting threads is equal to zero and the number of idle connections is greater than zero, the number of connections currently requiring renewal in the current host connection pool is scaled down according to a predetermined scaling-down factor.
12. A computer storage medium storing a plurality of instructions, wherein the instructions are suitable for being loaded by a processor and executing the steps of the method according to any one of claims 1 to 10.
13. An electronic device comprising: A processor and a memory; wherein the memory stores a computer program, and the computer program is suitable for being loaded by the processor and executing the steps of the method as claimed in any one of claims 1 to 10.
14. A computer program product comprising instructions, which, when executed on a computer or a processor, causes the computer or the processor to execute the steps of the method according to any one of claims 1 to 10.
