WebSocket-based data push method, device, computer equipment, and readable storage medium

By writing from JVM off-heap memory on the server side and using off-heap memory for data push, combined with direct memory access of the DMA controller, the problem of high CPU and memory usage caused by frequent memory copying in traditional technology is solved, and communication is improved Efficiency and system stability.

CN118502968BActive Publication Date: 2024-12-31HANGZHOU PINGPONG INTELLIGENT TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410955178.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-17
Publication Date
2024-12-31
Estimated Expiration
2044-07-17

AI Technical Summary

Technical Problem

In the process of instant communication between servers and multiple clients, frequent copying of write buffers in traditional technology leads to high CPU usage and increased memory space usage, reducing communication efficiency.

Method used

By determining the target off-heap memory from the JVM off-heap memory, writing the data to be sent is used as direct memory, and data transmission is carried out using the DMA controller, which avoids frequent memory copies of the data objects in the heap waiting for push, and directly uses Out-of-heap memory for data push.

Benefits of technology

It reduces the load on the CPU and memory, improves system stability and communication efficiency, and avoids the data processing process of frequent memory copies.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118502968B_ABST
    Figure CN118502968B_ABST
Patent Text Reader

Abstract

The present application relates to a method, device, computer equipment and readable storage medium based on WebSocket data push. The method includes: determining data to be sent and multiple clients receiving the data to be sent; determining the target off-heap memory from the JVM off-heap memory, and writing the data to be sent into the target off-heap memory according to a preset data format; determining the address information of the target off-heap memory, and using the target off-heap memory as direct memory; calling the direct memory through a system function, and providing the address information to a DMA controller, so that the DMA controller reuses the direct memory according to the address information, and transmits the data to be sent in the direct memory to multiple clients based on the WebSocket connection. The use of this method can improve communication efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data communication technology, and in particular to a WebSocket-based data push method, device, computer equipment, and readable storage medium. Background Art

[0002] With the rapid development of mobile Internet, the data scale is growing exponentially, reaching the PB (Petabyte) level or even larger every day. Business systems need an efficient and reliable data processing method. When the server is communicating with multiple clients in real time, pushing data can provide all connected clients with instant data updates and maintain data consistency and synchronization. For example, in market conditions and trading scenarios, data needs to be updated in real time to ensure user experience and application responsiveness, so data needs to be pushed.

[0003] In traditional technology, when a JVM (Java Virtual Machine) server pushes messages to multiple clients, the data content in the heap is copied and written to the direct memory or system kernel space buffer corresponding to the connection. Frequent copying and writing to the buffer or direct memory corresponding to each connection causes higher CPU (Central Processing Unit) usage. At the same time, these buffers occupy more memory space, resulting in reduced communication efficiency. Summary of the invention

[0004] Based on this, it is necessary to provide a WebSocket-based data push method, device, computer equipment, computer-readable storage medium and computer program product that can improve communication efficiency in response to the above technical problems.

[0005] In a first aspect, the present application provides a WebSocket-based data push method, which is applied to a server, including:

[0006] Determine data to be sent and multiple clients that receive the data to be sent;

[0007] Determine a target off-heap memory from the JVM off-heap memory, and write the data to be sent into the target off-heap memory according to a preset data format;

[0008] Determine address information of the target off-heap memory, and use the target off-heap memory as direct memory;

[0009] The direct memory is called by a system function, and the address information is provided to a DMA controller, so that the DMA controller reuses the direct memory according to the address information, and transmits the data to be sent in the direct memory to the multiple clients based on the WebSocket connection.

[0010] In one of the embodiments, determining a target off-heap memory from the JVM off-heap memory, and writing the to-be-sent data into the target off-heap memory according to a preset data format, includes:

[0011] A first off-heap memory is determined from the JVM off-heap memory, and the data to be sent is written into the first off-heap memory according to a preset data format. If all the data to be sent is written, the first off-heap memory is the target off-heap memory.

[0012] In one embodiment, the method further comprises:

[0013] If the data to be sent is not all written, determine a second off-heap memory from the JVM off-heap memory, and write the unwritten data in the data to be sent into the second off-heap memory according to the preset data format;

[0014] If all of the unwritten data has been written into the second off-heap memory, determining the first off-heap memory and the second off-heap memory as target off-heap memories;

[0015] If the unwritten data is not all written into the second off-heap memory, the step of determining the second off-heap memory from the JVM off-heap memory is performed until the data to be sent is all written.

[0016] In one of the embodiments, writing the to-be-sent data into a target off-heap memory according to a preset data format includes:

[0017] Generate a message data object of the data to be sent in the JVM heap memory;

[0018] Convert the message data object into a byte sequence, and compress the byte sequence to obtain byte data;

[0019] The byte data is written into the target off-heap memory according to a preset data format.

[0020] In one embodiment, the client and the server establish a communication connection according to the WebSocket protocol, and the writing of the byte data into the target off-heap memory according to a preset data format includes:

[0021] Based on the WebSocket protocol, the byte data is encapsulated into a binary data frame corresponding to the WebSocket protocol, and the binary data frame is written into a target off-heap memory.

