A tcp connection multiplexing method and terminal

CN116668514BActive Publication Date: 2026-09-25FUJIAN TIANQUAN EDUCATION TECH LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310766376.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-06-27
Publication Date
2026-09-25
Estimated Expiration
2043-06-27

AI Technical Summary

Technical Problem

但是TCP连接在创建过程中需要进行3次握手,因此,若每一个会话都需要通过一个TCP连接来进行维护,那么存在多个会话时,创建多个TCP连接的过程必然会带来一定的时间消耗,影响系统性能

Benefits of technology

[0014]本发明的有益效果在于:通过判断TCP连接池中是否存在空闲连接,优先将数据请求合理分配至具有空闲资源的TCP连接进行转发,实现了一个TCP连接上支持多个数据请求连接的功能,进而减少TCP连接创建过程所造成的时间消耗,有效提高系统性能。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116668514B_ABST
    Figure CN116668514B_ABST
Patent Text Reader

Abstract

The application provides a TCP connection multiplexing method and a terminal. By judging whether there is an idle connection in a TCP connection pool, data requests are preferentially and reasonably distributed to TCP connections with idle resources for forwarding, so that the function of supporting multiple data request connections on one TCP connection is realized, time consumption caused by a TCP connection creation process is reduced, and system performance is effectively improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of TCP connection technology, and in particular to a TCP connection multiplexing method and terminal. Background Technology

[0002] Currently, all systems require data storage, thus each system relies on a database for data storage. Databases include relational databases and non-relational databases. As the amount of system data increases, the demands on data retrieval also rise. In traditional database connections, executing an SQL (Structured Query Language) operation requires creating a session beforehand. Creating a session, in turn, requires a dedicated TCP (Transmission Control Protocol) connection. That is, current database connection methods require first creating a TCP connection, then maintaining a session through that TCP connection, and finally executing SQL operations through this session. However, TCP connection creation involves a three-way handshake. Therefore, if each session needs to be maintained through a separate TCP connection, the creation of multiple TCP connections for multiple sessions inevitably incurs time consumption, impacting system performance. Summary of the Invention

[0003] The technical problem to be solved by this invention is to provide a TCP connection multiplexing method and terminal to reduce the time consumption of TCP connection creation and improve system performance.

[0004] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows:

[0005] A TCP connection multiplexing method includes:

[0006] Receive data requests from clients;

[0007] Determine if there is an idle connection in the TCP connection pool. If so, use the idle connection as the target connection and forward the data request to the corresponding database through the target connection.

[0008] Otherwise, obtain the session list of the TCP connection pool, select a target connection based on the session list, and forward the data request to the corresponding database through the target connection.

[0009] To solve the above-mentioned technical problems, another technical solution adopted by the present invention is as follows:

[0010] A TCP connection multiplexing terminal includes a memory, a processor, and a computer program stored in the memory and running on the processor. When the processor executes the computer program, it performs the following steps:

[0011] Receive data requests from clients;

[0012] Determine if there is an idle connection in the TCP connection pool. If so, use the idle connection as the target connection and forward the data request to the corresponding database through the target connection.

[0013] Otherwise, obtain the session list of the TCP connection pool, select a target connection based on the session list, and forward the data request to the corresponding database through the target connection.

[0014] The beneficial effects of this invention are as follows: by determining whether there are idle connections in the TCP connection pool, data requests are preferentially and reasonably allocated to TCP connections with idle resources for forwarding, thereby realizing the function of supporting multiple data request connections on a single TCP connection, reducing the time consumption caused by the TCP connection creation process, and effectively improving system performance. Attached Figure Description

[0015] Figure 1 This is a flowchart of the steps of a TCP connection multiplexing method disclosed in this invention;

[0016] Figure 2 This is a schematic diagram of the structure of a TCP connection multiplexing terminal disclosed in this invention;

[0017] Label Explanation:

[0018] 201. Memory; 202. Processor. Detailed Implementation

