Architectural analysis of the Swoole

Source: Internet
Author: User
Tags epoll
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

    • TCPbuffer, splice, and split the data sent by the client into a complete request packet

    • ReactorRun 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

    • WorkerRun 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

    • TaskWorkerRun 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:

    1. The main thread calls the Aio_read function to register the read completion event on the socket to the kernel.

    2. The main thread continues to handle other I/O events.

    3. 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.

    4. 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.

    5. The main thread continues to process other logic.

    6. 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.

    7. 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:

    1. The main thread registers the read-ready event on the socket to the Epoll kernel event table.

    2. The main thread calls epoll_wait to wait for data to be read on the socket.

    3. 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.

    4. 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.

    5. The main thread calls Epoll_wait to wait for the socket to be writable.

    6. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.