[0022] In one of the embodiments, before determining the data to be sent and a plurality of clients receiving the data to be sent, the method further includes:

[0023] Determining a communication handshake negotiation strategy with the client; the communication handshake negotiation strategy includes a sub-data protocol format data compression algorithm and not using a WebSocket extension that changes data content;

[0024] In response to the WebSocket connection request sent by the client, a communication connection is established with the client according to the communication handshake negotiation strategy.

[0025] In a second aspect, the present application also provides a WebSocket-based data push device, comprising:

[0026] A first determining module, used to determine data to be sent and a plurality of clients that receive the data to be sent;

[0027] The off-heap memory application module is used to determine the target off-heap memory from the JVM off-heap memory;

[0028] A data writing module, used for writing the data to be sent into the target off-heap memory according to a preset data format;

[0029] A second determination module is used to determine the address information of the target off-heap memory and use the target off-heap memory as direct memory;

[0030] The data push module is used to call the direct memory through a system function and provide the address information to the DMA controller, so that the DMA controller reuses the direct memory according to the address information and transmits the data to be sent in the direct memory to the multiple clients based on the WebSocket connection.

[0031] In a third aspect, the present application further provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:

[0032] Determine data to be sent and multiple clients that receive the data to be sent;

[0033] Determine a target off-heap memory from the JVM off-heap memory, and write the data to be sent into the target off-heap memory according to a preset data format;

[0034] Determine address information of the target off-heap memory, and use the target off-heap memory as direct memory;

[0035] The direct memory is called by a system function, and the address information is provided to a DMA controller, so that the DMA controller reuses the direct memory according to the address information, and transmits the data to be sent in the direct memory to the multiple clients based on the WebSocket connection.

[0036] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:

[0037] Determine data to be sent and multiple clients that receive the data to be sent;

[0038] Determine a target off-heap memory from the JVM off-heap memory, and write the data to be sent into the target off-heap memory according to a preset data format;

[0039] Determine address information of the target off-heap memory, and use the target off-heap memory as direct memory;

[0040] The direct memory is called by a system function, and the address information is provided to a DMA controller, so that the DMA controller reuses the direct memory according to the address information, and transmits the data to be sent in the direct memory to the multiple clients based on the WebSocket connection.

[0041] In a fifth aspect, the present application further provides a computer program product, including a computer program, which implements the following steps when executed by a processor:

[0042] Determine data to be sent and multiple clients that receive the data to be sent;

[0043] Determine a target off-heap memory from the JVM off-heap memory, and write the data to be sent into the target off-heap memory according to a preset data format;

[0044] Determine address information of the target off-heap memory, and use the target off-heap memory as direct memory;

[0045] The direct memory is called by a system function, and the address information is provided to a DMA controller, so that the DMA controller reuses the direct memory according to the address information, and transmits the data to be sent in the direct memory to the multiple clients based on the WebSocket connection.

[0046] In the above-mentioned WebSocket-based data push method, device, computer equipment, computer-readable storage medium and computer program product, when the server pushes the data to be sent to multiple clients that receive the data to be sent, the target off-heap memory is determined from the JVM off-heap memory, the data to be sent is written into the target off-heap memory according to a preset data format, and the target off-heap memory is used as direct memory; the direct memory is called by the system function, and the address information of the target off-heap memory is provided to the DMA controller, so that the DMA controller transmits the data to be sent in the direct memory to multiple clients based on the WebSocket connection according to the address information; by writing the data to be sent into the off-heap memory as direct memory, the direct memory is used when pushing data, thereby avoiding the need for the in-heap data object to wait for the data to be pushed to all clients before being recycled by the JVM GC, thereby reducing the CPU usage and memory occupation of frequent memory copies, reducing the load on the CPU and memory, improving the stability of the system, and eliminating the data processing process of frequent memory copies in the process of writing the in-heap data object into the network buffer, thereby improving the communication efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0047] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the drawings required for use in the embodiments of the present application or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.

[0048] Figure 1 This is an application environment diagram based on a WebSocket data push method in an embodiment;

[0049] Figure 2 A schematic diagram of a process of a WebSocket data push method in one embodiment;

[0050] Figure 3 A schematic diagram of a flow chart of a method for handshaking and connecting based on the WebSocket protocol in one embodiment;

[0051] Figure 4 A flowchart of a client and a server establishing a WebSocket persistent connection communication negotiation through an external network in one embodiment;

[0052] Figure 5 A schematic diagram of a flow chart of a method for determining target off-heap memory in one embodiment;

[0053] Figure 6 A schematic diagram of a flow chart of a method for writing data into a target off-heap memory in one embodiment;

[0054] Figure 7 This is a schematic diagram of pushing data to be sent in an embodiment;

[0055] Figure 8 A schematic diagram of a flow chart of a WebSocket data push method in another embodiment;

[0056] Fig. 9 A schematic diagram of a flow chart of a WebSocket data push method in another embodiment;

[0057] Fig.10 It is a structural block diagram of a WebSocket-based data push device in an embodiment;