[0019] To explain in detail the technical content, objectives, and effects of the present invention, the following description is provided in conjunction with the embodiments and accompanying drawings.

[0020] Please refer to Figure 1 This invention provides a TCP connection multiplexing method, including:

[0021] Receive data requests from clients;

[0022] Determine if there is an idle connection in the TCP connection pool. If so, use the idle connection as the target connection and forward the data request to the corresponding database through the target connection.

[0023] Otherwise, obtain the session list of the TCP connection pool, select a target connection based on the session list, and forward the data request to the corresponding database through the target connection.

[0024] As can be seen from the above description, the beneficial effects of the present invention are as follows: by determining whether there are idle connections in the TCP connection pool, data requests are preferentially and reasonably allocated to TCP connections with idle resources for forwarding, thereby realizing the function of supporting multiple data request connections on a single TCP connection, thereby reducing the time consumption caused by the TCP connection creation process and effectively improving system performance.

[0025] Furthermore, before receiving the client's data request, the process also includes:

[0026] Initialize a TCP connection pool, which includes multiple TCP connections.

[0027] As described above, by creating an intermediate proxy layer, the TCP connection pool is initialized, thereby forwarding and maintaining data requests between the client and the database, ensuring the stability of the connection between the two, and facilitating simple database queries by the client, effectively improving query efficiency.

[0028] Furthermore, the data request includes multiple sessions;

[0029] The step of forwarding the data request to the corresponding database through the target connection includes:

[0030] Determine whether there is a corresponding session identifier for the multiple sessions. If so, obtain the session identifier, forward the multiple sessions to the corresponding database through the target connection according to the session identifier, and add the session identifier to the session list of the target connection.

[0031] Otherwise, after forwarding the multiple sessions to the corresponding database through the target connection, the session identifier returned by the database is received, and the session identifier is added to the session list of the target connection.

[0032] As described above, when a data request is the first request, the corresponding session in the data request can only obtain a unique session identifier from the database after being forwarded to the corresponding database, thus distinguishing the sessions. When a data request is a subsequent request, and the client needs to operate on the same session, the data request must carry the session identifier previously returned by the database, thereby ensuring that the specified operation can be performed on the same session. This invention obtains each session identifier from the database and allocates sessions reasonably to connections with idle resources based on the session identifier, thereby reducing time consumption.

[0033] Furthermore, when no idle connections exist, obtaining the session list of the TCP connection pool and selecting a target connection based on the session list includes:

[0034] Select the TCP connection with the fewest sessions in the session list as the target connection;

[0035] If the number of sessions for the target connection is one, the data request is directly forwarded to the corresponding database through the target connection;

[0036] If there are multiple sessions for the target connection, the session order in the target session list corresponding to the target connection is obtained, and the data request is forwarded to the corresponding database according to the session order.

[0037] As described above, when there is only one session in the target connection, the current data request can be executed after the current session is completed, ensuring the shortest waiting time and highest efficiency for data requests. However, when there are multiple sessions in the target connection, the waiting time is determined by the order in which each session was added to the target connection's session list, to avoid TCP connection congestion and ensure the shortest possible waiting time for data requests under the current circumstances, thus guaranteeing optimal data request processing efficiency.

[0038] Furthermore, after forwarding the data request to the corresponding database through the target connection, the process further includes:

[0039] Determine whether the database returns an end identifier. If so, obtain the session identifier corresponding to the end identifier, delete the session identifier from the target session list corresponding to the target connection, and end the data request.

[0040] As described above, when a data request is completed, an end identifier is generated synchronously. Each end identifier carries a corresponding session identifier to distinguish the session that needs to be terminated. The session identifier is then removed from the session list to indicate that the session has ended and does not need to be executed. This process updates the TCP connection session list in real time, ensuring the validity of TCP connection resources.

