TCP message forwarding method and device based on thread pool
By employing hash algorithms and virtual mapping node technology in the thread pool, the problem of out-of-order TCP packets in audio and video streaming forwarding is solved, achieving efficient and transparent data transmission and improving the forwarding performance of the streaming media server.
Patent Information
- Application Number
- CN202411155752.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-22
- Publication Date
- 2024-11-12
AI Technical Summary
Existing thread pool solutions suffer from TCP packet out-of-order issues in audio and video streaming forwarding, leading to data transmission opacity and reduced efficiency.
By employing hash algorithms and virtual mapping node technology, and creating task hash rings and partition segment mappings, the load balancing and data ordering of thread pool forwarding tasks are ensured, thereby achieving efficient audio and video streaming media forwarding.
It achieves transparency and efficient data transmission for audio and video streaming media servers, solves the problem of out-of-order TCP packets in thread pool forwarding, and improves the stability and efficiency of data transmission.
Smart Images

Figure CN118945243A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of audio and video streaming media application, and in particular to a TCP message forwarding method and device based on a thread pool. Background Art
[0002] With the popularization of 5G technology and the optimization of the network environment, the application areas of audio and video streaming media are becoming increasingly broad. From live streaming, video on demand, video conferencing to security monitoring, audio and video have become an important way of modern information dissemination.
[0003] Streaming media forwarding plays a vital role in audio and video network applications. It can distribute audio and video content from the source server to multiple target devices or users, helping to reduce the load on the source server, thereby allowing wider access. It can perform intelligent routing based on network conditions and select the best path to transmit content, thereby reducing latency, jitter, and packet loss. Streaming media forwarding plays an important role in providing efficient, secure, and reliable audio and video content transmission, and is an indispensable part of modern audio and video network applications.
[0004] Modern network communication data transmission generally uses two major transmission control protocols, UDP (User Datagram Protocol) or TCP (Transmission Control Protocol), to provide end-to-end byte stream transmission in the network. UDP is a connectionless protocol that does not guarantee reliable delivery, but has fast data transmission, which is suitable for real-time applications such as video and audio. When using UDP, the application must deal with problems such as data loss, duplication, and disordered order. TCP is a connection-oriented protocol that provides reliable delivery and slow data transmission, but provides flow control and packet loss retransmission mechanisms, which is suitable for non-real-time applications such as file transfer. However, with the increase in network bandwidth and the characteristics of multi-cross-network transmission of audio and video streaming media, more audio and video application data transmission now also uses TCP.
[0005] The current streaming media server forwarding data adopts single-thread, multi-thread and thread pool. The single-thread solution has low forwarding efficiency and fewer audio and video channels. It is generally only used to forward data in a local area network with fewer streaming media channels. The multi-thread solution has high forwarding efficiency and more audio and video channels. However, as the number of forwarding channels increases, the number of forwarding threads will also increase. The increase in the number of threads will increase the CPU scheduling time, resulting in a decrease in the efficiency of thread forwarding streaming data. Therefore, the multi-thread solution is not suitable for high-channel audio and video streaming media forwarding scenarios. The working principle of the thread pool solution is based on pooling technology, which reuses resources or tasks to reduce the overhead of repeated creation and destruction. The thread pool creates and maintains multiple threads in advance to reduce the overhead of frequent thread creation and destruction, thereby improving the performance of the program. Therefore, the thread pool solution is the most suitable solution for streaming media servers to forward data.
[0006] The streaming media forwarding server has multiple input and multiple output streams, and the output streams may be more than the input streams, such as when multiple clients request the same audio and video stream at the same time. A thread in the thread pool is responsible for processing the reception and forwarding of TCP data packets. Due to the randomness of thread scheduling, TCP packets on the same link will be scheduled and forwarded by different threads, which will cause the distribution of data packets to be out of order. Although the domain fields can be defined according to the order of the message content, it will increase the extra work of the next forwarding node or client application end, and does not have transparent consistency in data transmission. Summary of the invention
[0007] In view of the problems existing in the prior art, the present invention provides a TCP message forwarding method and device based on a thread pool, which adopts a hash algorithm and a virtual mapping node concept to ensure the sequential forwarding of input audio and video streams under the premise of high-efficiency forwarding of the thread pool solution, thereby ensuring the transparency and high efficiency of data forwarding by the forwarding node of the streaming media server.
[0008] The technical solution adopted by the present invention specifically includes:
[0009] The first aspect discloses a TCP message forwarding method based on a thread pool, comprising:
[0010] Build a high-concurrency TCP service to receive multiple audio and video source data, and assign a source data channel ID to each audio and video source data;
[0011] Create a thread pool for forwarding source data tasks and set the core parameters of the thread pool, including the maximum number of threads in the thread pool, the current processing thread ID of the thread pool, and the maximum number of channels for forwarding audio video sources;
[0012] Create a task hash ring, where the task hash ring is a ring composed of several points, each point corresponds to a source data forwarding task;
[0013] Create a task virtual thread according to the maximum number of transcoded video source channels;
[0014] The task virtual threads are segmented according to the maximum number of threads in the thread pool;
[0015] Map the actual thread to the task virtual thread segment according to the thread pool's current processing thread ID;
[0016] The source data forwarding task is mapped to the hash ring according to the source data channel ID and assigned to the corresponding task virtual thread node. Then the segment where the task virtual thread node is located is mapped to the actual thread forwarding node for source data forwarding.
[0017] In one implementation, the thread pool core parameters also include the number of task virtual threads, and creating task virtual threads according to the maximum number of transcribed audio video source channels includes:
[0018] An equal number of task virtual threads are created according to the maximum number of transcoded video source channels, where the task virtual threads are mapped to the task hash ring according to (2^32 / NUMvt)*i, i=0~NUMvt-1, VT(i) represents the task virtual thread with index i, and NUMvt represents the number of task virtual threads.
[0019] In one implementation, segmenting the task virtual threads according to the maximum number of threads in the thread pool includes:
[0020] Divide the task virtual threads into segments equal to the maximum number of threads in the thread pool.
[0021] In one embodiment, the thread pool core parameters also include the current number of threads in the thread pool and the number of core threads, and the actual thread is mapped to the segment of the task virtual thread according to the thread pool current processing thread ID, including:
[0022] The thread pool currently processes thread IDs in ascending order, and maps actual threads to each virtual thread segment one by one in a clockwise direction. The mapping rules are as follows:
[0023]
[0024]
[0025] Where CT(j) is the current processing thread with index j, starting from 0 and up to NUMct-1; ZONE(j) represents the virtual thread segment of the task with index j, NUMct represents the current number of threads in the thread pool, NUMcorepool represents the number of core threads, and the mapping rule indicates that if the current number of threads is less than or equal to the number of core threads, the actual threads are mapped incrementally in a 1:2 virtual thread segment; if the current number of threads is greater than the number of core threads, the actual threads are mapped incrementally in a 1:1 virtual thread segment.
[0026] In one implementation, mapping the segment where the task virtual thread node is located to the actual thread forwarding node to forward source data includes:
[0027] Calculate the segment index of the task virtual thread where the source data forwarding task is located;
[0028] The actual thread is obtained according to the segment index of the task virtual thread, and the actual thread forwarding node forwards the source data.
[0029] In one implementation, when the number of forwarding task threads in the forwarding source data task thread pool decreases, the virtual thread segment element mapped by the actual thread is cleared, and the forwarding task mapped to the virtual thread segment is forwarded by the previous forwarding task thread.
[0030] In one implementation, when the forwarding task thread in the forwarding source data task thread pool increases, the ID of the newly added thread is mapped to a vacant virtual thread section element.
[0031] Based on the same inventive concept, the second aspect of the present invention discloses a TCP message forwarding device based on a thread pool, comprising:
[0032] TCP service building module, used to build high-concurrency TCP service, used to receive multiple audio and video source data, and assign source data channel ID to each audio and video source data;
[0033] The forwarding source data task thread pool creation module is used to create a forwarding source data task thread pool and set the thread pool core parameters, which include the maximum number of threads in the thread pool, the thread pool current processing thread ID and the maximum number of transcribed audio video source channels;
[0034] A task hash ring creation module is used to create a task hash ring, where the task hash ring is a circular ring composed of several points, each point corresponding to a source data forwarding task;
[0035] A task virtual thread creation module, used to create a task virtual thread according to the maximum number of transcoded video source channels;
[0036] A task virtual thread segmentation module is used to segment the task virtual threads according to the maximum number of threads in the thread pool;
[0037] The actual thread mapping module is used to map the actual thread to the task virtual thread segment according to the thread ID currently processed by the thread pool;
[0038] The source data forwarding module is used to map the source data forwarding task to the hash ring according to the source data channel ID and assign it to the corresponding task virtual thread node. Then the segment where the task virtual thread node is located is mapped to the actual thread forwarding node for source data forwarding.
[0039] Based on the same inventive concept, the third aspect of the present invention provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the thread pool-based TCP message forwarding method described in the first aspect is implemented.
[0040] Based on the same inventive concept, the fourth aspect of the present invention provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the thread pool-based TCP message forwarding method described in the first aspect is implemented.
[0041] Compared with the prior art, the advantages and beneficial technical effects of the present invention are as follows:
[0042] The present invention proposes a TCP message forwarding method based on thread pool, creates a forwarding source data task thread pool, creates a task hash ring, and a hash ring processing mode for forwarding source data channels of audio and video streaming media. The virtual thread node technology is adopted, and partition segment mapping is used to achieve relatively balanced load of thread pool forwarding task threads, thereby solving the "tilt" problem of uneven busy and idle time of forwarding data threads. Hash ring mapping is used to ensure that thread pool forwarding threads will not compete for forwarding the same source data channel, thereby ensuring the sequence of data messages in TCP transmission mode and achieving transparency of forwarding nodes of streaming media servers. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the embodiments of the present invention 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 some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0044] Figure 1 Schematic diagram of a forwarding task hash ring TasksLoop in an embodiment of the present invention;
[0045] Figure 2 Schematic diagram of mapping source data forwarding tasks to TasksLoop in an embodiment of the present invention;
[0046] Figure 3 A schematic diagram of virtual forwarding thread segmentation in an embodiment of the present invention;
[0047] Figure 4 It is a schematic diagram of mapping the actual forwarding thread to the virtual forwarding thread section in an embodiment of the present invention;
[0048] Figure 5 This is a flow chart of the sequential forwarding of TCP messages by a streaming media server in an embodiment of the present invention. DETAILED DESCRIPTION
[0049] The current single-threaded method of streaming media data forwarding has low forwarding efficiency and limited forwarding paths; the multi-threaded forwarding solution has reduced performance when forwarding more audio and video data; the conventional thread pool forwarding solution is mostly used for UDP data forwarding, and TCP message forwarding will appear "out of order".
[0050] In view of the above situation, the present invention proposes a dynamically adjustable scheme of partition thread pool for streaming media data forwarding, which improves the forwarding efficiency and ensures the sequential "entry and exit" of TCP messages, realizes the transparency of streaming media forwarding server nodes, and is more suitable for the construction of network streaming media application architecture. Specifically, the present invention adopts thread pool, hash algorithm, and virtual node technology, and ensures the sequential forwarding of input audio and video stream data under the premise of high-efficiency forwarding, thereby realizing the transparency of data forwarding by streaming media server forwarding nodes.
[0051] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0052] Embodiment 1
[0053] The present invention discloses a TCP message forwarding method based on a thread pool, the method comprising:
[0054] S1: Build a high-concurrency TCP service to receive multiple audio and video source data, and assign a source data channel ID to each audio and video source data;
[0055] S2: Create a thread pool for forwarding source data tasks and set the core parameters of the thread pool, which include the maximum number of threads in the thread pool, the current processing thread ID of the thread pool, and the maximum number of channels for forwarding video sources;
[0056] S3: Create a task hash ring, where the task hash ring is a ring composed of several points, and each point corresponds to a source data forwarding task;
[0057] S4: Create a task virtual thread according to the maximum number of transcoded video source channels;
[0058] S5: segment the task virtual threads according to the maximum number of threads in the thread pool;
[0059] S6: Mapping the actual thread to the segment of the task virtual thread according to the current processing thread ID of the thread pool;
[0060] S7: Map the source data forwarding task to the hash ring according to the source data channel ID and assign it to the corresponding task virtual thread node. Then, the segment where the task virtual thread node is located is mapped to the actual thread forwarding node for source data forwarding.
[0061] For the specific implementation process of the TCP message forwarding method based on thread pool, please refer to Figure 5 .
[0062] Specifically, the source data channel ID uniquely identifies the data stream. The ID is a 4-byte unsigned integer, represented by IDsourcedata, or IDsd for short. The method for generating IDsd is as follows:
[0063] IDsd=RSHash("IP:Port") or IDsd=BKDRHash("IP:Port")
[0064] Where IP is the IP address of the audio and video source data, and Port is the data link port for uploading the audio and video source data, for example: IDsd = RSHash ("112.23.33.238:35021"). RSHash and BKDRHash are two commonly used string hash functions.
[0065] During the specific implementation process, the core parameters of the thread pool are set in S2 as follows:
[0066] The maximum number of threads in the thread pool is represented by NUMmaxpoolsize, or NUMmaxpool for short.
[0067] The number of core threads in the thread pool is represented by NUMcorepoolsize, or NUMcorepool for short.
[0068] The current number of threads in the thread pool is represented by NUMcurthread, or NUMct for short.
[0069] The ID of the thread pool's current processing thread (actual thread) is represented by IDcurthread, or IDct for short. Thread IDs are all represented in integer format and are slightly different on different operating systems.
[0070] The number of task virtual threads is represented by NUMvirtualthread, or NUMvt for short.
[0071] The ID values of all threads in the thread pool are stored in an array according to the rules. The index of the current thread is represented by IDXcurthread, abbreviated as IDXct.
[0072] The number of server CPU cores is represented by NUMcpucore, or NUMcpu for short.
[0073] The maximum number of video source channels that can be forwarded by the media forwarding server is denoted by NUMmaxsourcedata, or NUMmaxsd for short. For example, it is 200 channels. This parameter is mainly set based on the server network bandwidth and server hardware performance.
[0074] The maximum thread pool size, core thread pool size, idle thread survival time, etc. These parameters can be set through the thread pool construction method. Depending on the application scenario, they need to be adjusted according to the actual situation.
[0075] The general best practice recommendation is to set the core thread pool to the number of CPU cores and the maximum thread pool size to 2*the number of CPU cores, that is, NUMcorepool=NUMcpu, NUMmaxpool=2*NUMcpu.
[0076] Regarding the construction of the task hash ring in S3, in the specific implementation process, 2^32 values are abstracted into a ring. The point directly above the ring represents 0, and the points are arranged clockwise, and so on: 1, 2, 3... until 2^32-1. This ring composed of 2^32 points is called the task hash ring TasksLoop. See the attached Figure 1 .
[0077] When mapping the audio and video source data forwarding task to the task hash ring, you can use the IDsd% 2^32 method. The result must be an integer between 0 and 2^32-1. Map this value to the TasksLoop ring. See the attached Figure 2 ,.
[0078] In one implementation, the thread pool core parameters also include the number of task virtual threads, and creating task virtual threads according to the maximum number of transcribed audio video source channels includes:
[0079] An equal number of task virtual threads are created according to the maximum number of transcoded video source channels, where the task virtual threads are mapped to the task hash ring according to (2^32 / NUMvt)*i, i=0~NUMvt-1, VT(i) represents the task virtual thread with index i, and NUMvt represents the number of task virtual threads.
[0080] Specifically, each task virtual thread maps a segment source data forwarding task, the number of the first segment source data forwarding tasks is 2^32 / NUMv, and the number of the last segment source data forwarding tasks is 2^32-(2^32 / NUMvt)*(NUMvt-1).
[0081] The expression is as follows:
[0082]
[0083] Where i=0 to NUMvt-1, VT(i) represents the virtual thread with index i. Where "]" means less than or equal to (including the element), and ")" means less than but not including the element.
[0084] In one implementation, segmenting the task virtual threads according to the maximum number of threads in the thread pool includes:
[0085] Divide the task virtual threads into segments equal to the maximum number of threads in the thread pool.
[0086] In the specific implementation process, the maximum thread pool size partitioning scheme is adopted, that is, the task virtual thread is equally divided into NUMmaxpool segments. The task virtual thread segment is represented by ZONE[IDXzonevt], and the index IDXzonevt ranges from 0 to NUMmaxpool-1. See the attached Figure 3 The number of task virtual threads that should be mapped to each segment is calculated based on NUMvt / NUMmaxpool. Due to whether it is divisible or not, the number of task virtual threads in the last segment may be different from the number of task virtual threads mapped in the previous segment. For example, there are 200 task virtual threads and the maximum number of threads is 6, 200 / 6=33, 33 task virtual threads / segment in the first segment and 35 task virtual threads in the last segment.
[0087] In one embodiment, the thread pool core parameters also include the current number of threads in the thread pool and the number of core threads, and the actual thread is mapped to the segment of the task virtual thread according to the thread pool current processing thread ID, including:
[0088] The thread pool currently processes thread IDs in ascending order, and maps actual threads to each virtual thread segment one by one in a clockwise direction. The mapping rules are as follows:
[0089]
[0090] Where CT(j) is the current processing thread with index j, starting from 0 and up to NUMct-1; ZONE(j) represents the virtual thread segment of the task with index j, NUMct represents the current number of threads in the thread pool, NUMcorepool represents the number of core threads, and the mapping rule indicates that if the current number of threads is less than or equal to the number of core threads, the actual threads are mapped incrementally in a 1:2 virtual thread segment; if the current number of threads is greater than the number of core threads, the actual threads are mapped incrementally in a 1:1 virtual thread segment.
[0091] Specifically, when the forwarding service is started and the source data task forwarding starts for the first time, if the current number of threads is less than or equal to the number of core threads, the actual thread is incrementally mapped as 1 to 2 virtual thread segments (i.e., one actual thread is mapped to two virtual thread segments); if the current number of threads is greater than the number of core threads, the actual thread is incrementally mapped as 1 virtual thread segment (i.e., one actual thread is mapped to one virtual thread segment). The actual thread ID is stored in the corresponding index element of the virtual thread segment.
[0092] The specific number of segments mapped by the actual thread is the segment index mapped by the current thread to the segment index mapped by the next actual thread. For example, in the case of NUMct <= NUMcorepool, the actual thread 0 is mapped to the virtual thread segment 0 and segment 1. Figure 4 .
[0093] All virtual threads in the virtual thread section are mapped to the current actual thread, and the final data forwarding is processed by the actual thread. This ensures that the forwarding tasks mapped to each section are processed by thread pool threads, solves the "skew" phenomenon of uneven thread processing forwarding tasks, and achieves thread load balancing.
[0094] In one implementation, mapping the segment where the task virtual thread node is located to the actual thread forwarding node to forward source data includes:
[0095] Calculate the segment index of the task virtual thread where the source data forwarding task is located;
[0096] The actual thread is obtained according to the segment index of the task virtual thread, and the actual thread forwarding node forwards the source data.
[0097] Specifically, the forwarding data task IDsd is mapped to the corresponding task virtual thread node, and then the segment where the task virtual thread node is located is mapped to the actual thread forwarding node for data forwarding. Because each thread is fixed to process the data forwarding of a certain audio and video source data channel, there will be no "competition" phenomenon in which multiple threads forward the same source data channel, and there will be no problem of "disorder" of TCP messages at the receiving end due to network asynchronous IO.
[0098] In the specific implementation process, the calculation of the actual task thread of the current source data forwarding task mapping can be achieved through the following steps:
[0099] Step 1: Calculate the virtual thread segment index where the source data forwarding task is located
[0100] Calculate IDXzonevt:
[0101] IDXzonevt=(IDsd / (2^32 / NUMvt)) / (NUMvt / NUMmaxpool);
[0102] Determine the IDXzonevt value based on the relationship between IDXzonevt and NUMmaxpool:
[0103] IDXzonevt=(IDXzonevt>=NUMmaxpool)? (NUMmaxpool-1):IDXzonevt;
[0104] Among them, IDXzonevt represents the task virtual thread segment index, IDsd represents the source data channel ID, NUMvt represents the number of task virtual threads, and NUMmaxpool represents the maximum number of threads in the thread pool.
[0105] Step 2: Get the actual thread according to the virtual thread segment index
[0106] After obtaining IDXzonevt, the value of ZONE[IDXzonevt] is taken to obtain the actual thread ID, thereby obtaining the scheduling thread (actual thread) for data forwarding of the source data.
[0107] In one embodiment, the method further includes, when the forwarding task threads in the forwarding source data task thread pool decrease, clearing the virtual thread segment element mapped by the actual thread, and forwarding tasks mapped to the virtual thread segment are forwarded by the previous forwarding task thread.
[0108] Specifically, when the number of forwarding threads in the thread pool decreases according to the actual forwarding scenario, the virtual thread segment element mapped to the actual thread is emptied, and the forwarding task mapped to the virtual thread segment is forwarded by the forwarding task thread in front of it. In this way, only the forwarding tasks in the emptied segment due to thread reduction will be switched, and the forwarding tasks in other segments will remain unchanged. This treatment will not cause multi-thread competition for forwarding tasks in the emptied area to cause "disorder" of messages, and will also reduce thread switching of existing source data forwarding tasks, which can improve the execution performance of the forwarding service.
[0109] In one embodiment, the method further includes, when the forwarding task thread in the forwarding source data task thread pool increases, mapping the ID of the newly added thread to a vacant virtual thread segment element.
[0110] Specifically, if the number of threads in the thread pool increases, the virtual thread segment will be searched clockwise from the beginning for an empty segment for mapping, that is, the newly added thread ID will be mapped to an empty virtual thread segment element. In this way, only the forwarding task of the virtual segment mapped by the thread increase will be subject to thread scheduling switching, and the forwarding task scheduling of other segments will remain unchanged. This processing will not cause multi-thread competition for the forwarding task of the newly mapped segment, resulting in "disordered" messages.
[0111] In general, the main key points and innovative points of the present invention include the following:
[0112] 1. Hash ring processing mode of audio and video streaming media forwarding source data channel;
[0113] 2. Use virtual thread node technology and partition segment mapping to achieve relatively balanced load on thread pool forwarding task threads, solving the problem of uneven busyness and idleness of forwarding data threads;
[0114] 3. Hash ring mapping is used to ensure that the thread pool forwarding threads will not compete for the same source data channel, thereby ensuring the sequence of data messages in TCP transmission mode and achieving transparency of the streaming media server forwarding node.
[0115] Embodiment 2
[0116] Based on the same inventive concept, this embodiment discloses a TCP message forwarding device based on a thread pool, comprising:
[0117] TCP service building module, used to build high-concurrency TCP service, used to receive multiple audio and video source data, and assign source data channel ID to each audio and video source data;
[0118] The forwarding source data task thread pool creation module is used to create a forwarding source data task thread pool and set the thread pool core parameters, which include the maximum number of threads in the thread pool, the thread pool current processing thread ID and the maximum number of transcribed audio video source channels;
[0119] A task hash ring creation module is used to create a task hash ring, where the task hash ring is a circular ring composed of several points, each point corresponding to a source data forwarding task;
[0120] A task virtual thread creation module, used to create a task virtual thread according to the maximum number of transcoded video source channels;
[0121] A task virtual thread segmentation module is used to segment the task virtual threads according to the maximum number of threads in the thread pool;
[0122] The actual thread mapping module is used to map the actual thread to the task virtual thread segment according to the thread ID currently processed by the thread pool;
[0123] The source data forwarding module is used to map the source data forwarding task to the hash ring according to the source data channel ID and assign it to the corresponding task virtual thread node. Then the segment where the task virtual thread node is located is mapped to the actual thread forwarding node for source data forwarding.
[0124] Since the device introduced in the second embodiment of the present invention is a device used to implement the thread pool-based TCP message forwarding method in the first embodiment of the present invention, based on the method introduced in the first embodiment of the present invention, those skilled in the art can understand the specific structure and deformation of the device, so it is not repeated here. All devices used in the method in the first embodiment of the present invention belong to the scope of protection of the present invention.
[0125] Embodiment 3
[0126] Based on the same inventive concept, the present invention further provides a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the method described in the first embodiment is implemented.
[0127] Since the computer-readable storage medium introduced in the third embodiment of the present invention is the computer-readable storage medium used to implement the thread pool-based TCP message forwarding method in the first embodiment of the present invention, based on the method introduced in the first embodiment of the present invention, those skilled in the art can understand the specific structure and deformation of the computer-readable storage medium, so it is not repeated here. All computer-readable storage media used in the method of the first embodiment of the present invention belong to the scope of protection of the present invention.
[0128] Embodiment 4
[0129] The present invention also provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method described in Embodiment 1 when executing the program.
[0130] Since the computer device introduced in the fourth embodiment of the present invention is a computer device used to implement the thread pool-based TCP message forwarding method in the first embodiment of the present invention, based on the method introduced in the first embodiment of the present invention, the person skilled in the art can understand the specific structure and deformation of the computer device, so it is not repeated here. All computer devices used in the method of the first embodiment of the present invention belong to the scope of protection of the present invention.
[0131] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0132] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0133] Although preferred embodiments of the present invention have been described, additional changes and modifications may be made to these embodiments by those skilled in the art once the basic creative concepts are known. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention. Obviously, those skilled in the art may make various changes and modifications to the embodiments of the present invention without departing from the spirit and scope of the embodiments of the present invention. Thus, if these modifications and variations of the embodiments of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A TCP message forwarding method based on thread pool, characterized in that: include: Build a high-concurrency TCP service to receive multiple audio and video source data, and assign a source data channel ID to each audio and video source data; Create a forwarding source data task thread pool and set the thread pool core parameters, including the maximum number of threads in the thread pool, the thread pool current processing thread ID, and the maximum number of transcribed audio video source channels; Create a task hash ring, where the task hash ring is a ring composed of several points, each point corresponds to a source data forwarding task; Create a task virtual thread according to the maximum number of transcoded video source channels; The task virtual threads are segmented according to the maximum number of threads in the thread pool; Map the actual thread to the task virtual thread segment according to the thread pool's current processing thread ID; The source data forwarding task is mapped to the hash ring according to the source data channel ID and assigned to the corresponding task virtual thread node. Then the segment where the task virtual thread node is located is mapped to the actual thread forwarding node for source data forwarding.
2. The thread pool-based TCP message forwarding method according to claim 1, characterized in that: The core parameters of the thread pool also include the number of task virtual threads. Task virtual threads are created according to the maximum number of transcoded video source channels, including: An equal number of task virtual threads are created according to the maximum number of transcoded video source channels, where the task virtual threads are mapped to the task hash ring according to (2^32 / NUMvt)*i, i=0~NUMvt-1, VT(i) represents the task virtual thread with index i, and NUMvt represents the number of task virtual threads.
3. The thread pool-based TCP message forwarding method according to claim 1, characterized in that: The task virtual threads are segmented according to the maximum number of threads in the thread pool, including: Divide the task virtual threads into segments equal to the maximum number of threads in the thread pool.
4. The thread pool-based TCP message forwarding method according to claim 1, characterized in that: The core parameters of the thread pool also include the current number of threads in the thread pool and the number of core threads. The actual thread is mapped to the segment of the task virtual thread according to the thread pool's current processing thread ID, including: The thread pool currently processes thread IDs in ascending order, and maps actual threads to each virtual thread segment one by one in a clockwise direction. The mapping rules are as follows: Where CT(j) is the current processing thread with index j, starting from 0 and up to NUMct-1; ZONE(j) represents the virtual thread segment of the task with index j, NUMct represents the current number of threads in the thread pool, NUMcorepool represents the number of core threads, and the mapping rule indicates that if the current number of threads is less than or equal to the number of core threads, the actual threads are mapped incrementally in a 1:2 virtual thread segment; if the current number of threads is greater than the number of core threads, the actual threads are mapped incrementally in a 1:1 virtual thread segment.
5. The thread pool-based TCP message forwarding method according to claim 1, characterized in that: The segment where the task virtual thread node is located is mapped to the actual thread forwarding node for source data forwarding, including: Calculate the segment index of the task virtual thread where the source data forwarding task is located; The actual thread is obtained according to the segment index of the task virtual thread, and the actual thread forwarding node forwards the source data.
6. The thread pool-based TCP message forwarding method according to claim 1, characterized in that: The method further includes, when the forwarding task threads in the forwarding source data task thread pool are reduced, emptying the virtual thread segment element mapped by the actual thread, and forwarding tasks mapped to the virtual thread segment are forwarded by the previous forwarding task thread.
7. The thread pool-based TCP message forwarding method according to claim 6, characterized in that: The method further includes, when the forwarding task thread in the forwarding source data task thread pool increases, mapping the ID of the newly added thread to the vacant virtual thread section element.
8. A TCP message forwarding device based on thread pool, characterized in that: include: TCP service building module, used to build high-concurrency TCP service, used to receive multiple audio and video source data, and assign source data channel ID to each audio and video source data; The forwarding source data task thread pool creation module is used to create a forwarding source data task thread pool and set the thread pool core parameters, which include the maximum number of threads in the thread pool, the thread pool current processing thread ID and the maximum number of transcribed audio video source channels; A task hash ring creation module is used to create a task hash ring, where the task hash ring is a circular ring composed of several points, each point corresponding to a source data forwarding task; A task virtual thread creation module, used to create a task virtual thread according to the maximum number of transcoded video source channels; A task virtual thread segmentation module is used to segment the task virtual threads according to the maximum number of threads in the thread pool; The actual thread mapping module is used to map the actual thread to the task virtual thread segment according to the thread ID currently processed by the thread pool; The source data forwarding module is used to map the source data forwarding task to the hash ring according to the source data channel ID and assign it to the corresponding task virtual thread node. Then the segment where the task virtual thread node is located is mapped to the actual thread forwarding node for source data forwarding.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the thread pool-based TCP message forwarding method as described in any one of claims 1 to 7 is implemented.
10. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the thread pool-based TCP message forwarding method as described in any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Data processing method, data processing device, storage medium and electronic device
CN110841282A
Multi-layer consistent hash ring and application thereof in creating distributed database
CN111159193A
Load balancing method based on clusters and multiple processes
CN111858033A
Service-oriented vehicle AUTOSAR communication system
CN116032933A
Message pushing method and device, computer equipment and storage medium
CN117176796A