[0058] Fig.11 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0059] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0060] WebSocket: A communication protocol that establishes a persistent connection between a client and a server. After a protocol handshake negotiation to confirm the protocol version, supported subprotocols, and extensions, bidirectional communication is carried out between the client and the server to transmit data in real time.

[0061] WebSocket frame: It can be the basic unit used to transmit data between the client and the server in the WebSocket protocol. It can be divided into control frames and data frames. The control frame is used to control the connection status and transmission process to ensure the stability and effectiveness of the connection. The data frame can be divided into text frames and binary frames.

[0062] WebSocket extension: A WebSocket extension is a pluggable component that can be proposed by the client during the WebSocket handshake phase and takes effect after confirmation by the server. These extensions can be used to add new protocol features, such as compression and encryption. Direct I / O: A method for data transmission. The word "direct" in its name is used to distinguish cached I / O that uses a kernel space buffer mechanism, bypassing the operating system's file system cache and directly transferring data from the application's memory to the device (or copying data from the device to memory) to improve performance and efficiency.

[0063] DMA: Direct Memory Access is a memory access technology in computer science. It allows certain computer hardware subsystems (computer peripherals) to read and write system memory (Directmemory) independently without the intervention of the central processing unit (CPU).

[0064] JVM GC: The garbage collection (GC) of the Java virtual machine is an automatic memory management mechanism that is used to automatically recycle the memory space occupied by objects that are no longer in use at runtime to avoid memory leaks and improve memory utilization.

[0065] The WebSocket data push method provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown. Among them, the client 102 communicates with the server 104 through the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or it can be placed on the cloud or other network servers. The server 104 determines the data to be sent and multiple clients that receive the data to be sent; determines the target off-heap memory from the JVM off-heap memory, and writes the data to be sent into the target off-heap memory according to the preset data format; determines the address information of the target off-heap memory, and uses the target off-heap memory as direct memory; calls the direct memory through the system function, and provides the address information to the DMA controller, so that the DMA controller reuses the direct memory according to the address information, and transmits the data to be sent in the direct memory to the multiple clients based on the WebSocket connection. Among them, the client 102 can be, but is not limited to, various personal computers, laptops, smart phones, tablets, Internet of Things devices and portable wearable devices, and the Internet of Things devices can be smart speakers, smart TVs, smart air conditioners, smart car devices, projection devices, etc. The server 104 can be an independent physical server, a server cluster or a distributed system composed of multiple physical servers, or a cloud server that provides cloud computing services. The client 102 can establish a TCP connection with the server through a network load balancing domain name. The client 102 and the server 104 can also be based on the WebSocket protocol handshake and connection.

[0066] In an exemplary embodiment, Figure 2 As shown, a WebSocket-based data push method is provided, which is applied to Figure 1 The server in the example is used to illustrate, including the following steps 202 to 208. Among them:

[0067] Step 202: determine data to be sent and multiple clients that receive the data to be sent.

[0068] The form of the message to be sent can be, but is not limited to, text, pictures, audio, and links. Different business scenarios require different data to be sent. For example, in an instant messaging scenario, the data to be sent can be a message to be sent. In a transaction payment business, the message to be sent can be, but is not limited to, market conditions (e.g., stocks, exchange rates), or any of the transaction messages.

[0069] For example, in business scenarios such as high-frequency push of market information / broadcast messages, the server running on the JVM collects data according to the preset data collection frequency, determines the data to be sent from the collected data, and determines the multiple clients that receive the data to be sent. High frequency can be understood as greater than the preset sending frequency. For example, data push once a day can be understood as high frequency, and high frequency can be determined according to actual scenario requirements. For another example, pushing hundreds of exchange rate data to multiple clients in 1 second, such as 10,000 clients, and pushing once in 1 second can be understood as high frequency.

[0070] It should be noted that data push based on WebSocket can be active push or passive push.

[0071] Step 204, determine the target off-heap memory from the JVM off-heap memory, and write the data to be sent into the target off-heap memory according to a preset data format.

[0072] The target off-heap memory may be determined based on the data size of the message to be sent, and the target off-heap memory may be determined from the JVM off-heap memory based on the memory size required by the message to be sent. Alternatively, the memory space may be allocated according to the preset memory size, and if the data to be sent can be completely written into the pre-allocated memory space, the process ends. If the data to be sent cannot be completely written into the pre-allocated memory space, the process ends. The preset data format may be, but is not limited to, a binary data frame, and the preset data format may be a data format negotiated between the server and the client.

[0073] Exemplarily, the target off-heap memory is determined from the JVM off-heap memory according to the memory size required for the message to be sent, and the data to be sent is written into the target off-heap memory in a binary frame data format.

[0074] Step 206: determine the address information of the target off-heap memory, and use the target off-heap memory as direct memory.

[0075] It is understood that direct memory is the memory space allocated outside the Java virtual machine (JVM) heap. Unlike objects in the Java heap, direct memory is not limited by the size of the JVM heap memory and can be managed through native functions provided by the operating system.