[0041] Please refer to Figure 2 Another embodiment of the present invention provides a TCP connection multiplexing terminal, including a memory, a processor, and a computer program stored in the memory and running on the processor. When the processor executes the computer program, it performs the following steps:

[0042] Receive data requests from clients;

[0043] Determine if there is an idle connection in the TCP connection pool. If so, use the idle connection as the target connection and forward the data request to the corresponding database through the target connection.

[0044] Otherwise, obtain the session list of the TCP connection pool, select a target connection based on the session list, and forward the data request to the corresponding database through the target connection.

[0045] As can be seen from the above description, the beneficial effects of the present invention are as follows: by determining whether there are idle connections in the TCP connection pool, data requests are preferentially and reasonably allocated to TCP connections with idle resources for forwarding, thereby realizing the function of supporting multiple data request connections on a single TCP connection, thereby reducing the time consumption caused by the TCP connection creation process and effectively improving system performance.

[0046] Furthermore, before receiving the client's data request, the process also includes:

[0047] Initialize a TCP connection pool, which includes multiple TCP connections.

[0048] As described above, by creating an intermediate proxy layer, the TCP connection pool is initialized, thereby forwarding and maintaining data requests between the client and the database, ensuring the stability of the connection between the two, and facilitating simple database queries by the client, effectively improving query efficiency.

[0049] Furthermore, the data request includes multiple sessions;

[0050] The step of forwarding the data request to the corresponding database through the target connection includes:

[0051] Determine whether there is a corresponding session identifier for the multiple sessions. If so, obtain the session identifier, forward the multiple sessions to the corresponding database through the target connection according to the session identifier, and add the session identifier to the session list of the target connection.

[0052] Otherwise, after forwarding the multiple sessions to the corresponding database through the target connection, the session identifier returned by the database is received, and the session identifier is added to the session list of the target connection.

[0053] As described above, when a data request is the first request, the corresponding session in the data request can only obtain a unique session identifier from the database after being forwarded to the corresponding database, thus distinguishing the sessions. When a data request is a subsequent request, and the client needs to operate on the same session, the data request must carry the session identifier previously returned by the database, thereby ensuring that the specified operation can be performed on the same session. This invention obtains each session identifier from the database and allocates sessions reasonably to connections with idle resources based on the session identifier, thereby reducing time consumption.

[0054] Furthermore, when no idle connections exist, obtaining the session list of the TCP connection pool and selecting a target connection based on the session list includes:

[0055] Select the TCP connection with the fewest sessions in the session list as the target connection;

[0056] If the number of sessions for the target connection is one, the data request is directly forwarded to the corresponding database through the target connection;

[0057] If there are multiple sessions for the target connection, the session order in the target session list corresponding to the target connection is obtained, and the data request is forwarded to the corresponding database according to the session order.

[0058] As described above, when there is only one session in the target connection, the current data request can be executed after the current session is completed, ensuring the shortest waiting time and highest efficiency for data requests. However, when there are multiple sessions in the target connection, the waiting time is determined by the order in which each session was added to the target connection's session list, to avoid TCP connection congestion and ensure the shortest possible waiting time for data requests under the current circumstances, thus guaranteeing optimal data request processing efficiency.

[0059] Furthermore, after forwarding the data request to the corresponding database through the target connection, the process further includes:

[0060] Determine whether the database returns an end identifier. If so, obtain the session identifier corresponding to the end identifier, delete the session identifier from the target session list corresponding to the target connection, and end the data request.

[0061] As described above, when a data request is completed, an end identifier is generated synchronously. Each end identifier carries a corresponding session identifier to distinguish the session that needs to be terminated. The session identifier is then removed from the session list to indicate that the session has ended and does not need to be executed. This process updates the TCP connection session list in real time, ensuring the validity of TCP connection resources.

[0062] This invention provides a TCP connection multiplexing method and terminal, which can be applied to Internet-related products and database connection projects. It achieves TCP connection multiplexing through a special protocol, reducing the time consumed in the TCP connection creation process and improving system performance. Specific embodiments are described below:

[0063] Please refer to Figure 1 Embodiment 1 of the present invention is as follows:

[0064] A TCP connection multiplexing method includes:

[0065] S101. Initialize the TCP connection pool, which includes multiple TCP connections.

[0066] It should be noted that after the data connection proxy layer is started, multiple TCP connections will be initialized locally as a basic TCP connection pool. The function of this TCP connection pool is similar to that of a regular data connection pool.

[0067] S102, Receive data requests from clients.

[0068] Specifically, the data request includes multiple sessions.

[0069] S104. Determine whether there are any idle connections in the TCP connection pool. If there are idle connections, proceed to step S1042; if there are no idle connections, proceed to step S1044.

[0070] S1042. Use the idle connection as the target connection, and forward the data request to the corresponding database through the target connection.

[0071] It should be noted that the term "idle connection" refers to a TCP connection that is not currently in use and is in a waiting state.

[0072] In some embodiments, if there are multiple idle connections, an idle connection is randomly selected from the idle connections as the target connection.

[0073] S1044. Obtain the session list of the TCP connection pool, select a target connection according to the session list, and forward the data request to the corresponding database through the target connection.

[0074] Forwarding the data request to the corresponding database via the target connection in steps S1042 and S1044 includes:

[0075] S1052. Determine whether there is a corresponding session identifier for the multiple sessions. If there is a corresponding session identifier, proceed to step A1; if there is no corresponding session identifier, proceed to step A2.

[0076] It should be noted that before executing step S1052, the communication protocol of the old database connection needs to be modified: a new `sessionId` field is added to the parameter list of the data request, where the `sessionId` field is the session identifier. When the data request is forwarded for the first time, the session identifier (`sessionId` field) is empty. After the database successfully executes the data request, the database will return the actual session identifier (`sessionId` field). When the client needs to process the same session again subsequently, the data request corresponding to that session must be written with the corresponding session identifier. The data connection proxy layer adds the session identifier before forwarding the data request. For example, the data request includes sessions A and B. If session A is the first request, then the session identifier (`sessionId` field) of session A is empty; while if session B is a subsequent request, then session B carries the corresponding session identifier (`sessionId` field).

[0077] Step A1: Obtain the session identifier, forward multiple sessions to the corresponding databases through the target connection according to the session identifier, and add the session identifier to the session list of the target connection.

[0078] Step A2: After forwarding the multiple sessions to the corresponding database through the target connection, receive the session identifier returned by the database and add the session identifier to the session list of the target connection.

[0079] It should be noted that the session list indicates which sessions are currently executing or waiting on the TCP connection.

[0080] In step S1044, obtaining the session list of the TCP connection pool and selecting a target connection based on the session list includes:

[0081] Step B1: Select the TCP connection with the fewest sessions in the session list as the target connection.

[0082] In some embodiments, the session list is stored in a local cache. When there are no idle connections in the TCP connection pool, the session list corresponding to all TCP connections in the TCP connection pool is retrieved from the local cache, and the TCP connection with the fewest sessions in the session list is selected as the target connection.

[0083] Step B2: If the number of sessions for the target connection is one, then the data request is directly forwarded to the corresponding database through the target connection.

[0084] Step B3: If there are multiple sessions for the target connection, obtain the session order in the target session list corresponding to the target connection, and forward the data request to the corresponding database according to the session order.

[0085] It should be noted that when multiple sessions are executing or waiting in a TCP connection, data transmission or waiting is performed according to the order in which the sessions were added to the target session list. Specifically, data corresponding to a session is transmitted to the database for processing according to the order in which the sessions were added to the session list. If the transmission is successful, the data status corresponding to the session identifier is changed to "Executing in the database." The next session is then acquired for data transmission, until the data status corresponding to all session identifiers in the session list is "Executing in the database." For example, if sessions A, B, and C already exist in the target TCP connection, and the session addition order is ABC, then there is blocking waiting in the TCP connection, meaning that while session A is transmitting, sessions B and C are waiting. When the business logic of session A is already being executed in the database, the TCP connection is currently idle, so it is session B's turn to transmit, and so on.

