Quick Start Dry Swoole engine principle

More welcome attention to micro-channel public number: All food engineer Xiaohui ~

Over the past year using PHP and Java technology stack finished a game server project. Since the item has a high-frequency network requests, the attempt to use the technology stack Swoole PHP engine (High Performance Parallel asynchronous event communication network engine) part of the game to complete the service.

Swoole installation

Swoole installation is very simple, because it is people do the project, a lot can issue official website to find the answer document. Installation in two ways:

  • Compile and install. Directly to github or gitee to download the official release, the compiler installation, writes php.ini file so expansion.
  • Container installation. swoole engine widely used, so there are a lot of available container hub, select the required pull click.

To do what you can Baidu, a lot of online content.

Swoole engine advantage

  1. Permanent memory. Traditional PHP framework or a single file before processing each request, have to do it again load the framework document, the operational configuration, and releases all memory resources after the request is complete, do not worry about memory leaks. However, if the requested increase in the number of concurrent high time, to quickly create a resource, and immediately release will lead to a sharp decline in the efficiency of PHP programs. The use Swoole do not have this problem: PHP code is loaded into memory, has a longer life cycle, thus established database connection and other large objects is not released. Processing each request requires only very little code, which code only in the first run, is compiled parser PHP, memory resident. After all loaded directly OPCODE, let Zend engine run directly. In addition, PHP can not be achieved before, such as database connection pooling, cache connection pooling can be achieved in Swoole engine. Efficiency of the system will be greatly enhanced.
  2. Rapid development. Swoole engine provides asynchronous PHP language, multi-threaded server, asynchronous TCP / UDP network client, asynchronous MySQL, asynchronous Redis, database connection pool, AsyncTask, message queues, millisecond timer, asynchronous file read and write, asynchronous DNS queries. Swoole built Http / WebSocket server / client, Http2.0 server.
  3. Coroutine programming mode. Swoole4 can use the code fully synchronous asynchronous program. PHP code without any additional keyword, automatically scheduling the underlying coroutine, asynchronous IO.

Process analytical engine Swoole

Swoole flowchart run as follows:

Swoole the diagram

Swoole in the thread or process

Structure is as follows:

Swoole thread chart

Swoole engine is divided into two modes: single-threaded mode and process mode. This article discusses the process only mode. Specific difference between the two official documents in a note.

Master process

For processing swoole core events, such as pipe connections, local communications from the client. master process has multiple threads, each running instance of a epol function. (After due process was not out by the Master Worker process fork, it may appear forced to kill Master process, Worker process still exists)

Reactor thread

Swoole main process is a multi-threaded program. Which has a very important set of threads, called the Reactor thread. It is really a TCP connection processing threads, send and receive data.
The main thread Swoole after a new connection Accept, this connection will be allocated to a fixed Reactor thread by thread is responsible for monitoring this socket. When read data is read socket, and protocol analysis, process the request delivered to the Worker. When the socket can write data to the TCP client

Process Manager

swoole in worker / task process is the process by the Manager Fork and management.
When the child process finishes running, manager of this sub-process is responsible for the recovery process to avoid becoming a zombie process. And create a new child process
when the server is shut down, manager process sends a signal to all child processes, inform the child process close the service
when the server reload, manager processes one by one off / restart a child process

Worker process

Swoole provides a complete process management mechanism, after the Worker process quit unexpectedly, such as PHP fatal error occurred, manslaughter another program, or reach max_request times normal exit. The main process will re-pull a new Worker process. Worker within the process can write code like ordinary apache + php or php-fpm in. Do not need to write code that asynchronous callbacks like Node.js.

The processes callback function

In the Master callback function:

  • onStart
  • onShutdown

The callback function in the Worker process

  • onWorkerStart
  • onWorkerStop
  • onConnect
  • onClose
  • onReceive
  • onFinish

The callback function in the process TaskWorker

  • onTask
  • onWorkerStart

Manager callback function in the process

  • onManagerStart
  • onManagerStop

Relations Reactor, Worker, TaskWorker of

Reactor is to be understood as nginx, Worker is php-fpm. Reactor-threaded asynchronous parallel processing network requests, and then forwarded to Worker process to deal with. Reactor between Worker and communicate via UnixSocket.
In the application php-fpm, it is often a task will be delivered to the Redis and other asynchronous queue and start some php process asynchronously handle these tasks in the background. TaskWorker is a more complete set of solutions Swoole provided the delivery task queue, php-tasking process management into one. API provided by the underlying can handle asynchronous tasks very simple to implement. Further TaskWorker can also perform the task, and then returns a result back to the Worker.
Swoole of Reactor, can close union between Worker, TaskWorker, provide more advanced use. A more popular analogy: Suppose Swoole application server is a factory that Reactor is the sales and accept customer orders. The Worker is a worker, when the sales order is received, Worker to work to produce what the customer wants. The TaskWorker can be understood as administrative staff can help Worker do some chores, let Worker concentrate on work.
Worker will provide the underlying process, TaskWorker process is assigned a unique ID. SendMessage can communicate through an interface between different Worker and TaskWorker process.

The actual project process threads of each division:

  • Process Manager: Manages worker processes, create or recycling
  • Worker process: the game logic processing
  • taskWorker process: network packet sent to the client, long-term close inactive tcp connection

Swoole Version Compatibility

swoole version of the engine used in the project development phase 1.9.6, and later as a test environment installed as version 4.3.2, so try to make adjustments business code. However, downward compatible swoole very worthy of admiration that this process has only found a code that is not compatible with the problem: This is a configuration parameter related swoole_server in the original version uses the magic number is configured, but the new version, this figure is not defined macros, and later found a group of macros by looking at swoole source code, and then modify the configuration at this. (However, the version upgrade is also based on the successful swoole business code is relatively small, so for reference purposes only)

Heck, if my card lost.  Micro-letter search for "whole food engineer Xiaohui," I can still find

Guess you like

Origin www.cnblogs.com/mseddl/p/11410291.html