[0076] The method for determining the address information of the target off-heap memory and the representation of the address information can be implemented by existing methods, which will not be described in detail here.

[0077] Step 208 , calling the direct memory through the system function, providing the address information to the DMA controller, so that the DMA controller transmits the to-be-sent data in the direct memory to the multiple clients based on the WebSocket connection according to the address information.

[0078] It should be noted that direct memory access is a hardware mechanism that allows bidirectional data transmission between peripherals and system memory without the involvement of the CPU. Using DMA can free the system CPU from the actual I / O data transmission process and improve the system throughput.

[0079] Among them, the system function can be but is not limited to the writev function. The writev function can realize data transmission between multiple buffers of files and processes with only one system call, eliminating the overhead of multiple system calls or copying data, reducing the switching between kernel state and user state, and improving data transmission efficiency. Exemplarily, the direct memory is called through the writev function, and the address information is provided to the DMA controller. When it is pushed to each of the multiple clients, direct I / O (Direct I / O) transmission is performed through shared reuse of the off-heap memory, that is, the DMA controller transmits the data to be sent in the direct memory to the corresponding clients based on the WebSocket connection according to the address information.

[0080] The above-mentioned WebSocket-based data push method, when the server pushes the to-be-sent data to multiple clients that receive the to-be-sent data, determines the target off-heap memory from the JVM off-heap memory, writes the to-be-sent data into the target off-heap memory according to a preset data format, and uses the target off-heap memory as direct memory; calls the direct memory through a system function, provides the address information of the target off-heap memory to a DMA controller, so that the DMA controller transmits the to-be-sent data in the direct memory to multiple clients based on a WebSocket connection according to the address information; writes the to-be-sent data into the off-heap memory as direct memory, and uses the direct memory when pushing data, thereby eliminating the data processing process of frequent memory copying in the process of writing the in-heap data object into the network buffer, reducing the CPU usage and memory occupancy of frequent memory copying, improving communication efficiency, and avoiding the problem that the in-heap data object cannot be recovered by the JVMGC when the writing of the sending buffer of the unfinished data is completed, and the residence time is long, resulting in reduced system stability and heap memory overflow.

[0081] It is understandable that due to the differences in the WebSocket communication negotiation mechanism between each connection and the server, different data formats may occur. In business scenarios where message push is frequent, the server needs to use the negotiated strategy encoding (Encode) for each connection to broadcast messages, and the byte content of TCP transmission may be different. Therefore, the communication protocol needs to be negotiated between the server and the client. Figure 3 As shown, a method for handshaking and connecting based on the WebSocket protocol is provided, including steps 302 to 304, wherein:

[0082] Step 302, determine the communication handshake negotiation strategy with the client; the communication handshake negotiation strategy includes the sub-protocol data format, the data compression algorithm, and not using the WebSocket extension that causes the data content to change.

[0083] Among them, the client and the server agree to use the same subprotocol (WebSocket Subprotocol) as the data format of the original message, such as STOMP (Streaming Text Oriented Messaging Protocol), MQTT (Message Queuing Telemetry Transport) or a custom private protocol. The data compression algorithm agreed upon in the negotiation can be any existing compression algorithm, for example, the compression algorithm can be zlib or the more efficient zstandard, etc., which is determined according to the actual application scenario and is not limited here. The negotiation agreement does not use WebSocket extensions that change the data content, such as the permessage-deflate extension provided by the WebSocket standard. This extension will cause the message data pushed by the server to the client to change, that is, when the server pushes the same data to multiple client connections, the off-heap memory cannot be reused as direct memory (shared buffer). The subprotocol and compression algorithm agreed upon in the communication handshake negotiation strategy are determined by the server program.

[0084] Step 304, responding to the WebSocket connection request sent by the client, and establishing a communication connection with the client according to the communication handshake negotiation strategy.

[0085] Exemplarily, the server uses the agreed communication handshake negotiation strategy to confirm the connection with the client and respond to the WebSocket connection request sent by the client; the communication handshake negotiation strategy includes the sub-protocol data format, compression algorithm, and not using WebSocket extensions that change the data content. The server establishes a WebSocket connection with the client according to the communication handshake negotiation strategy, and closes the connection if an error occurs during the negotiation process. Figure 4As shown, it is a flowchart of the communication negotiation of establishing a WebSocket long connection between the client and the server through the external network in one embodiment. The server running on the JVM opens the IP and port of the WebSocket communication service for the client to establish through the connection. When the client needs to establish a WebSocket connection with the server, it sends an HTTP / 1.1 protocol handshake request and a GET request to the server, which contains some specific header information (Connection: Upgrade / Upgrade: websocket) to indicate that the client wants to upgrade to the WebSocket protocol. The Sec-WebSocket-Extensions information in the request header indicates that the client can support the permessage-deflate extension provided by the WebSocket standard protocol. The server responds to the client's WebSocket connection request and does not respond to the Sec-WebSocket-Extensions header according to the agreed communication handshake negotiation strategy, which means that the two parties do not use this extension in communication. After the communication negotiation is completed, the two parties can communicate bidirectionally by transmitting WebSocket frames. Among them, one of the client and the server can disconnect and terminate the communication session.