[0086] After step S104, the method further includes:

[0087] S106. Determine whether the database returns an end identifier. If so, obtain the session identifier corresponding to the end identifier, delete the session identifier from the target session list corresponding to the target connection, and end the data request.

[0088] It should be noted that after the session is completed and the data transmission is finished, the database will return an end identifier. Each end identifier needs to include a sessionId field, which is the session identifier, to identify which session has ended.

[0089] In one optional implementation, the data returned by the database includes the total number of data packets to be returned and a session identifier. If the client receives the corresponding total number of data packets, the session corresponding to that session identifier is terminated.

[0090] It should be noted that when a session is added to the corresponding target connection, the session list in the local cache will be updated synchronously, that is, the corresponding session identifier will be added to the session list; when the session is completed, the session list in the local cache will be updated synchronously, that is, the corresponding session identifier will be deleted from the session list.

[0091] It should be noted that before step S101, a data connection proxy layer is created between the client and the database. The aforementioned TCP connection multiplexing method is implemented through this data connection proxy layer. Specifically, the data proxy layer receives data requests from the client, selects a target connection from the TCP connection pool, and then forwards multiple sessions from the data request to the corresponding databases through the target connection. That is, the client and the data connection proxy layer exchange information in the form of data requests, while the data connection proxy layer and the database exchange information in the form of sessions. Thus, information forwarding and maintenance between the client and the database are achieved through the data connection proxy layer.

[0092] Please refer to Figure 2 Embodiment two of the present invention is as follows:

[0093] A TCP connection multiplexing terminal includes a memory 201, a processor 202, and a computer program stored in the memory 201 and running on the processor 202. When the processor 202 executes the computer program, it implements the various steps in the TCP connection multiplexing method described in Embodiment 1.

[0094] In one alternative implementation, a TCP connection multiplexing terminal is a proxy layer.

[0095] In summary, the TCP connection multiplexing method and terminal provided by this invention modifies the communication protocol of existing database connections by adding session identifiers and recording all sessions corresponding to each TCP connection in a local cache to obtain a session list. Based on this session list, it can be determined whether there are idle connections in the TCP connection pool. If idle connections exist, they are directly selected as the target connection, and data requests are forwarded. If no idle connections exist, the TCP connection with the fewest sessions is selected as the target connection, and data requests are forwarded according to the session order. This prioritizes the reasonable allocation of data requests to TCP connections with idle resources, enabling multiple data request connections to be supported on a single TCP connection, thereby reducing the time consumption caused by the TCP connection creation process and effectively improving system performance.

[0096] The above description is merely an embodiment of the present invention and does not limit the patent scope of the present invention. Any equivalent modifications made based on the content of the present invention specification and drawings, or direct or indirect applications in related technical fields, are similarly included within the patent protection scope of the present invention.

Claims

