This article gives you the content is about the swoole of the structure of the analysis, there is a certain reference value, the need for friends can refer to, I hope you have some help.
The structure diagram is as follows:
Swoole is primarily used by the master process (master process) and the manager process to complete its functionality.
Master Process
is a multi-threaded program. There is a very important set of threads, called reactor threads. It is the thread that really handles the TCP connection, sending and receiving data.
Manager process
Manage the worker/task process. The Worker/task process is both forked and managed by the manager process.
Reactor threads
The main thread (master process) will assign this connection to a fixed reactor thread after the new connection is held, and this thread will be responsible for listening to the socket. Reads the data when the socket is readable and parses the protocol to post the request to the worker process.
Responsible for maintaining client TCP
connections, processing networks IO
, processing protocols, and sending and receiving data
Completely asynchronous, non-blocking mode
All C
code, except Start
the/ Shudown
event callback, does not execute any PHP code
TCP
buffer, splice, and split the data sent by the client into a complete request packet
Reactor
Run in a multi-threaded manner
Work process
Similar to the PHP-FPM process.
Accepts Reactor
a request packet posted by a thread and executes a PHP
callback function to process the data
Generate the response data and send Reactor
it to the thread, sent to the client by the Reactor
line Cheng TCP
Can be in asynchronous mode or synchronous mode
Worker
Run in a multi-process manner
Taskworker process
Processes that handle other tasks asynchronously, in a way similar to Gearman.
Accept Worker
swoole_server->task/taskwait
tasks that are posted by the process through methods
Processes the task and returns the resulting data ( swoole_server->finish
) to the Worker
process
TaskWorker
Run in a multi-process manner
Relationship
It can be understood to be Reactor
nginx
, that Worker
is php-fpm
. The Reactor
thread asynchronously processes the network request in parallel and then forwards it Worker
to the process (processing in the callback function). Reactor
Worker
communication between and through UnixSocket
.
Event Processing Flow
Learn about the Swoole event processing process and learn about the two types of network event processing patterns first.
Reactor mode
It requires that the main thread (I/O processing Unit) is only responsible for listening for events occurring on the file descriptor, and that the event is immediately notified to the worker/process (logical unit). In addition, the main thread does not do any other work. Read and write data, accept new connections, and process client requests are done in the worker thread.
Proactor mode
Two kinds of implementations
Implement Proactor mode using the I/O asynchronous model. Principle: All I/O operations are given to the main thread, the main thread mate and the kernel to handle, and the business logic operation is given to the logical unit. For example, use Aio_read to implement.
Work Flow:
The main thread calls the Aio_read function to register the read completion event on the socket to the kernel.
The main thread continues to handle other I/O events.
When the data on the socket is read into the user buffer, the kernel sends a signal to the application (logical unit) informing the application that the data is available.
The application reads the data (client's request) and, after processing, calls the Aio_write function to register the write event on the socket to the kernel.
The main thread continues to process other logic.
When data in the user's buffer is written to the socket, the kernel sends a signal to the application informing the application that the data has been sent.
The application pre-defined signal processing functions to deal with aftercare, such as closing the socket.
Implement Proactor mode using the I/O synchronization model. Principle: The main thread performs the read and write operation of the I/O event data, and the business logic operation is given to the logical unit. For example, use Epoll to implement.
Work Flow:
The main thread registers the read-ready event on the socket to the Epoll kernel event table.
The main thread calls epoll_wait to wait for data to be read on the socket.
After the epoll_wait has returned, the main thread reads the data from the socket and then encapsulates the read data into a Request object (the client's request) and inserts the request queue.
The queue's consumer thread processes the request object, and then registers a write-ready event on the socket in the Epoll kernel event table.
The main thread calls Epoll_wait to wait for the socket to be writable.
When the socket is writable, the epoll_wait notifies the main thread. The main thread writes the request result toward the socket.
Swoole Event Architecture Diagram
As can be seen from the graph, if we combine the reactor thread and the work process as a worker thread, Swoole uses the reactor event processing mode.
A request goes through the following steps:
1. The server main thread waits for the client to connect.
2. The reactor thread handles the socket, reads the request data (Receive) on the socket, encapsulates the request, and then delivers it to the work process.
3. The work process is a logical unit that processes business data.
4. The work process results are returned to the reactor thread.
5. The reactor thread writes the result back to the socket (Send).
For the work of each module, please review the structure described above.