[0086] In the above embodiment, a communication connection is established between the client and the server according to the communication handshake negotiation strategy, and the data bytes sent by each connection are exactly the same, so that the server can use memory with exactly the same byte data to push data for each client connection, which can reduce frequent memory copies during the network I / O process, thereby reducing CPU usage and memory space occupancy caused by memory copies.

[0087] In an exemplary embodiment, Figure 5 As shown, a method for determining target off-heap memory is provided, including steps 502 to 508. Among them:

[0088] Step 502, determine the first off-heap memory from the JVM off-heap memory, write the data to be sent into the first off-heap memory according to a preset data format, and if all the data to be sent are written, the first off-heap memory is the target off-heap memory.

[0089] The size of the first off-heap memory may be preset, for example, the size of the first off-heap memory may be 256 bytes. Data writing may be, but is not limited to, data objects in the heap memory are encoded and compressed according to the sub-protocol and written to the off-heap memory. The sub-protocol encoding may be a protocol negotiated between the client and the server.

[0090] Step 504: If all the data to be sent are not written, determine a second off-heap memory from the JVM off-heap memory, and write the unwritten data in the data to be sent into the second off-heap memory according to a preset data format.

[0091] The second off-heap memory may be determined according to the size of the space required for the unwritten data.

[0092] Step 506: If all the unwritten data has been written into the second off-heap memory, the first off-heap memory and the second off-heap memory are determined as target off-heap memories.

[0093] Step 508: If the unwritten data is not all written into the second off-heap memory, the step of determining the second off-heap memory from the JVM off-heap memory is performed until all the data to be sent is written.

[0094] It is understandable that when off-heap memory is allocated, a default size is generally given, such as 256 bytes. If the allocated off-heap memory is not enough to write all the data to be sent, the memory required for the unwritten data is calculated and then a space such as 256 is reallocated. The remaining unwritten bytes are written to the new external memory space, and so on, until all the data to be sent are written. Finally, these spaces are logically sent as a direct memory.

[0095] In the above embodiment, the data processing efficiency can be improved by allocating off-heap memory while writing.

[0096] Optionally, in an exemplary embodiment, a third off-heap memory is determined from the JVM off-heap memory, and the data to be sent is written into the third off-heap memory according to a preset data format. If the data to be sent is not all written, a fourth off-heap memory is determined from the JVM off-heap memory, and the first data to be sent written in the third off-heap memory is copied to the second off-heap memory, and the third off-heap memory is released; the second data to be sent that is not written in the data to be sent is written to the fourth off-heap memory according to the preset data format. If the data to be sent is all written to the fourth off-heap memory, the fourth off-heap memory is the target off-heap memory. Among them, the memory space of the fourth off-heap memory is larger than the memory space of the third off-heap memory. For example, when the off-heap memory is allocated, a size is generally given by default, such as 256 bytes. If the data to be sent is not all written, a larger space such as 512 is reallocated, and the data to be sent of the previously allocated off-heap memory is copied to the new off-heap memory and then the unwritten data is written to the remaining bytes until all data is written to the same off-heap memory.

[0097] In an exemplary embodiment, Figure 6 As shown, a method for writing data into a target off-heap memory is provided, including steps 602 to 606, wherein:

[0098] Step 602: Generate a message data object of the data to be sent in the memory in the JVM heap.

[0099] For example, a thread running on the server side of the JVM creates a DirectByteBuffer object in the heap memory. It is understandable that in this process, a section of memory space will be requested from the off-heap memory as direct memory, and the DirectByteBuffer object saves the address information of the direct memory. Figure 7 As shown, it is a schematic diagram of pushing data to be sent in an embodiment. The JVM thread business generates a message data object in the heap memory and sends it to multiple connections. In this process, a memory space is requested from the off-heap memory as direct memory. The object saves the address information of the direct memory. The mobile device and the client where the browser is located are based on the WebSoket connection and directly access the memory through the network interface controller.

[0100] Step 604: convert the message data object into a byte sequence, and compress the byte sequence to obtain byte data.

[0101] The compression process can be performed by a compression algorithm negotiated between the server and the client. Here, the compression algorithm is not limited. The conversion of the message data object into a byte sequence can be understood as the serialization of the message data object. The compression algorithm negotiated between the server and the client, when receiving data, the client compresses the data in a corresponding decompression method and decompresses it to obtain the original message to be sent.

[0102] Step 606: Write the byte data into the target off-heap memory according to a preset data format.

[0103] The writing into the target off-heap memory may be based on the WebSocket protocol, encapsulating the byte data into a binary data frame corresponding to the WebSocket protocol, and writing the binary data frame into the target off-heap memory.

[0104] Exemplarily, the server serializes and compresses the message data object corresponding to the data to be sent to obtain byte data, encapsulates the byte data into a WebSocket binary frame, and writes the byte data into the off-heap memory associated with DirectByteBuffer. The compression of the data can reduce the network bandwidth in network transmission.