1. A TCP connection multiplexing method, characterized in that, include: Receive data requests from clients; Determine if there is an idle connection in the TCP connection pool. If so, use the idle connection as the target connection and forward the data request to the corresponding database through the target connection. Otherwise, obtain the session list of the TCP connection pool, select a target connection according to the session list, and forward the data request to the corresponding database through the target connection; When no idle connections exist, obtaining the session list of the TCP connection pool and selecting a target connection based on the session list includes: Select the TCP connection with the fewest sessions in the session list as the target connection; If the number of sessions for the target connection is one, the data request is directly forwarded to the corresponding database through the target connection; If there are multiple sessions for the target connection, the session order in the target session list corresponding to the target connection is obtained, and the data request is forwarded to the corresponding database according to the session order. The data request includes multiple sessions; The step of forwarding the data request to the corresponding database through the target connection includes: Determine whether there is a corresponding session identifier for the multiple sessions. If so, obtain the session identifier, forward the multiple sessions to the corresponding database through the target connection according to the session identifier, and add the session identifier to the session list of the target connection. Otherwise, after forwarding the multiple sessions to the corresponding database through the target connection, the session identifier returned by the database is received, and the session identifier is added to the session list of the target connection; A data connection proxy layer is created between the client and the database. The client and the data connection proxy layer interact with each other in the form of data requests, while the data connection proxy layer and the database interact with each other in the form of sessions. The data connection proxy layer modifies the communication protocol of the old database connection by adding a field identifying the session as a session identifier to the parameter list of the data request. When the data request is forwarded for the first time, the session identifier is empty. The database returns the session identifier after successfully executing the data request. When the same session is processed again subsequently, the corresponding session identifier is written into the data request, and the data connection proxy layer adds the session identifier before forwarding the data request.

2. The TCP connection multiplexing method according to claim 1, characterized in that, Before receiving the data request from the client, the following also includes: Initialize a TCP connection pool, which includes multiple TCP connections.

3. The TCP connection multiplexing method according to claim 1, characterized in that, After forwarding the data request to the corresponding database through the target connection, the process further includes: Determine whether the database returns an end identifier. If so, obtain the session identifier corresponding to the end identifier, delete the session identifier from the target session list corresponding to the target connection, and end the data request.

4. A TCP connection multiplexing terminal, comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it performs the following steps: Receive data requests from clients; Determine if there is an idle connection in the TCP connection pool. If so, use the idle connection as the target connection and forward the data request to the corresponding database through the target connection. Otherwise, obtain the session list of the TCP connection pool, select a target connection according to the session list, and forward the data request to the corresponding database through the target connection; When no idle connections exist, obtaining the session list of the TCP connection pool and selecting a target connection based on the session list includes: Select the TCP connection with the fewest sessions in the session list as the target connection; If the number of sessions for the target connection is one, the data request is directly forwarded to the corresponding database through the target connection; If there are multiple sessions for the target connection, the session order of the target session list corresponding to the target connection is obtained, and the data request is forwarded to the corresponding database according to the session order. The data request includes multiple sessions; The step of forwarding the data request to the corresponding database through the target connection includes: Determine whether there is a corresponding session identifier for the multiple sessions. If so, obtain the session identifier, forward the multiple sessions to the corresponding database through the target connection according to the session identifier, and add the session identifier to the session list of the target connection. Otherwise, after forwarding the multiple sessions to the corresponding database through the target connection, the session identifier returned by the database is received, and the session identifier is added to the session list of the target connection; A data connection proxy layer is created between the client and the database. The client and the data connection proxy layer interact with each other in the form of data requests, while the data connection proxy layer and the database interact with each other in the form of sessions. The data connection proxy layer modifies the communication protocol of the old database connection by adding a field identifying the session as a session identifier to the parameter list of the data request. When the data request is forwarded for the first time, the session identifier is empty. The database returns the session identifier after successfully executing the data request. When the same session is processed again subsequently, the corresponding session identifier is written into the data request, and the data connection proxy layer adds the session identifier before forwarding the data request.

5. A TCP connection multiplexing terminal according to claim 4, characterized in that, Before receiving the data request from the client, the following also includes: Initialize a TCP connection pool, which includes multiple TCP connections.

6. A TCP connection multiplexing terminal according to claim 4, characterized in that, After forwarding the data request to the corresponding database through the target connection, the process further includes: Determine whether the database returns an end identifier. If so, obtain the session identifier corresponding to the end identifier, delete the session identifier from the target session list corresponding to the target connection, and end the data request.

Citation Information

Patent Citations

  • A method for establishing an iSCSI session and an iSCSI initiator

    CN102281263A

  • Method for connection multiplexing in seven-layer load balancing and load balancer

    CN112910793A