One of the most important features of a non-blocking server model is that it returns immediately after the call is read or written to the interface without going into a blocking state. Before exploring the single-threaded non-blocking IO model, it is important to understand the detection mechanism of the socket event in non-blocking situations, because the most significant thing for nonblocking mode is to detect which connections are interested in events, and generally there are three kinds of detection methods.
Application Traversal Socket Detection
, when multiple clients request to the server, the server side saves a list of socket connections, and the application layer thread polls the socket list for attempts to read or write. For a read operation, the read data is processed if it is successfully read to some data, and the next loop continues with the read failure, and for the write operation, the first attempt is made to write the data to a specified socket, and the next loop continues to attempt the write failure.
In this way, no matter how many socket connections can be managed by a thread, a thread is responsible for traversing these socket lists, constantly trying to read or write data, good use of the blocking time, processing power is improved. But this model needs to traverse all the socket lists in the application, and it needs to handle the stitching of the data, which may also consume more CPU resources when idle, and is not suitable for practical use. This improvement is the use of event-driven nonblocking methods.
Event detection for kernel traversal sockets
In this way, the traversal of the socket is given to the operating system kernel, which organizes the results of the socket traversal into a series of event lists and returns to the application layer processing. For the application layer, the objects they need to deal with are those events, which is an implementation of an event-driven nonblocking approach.
, there are multiple client connections on the server side, and the application layer requests a read-write event list to the kernel. The kernel traverses all sockets and generates the corresponding readable list readlist and writable list writelist,readlist indicates whether each socket is readable, for example Socket1 has a value of 1, is readable, and the Socket2 value is 0, which means unreadable. The writelist indicates whether each socket is writable. The application layer traverses the read-write event List readlist and writelist, doing the corresponding read and write operations.
The way the kernel traverses the socket does not have to traverse all sockets at the application layer, moving the traversal down to the kernel layer, which helps improve detection efficiency. However, it needs to pass the list of readable events and writable events of all connections to the application layer, and if the number of sockets is large, it is not a small overhead to copy the list from the kernel to the application layer. Also, when there are fewer active connections, there are many invalid copies of data between the kernel and the application layer, because it copies the active and inactive connection states to the application layer.
Kernel callback-based event detection
It is a less efficient way to detect whether a socket is readable and writable by traversing, whether it is traversing the application layer or the kernel. So another mechanism is needed to optimize the way of traversal, which is the callback function. The socket in the kernel corresponds to a callback function, and when the client sends data to the socket, the kernel receives the data from the network card and then calls the callback function to add the socket as a readable event to the list of events. The application layer gets this list of events to get all the events of interest.
, the server side has more than one client socket connection, first the application layer tells the kernel each socket is interested in the event, and then when the client sends the data, the corresponding will have a callback function, When the kernel replicates the data from the network card, the callback function adds Socket1 as a readable event event1 to the list of events, and similarly, the kernel discovers that the NIC is writable and adds Socket2 as a writable event Event2 to the list of events, and finally, the application layer requests the read-write event list to the kernel. The kernel will return the event list containing the event1 and event2 back to the application layer, and the application layer, through the event list, learns that Socket1 has data to read, reads it, and Socket2 can write data.
In this way, all connections to the client are maintained by the operating system kernel and the list of readable writable events is continuously updated through the callback function, and the application layer thread simply iterates through the list of events to know which connections can be read or writable, and then read and write the connections. Greatly improve the detection efficiency, natural processing capacity is also stronger.
The above three non-blocking event detection mechanisms, for Java, the implementation of non-blocking IO is entirely based on the operating system core non-blocking IO, it masks the difference between the non-blocking IO of the operating system and provides a unified API, so we do not have to care about the operating system. JDK will help us choose the implementation of non-blocking IO, for example, for Linux systems, the JDK will prefer to implement Java non-blocking IO with epoll in the case of epoll support. This non-blocking approach to event detection is the most efficient "kernel callback-based event detection".
Java non-blocking IO event detection from the operating system kernel