[0105] In the above embodiment, when the server running on the JVM transmits the in-heap message data through the WebSocket connection, the in-heap data object is serialized, compressed and encapsulated into a WebSocket binary frame, and written into the JVM's off-heap memory. The data object can then be recycled by the JVM GC, thus avoiding the JVM in-heap object from resident in the memory. When sending broadcast message data at a high frequency, the GC pressure caused by the object's long-term memory residence can be significantly reduced.

[0106] In an exemplary embodiment, the method of determining multiple clients to which data is to be sent may be determined by a business thread, that is, according to the data to be sent, the business thread traverses all connections with the server and client where the data to be sent is located. If there is a target connection that meets the preset subscription conditions among all the connections, the data to be sent is transmitted to the target connection, and the instant message of the client corresponding to the target connection is displayed. Among them, the preset subscription conditions can be determined according to actual needs and are not limited here. For example, the server pushes a new real-time market quotation data, and the business thread traverses all connections between the server and the client. If the connection meets the conditions for subscribing to the market quotation data, the real-time market quotation data is transmitted to the connection that meets the conditions for subscribing to the market quotation data to support the display of instant messages on the client.

[0107] In an exemplary embodiment, Figure 8 As shown, a WebSocket-based data push method is provided, which is applied to Figure 1 The server in the example is used to illustrate, including the following steps 802 to 812. Among them:

[0108] Step 802, determine the communication handshake negotiation strategy with the client; the communication handshake negotiation strategy includes the protocol format, data compression algorithm and not using the WebSocket standard protocol extension that changes the data content.

[0109] Step 804, responding to the WebSocket connection request sent by the client, and establishing a communication connection with the client according to the communication handshake negotiation strategy.

[0110] Step 806: determine the data to be sent and multiple clients that receive the data to be sent.

[0111] Step 808, determine the target off-heap memory from the JVM off-heap memory, encapsulate the byte data into a binary data frame corresponding to the WebSocket protocol based on the WebSocket protocol, and write the binary data frame into the target off-heap memory.

[0112] Step 810: determine the address information of the target off-heap memory, and use the target off-heap memory as direct memory.

[0113] Step 812, calling the direct memory through the system function, providing the address information to the DMA controller, so that the DMA controller transmits the to-be-sent data in the direct memory to multiple clients based on the WebSocket connection according to the address information.

[0114] It is understandable that the specific implementation of this example can be implemented in the above-defined manner, which will not be described in detail here. Fig. 9As shown, a flow chart of the WebSocket data push method is shown, in which the data to be sent is the message to be sent, the JVM thread business logic generates a message data object in the heap memory to be sent to multiple connections, allocates the first off-heap memory from the off-heap memory using the default settings, compresses the content of the message data to be sent and encapsulates the WebSocket binary frame, and writes it into the first off-heap memory. If the first off-heap memory is large enough to completely write the data, the off-heap memory where the data is completely written is used as direct memory, and all WebSocket connections send using the direct memory (shared buffer), call the direct I / O system kernel function, and the DMA controller transfers the direct memory data to the client device corresponding to the WebSocket connection. The client receives the binary data frame from the WebSocket connection and decompresses the data to obtain the message content.

[0115] If the first off-heap memory is not large enough to write all the data, and aggregate write is used, a larger second off-heap memory is allocated to continue writing data until the message to be sent is completely written. Multiple off-heap memories are used as direct memory together. All WebSocket connections send using this direct memory (shared buffer). The direct I / O system kernel function is called, and the DMA controller transfers the direct memory data to the client device corresponding to the WebSocket connection. The client receives the binary data frame from the WebSocket connection and decompresses the data to obtain the message content.

[0116] If the first off-heap memory is not large enough to fully write data, and aggregate write is not used, the third off-heap memory is allocated, the byte data that fills the first off-heap memory is copied, and the first off-heap memory is released until the message to be sent is completely written. The fully written off-heap memory is used as direct memory, and all WebSocket connections send using this direct memory (shared buffer). The direct I / O system kernel function is called, and the DMA controller transfers the direct memory data to the client device corresponding to the WebSocket connection. The client receives the binary data frame of the WebSocket connection and decompresses the data to obtain the message content.

[0117] In the above embodiment, the server no longer uses the extension (WebSocket Extension) provided in the WebSocket standard protocol that may affect the message body during the WebSocket handshake negotiation process with the client, so that the server can use the memory with exactly the same byte data to push the WebSocket frame of each client connection; the communication mechanism stipulates that the WebSocket binary frame data sent by the server to the client is a compressed format of the message, which can be confirmed during the connection negotiation process to reduce bandwidth usage; and the server compresses the data objects in the heap and writes them to the off-heap memory for use as direct memory, as a buffer for direct I / O sending. The data objects in the JVM heap can be reclaimed by the JVM GC without waiting for the connection write to be completed, reducing the GC pressure caused by the object memory residing for a long time; the write system call of each client connection can share and reuse the memory space instead of memory copying, eliminating the frequent memory copying caused by writing each connection heap memory to the kernel space buffer or off-heap memory (Direct I / O), reducing CPU load and memory usage, ensuring system stability while significantly improving communication efficiency.

[0118] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.

[0119] Based on the same inventive concept, the embodiment of the present application also provides a WebSocket-based data push device for implementing the WebSocket-based data push method involved above. The implementation solution provided by the device to solve the problem is similar to the implementation solution recorded in the above method, so the specific limitations in one or more embodiments of the WebSocket-based data push device provided below can refer to the limitations of the WebSocket-based data push method above, and will not be repeated here.

[0120] In an exemplary embodiment, Fig.10As shown, a WebSocket-based data push device is provided, including: a first determination module 1002, an off-heap memory application module 1004, a data writing module 1006, a second determination module 1008 and a data push module 1010, wherein:

[0121] The first determining module 1002 is used to determine the data to be sent and a plurality of clients that receive the data to be sent.

[0122] The off-heap memory application module 1004 is used to determine the target off-heap memory from the JVM off-heap memory.

[0123] The data writing module 1006 is used to write the data to be sent into the target off-heap memory according to a preset data format.

[0124] The second determination module 1008 is used to determine the address information of the target off-heap memory and use the target off-heap memory as direct memory.

[0125] The data push module 1010 is used to call the direct memory through the system function and provide the address information to the DMA controller, so that the DMA controller reuses the direct memory according to the address information and transmits the data to be sent in the direct memory to multiple clients based on the WebSocket connection.

[0126] The above-mentioned WebSocket-based data pushing device, when the server pushes the data to be sent to multiple clients that receive the data to be sent, determines the target off-heap memory from the JVM off-heap memory, writes the data to be sent into the target off-heap memory according to a preset data format, and uses the target off-heap memory as direct memory; calls the direct memory through a system function, provides the address information of the target off-heap memory to a DMA controller, so that the DMA controller transmits the data to be sent in the direct memory to multiple clients based on a WebSocket connection according to the address information; writes the data to be sent into the off-heap memory as direct memory, and uses the direct memory when pushing data, thereby avoiding the need for the in-heap data object to wait for the data to be pushed to all clients before being reclaimed by the JVM GC, thereby reducing the CPU usage and memory occupation of frequent memory copies, reducing the load on the CPU and memory, improving the stability of the system, and eliminating the data processing process of frequent memory copies in the process of writing the in-heap data object into the network buffer, thereby improving the communication efficiency.

[0127] In an exemplary embodiment, the off-heap memory application module 1004 is used to determine the first off-heap memory from the JVM off-heap memory.

[0128] In an exemplary embodiment, the data writing module 1006 is further used to write the data to be sent into the first off-heap memory according to a preset data format. If all the data to be sent are written, the first off-heap memory is the target off-heap memory.

[0129] In an exemplary embodiment, the off-heap memory application module 1004 is used to determine a second off-heap memory from the JVM off-heap memory if the data to be sent has not been fully written.

[0130] In an exemplary embodiment, the data writing module 1006 is further configured to write the unwritten data in the data to be sent into the second off-heap memory according to a preset data format;

[0131] If all the unwritten data has been written into the second off-heap memory, the first off-heap memory and the second off-heap memory are determined as the target off-heap memory.

[0132] In an exemplary embodiment, the WebSocket-based data push device further includes a data compression module, which is used to generate a message data object of the data to be sent in the JVM heap memory;

[0133] Convert the message data object into a byte sequence and compress the byte sequence to obtain byte data.

[0134] In an exemplary embodiment, the data writing module 1006 is further configured to write the byte data into the target off-heap memory according to a preset data format.

[0135] In an exemplary embodiment, the data writing module 1006 is further used to encapsulate the byte data into a binary data frame corresponding to the WebSocket protocol based on the WebSocket protocol, and write the binary data frame into the target off-heap memory.

[0136] In an exemplary embodiment, the WebSocket-based data push device further includes a communication module, which is used to determine a communication handshake negotiation strategy with a client; a communication handshake negotiation strategy sub-protocol data format, a compression algorithm, and a WebSocket standard protocol extension that does not change the data content;

[0137] Respond to the WebSocket connection request sent by the client and establish a communication connection with the client according to the communication handshake negotiation strategy.

[0138] Each module in the above-mentioned WebSocket-based data push device can be implemented in whole or in part by software, hardware, or a combination thereof. Each of the above-mentioned modules can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each of the above modules.

[0139] In an exemplary embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as shown in FIG. Fig.11 As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. Among them, the processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store data to be pushed. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a method for pushing data based on WebSocket is implemented.

[0140] Those skilled in the art will understand that Fig.11 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0141] In an exemplary embodiment, a computer device is further provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above-mentioned method embodiments when executing the computer program.

[0142] In an exemplary embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments are implemented.

[0143] In an exemplary embodiment, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps in the above method embodiments are implemented.

[0144] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant regulations.

[0145] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic unit, a data processing logic unit based on quantum computing, an artificial intelligence (AI) processor, etc., but are not limited to this.

[0146] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.

[0147] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. A method for pushing data based on WebSocket, characterized in that: Applied to the server, the method includes: In the case where the data push frequency is greater than the preset sending frequency, determine the communication handshake negotiation strategy with the client; the communication handshake negotiation strategy includes the sub-protocol data format, the data compression algorithm, and not using the WebSocket extension that causes the data content to change; the sub-protocol data format and the data compression algorithm are determined by the server program; the sub-protocol data format is the data format of the original message agreed by the client and the server to use the same sub-protocol; the same sub-protocol includes a custom private protocol; Responding to the WebSocket connection request sent by the client, and establishing a communication connection with the client according to the communication handshake negotiation strategy; Determine data to be sent and multiple clients that receive the data to be sent; Determine a target off-heap memory from the JVM off-heap memory, and write the data to be sent into the target off-heap memory according to a preset data format; Determine the address information of the target off-heap memory, and use the target off-heap memory as direct memory; the direct memory is the memory space allocated outside the Java virtual machine heap, the direct memory is not limited by the JVM heap memory size, and is managed by native functions provided by the operating system; Calling the direct memory through a system function, providing the address information to a DMA controller, so that the DMA controller reuses the direct memory according to the address information, and transmits the data to be sent in the direct memory to the multiple clients based on the WebSocket connection; The step of determining a target off-heap memory from the JVM off-heap memory and writing the data to be sent into the target off-heap memory according to a preset data format includes: Determine a first off-heap memory from the JVM off-heap memory, write the data to be sent into the first off-heap memory according to a preset data format, and if all the data to be sent are written, the first off-heap memory is the target off-heap memory; If the data to be sent is not all written, determine a second off-heap memory from the JVM off-heap memory, and write the unwritten data in the data to be sent into the second off-heap memory according to the preset data format; If all of the unwritten data has been written into the second off-heap memory, determining the first off-heap memory and the second off-heap memory as target off-heap memories; If the unwritten data is not all written into the second off-heap memory, executing the step of determining the second off-heap memory from the JVM off-heap memory until all the data to be sent are written; Writing the data to be sent into the target off-heap memory according to a preset data format includes: Generate a message data object of the data to be sent in the JVM heap memory; Convert the message data object into a byte sequence, and compress the byte sequence to obtain byte data; Based on the WebSocket protocol, the byte data is encapsulated into a binary data frame corresponding to the WebSocket protocol, and the binary data frame is written into a target off-heap memory.

2. A data push device based on WebSocket, characterized in that: The device comprises: A communication module is used to determine a communication handshake negotiation strategy with a client when the data push frequency is greater than a preset sending frequency; the communication handshake negotiation strategy includes a sub-protocol data format, a data compression algorithm, and not using a WebSocket extension that causes a change in data content; the sub-protocol data format and the data compression algorithm are determined by a server program; the sub-protocol data format is a data format in which the client and the server agree to use the same sub-protocol as the original message; the same sub-protocol includes a custom private protocol; Responding to the WebSocket connection request sent by the client, and establishing a communication connection with the client according to the communication handshake negotiation strategy; A first determining module, used to determine data to be sent and a plurality of clients that receive the data to be sent; The off-heap memory application module is used to determine the target off-heap memory from the JVM off-heap memory; A data writing module, used for writing the data to be sent into the target off-heap memory according to a preset data format; A second determination module is used to determine the address information of the target off-heap memory and use the target off-heap memory as direct memory; the direct memory is the memory space allocated outside the Java virtual machine heap, the direct memory is not limited by the JVM heap memory size, and is managed by native functions provided by the operating system; A data push module, configured to call the direct memory through a system function and provide the address information to a DMA controller, so that the DMA controller reuses the direct memory according to the address information and transmits the data to be sent in the direct memory to the multiple clients based on a WebSocket connection; Wherein, the data compression module is used to generate a message data object of the data to be sent in the JVM heap memory; Convert the message data object into a byte sequence, and compress the byte sequence to obtain byte data; The data writing module is further used to encapsulate the byte data into a binary data frame corresponding to the WebSocket protocol based on the WebSocket protocol, and write the binary data frame into the target off-heap memory; Wherein, the off-heap memory application module is further used to determine the first off-heap memory from the JVM off-heap memory; The data writing module is further used to write the data to be sent into the first off-heap memory according to a preset data format. If all the data to be sent are written, the first off-heap memory is the target off-heap memory. The off-heap memory application module is further used to determine a second off-heap memory from the JVM off-heap memory if all the data to be sent are not written. The data writing module is further used to write the unwritten data in the data to be sent into the second off-heap memory according to the preset data format; If all of the unwritten data has been written into the second off-heap memory, determining the first off-heap memory and the second off-heap memory as target off-heap memories; If the unwritten data is not all written into the second off-heap memory, the data to be sent is not written into the second off-heap memory until all the data to be sent is written into the second off-heap memory.

3. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that: When the processor executes the computer program, the steps of the method according to claim 1 are implemented.

4. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to claim 1 are implemented.

5. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to claim 1 are implemented.