Use Swoole2’s \ Swoole \ Coroutine \ MySQL to create connections and maintain a pool of connections through static classes and static member properties that can be shared by different coroutines.

Queuing mechanism (FIFO) is implemented using special features of coroutines:

  • \Swoole\Coroutine:: Resume (name) : Resume a suspended Coroutine execution from the name domain;

  • \Swoole\Coroutine::suspend(name) :suspend(name) :suspend the current Coroutine axial name) : Suspend (name) :suspend the current Coroutine axial name itself.

Limiting conditions

  • Each worker has its own MySQL connection pool, and different workers cannot share each other’s MySQL connection pool.

  • There may be different connection pool maximization between worker processes (depending on business implementation).

advantages

  • Compared with the independent connection pool (the connection pool implementation that can be shared between worker processes), there is no inter-progress communication overhead;

  • Separate connection pools increase o&M costs.

Method of use

/ * * * @ * * initialized connection pool param array $connsConfig array configuration * * 'connName1' = > [* 'serverInfo' = > [' host '= >' 127.0.0.1 ', 'user' = > 'test', 'password'=>'pass', 'database'=>'tt', 'charset'=>'utf8'], // \ Swoole \ Coroutine \ MySQL connection parameter *'maxSpareConns'=> 5, / / maximum number of links to * 'maxConns' = > 10, / / maximum number of connections *], *' connName2 '= > [*' serverInfo '= > [' host' = > '127.0.0.2', 'user' = > 'test', 'password'=>'pass', 'database'=>'tt', 'charset'=>'utf8'], // \ Swoole \ Coroutine \ MySQL connection parameter *'maxSpareConns'=> 5, // maxConns => 10, // maxConns => 10, *] * / Swoole \ Coroutine\ Pool \ MySQLPool :: Init (array $connsConfig) The connection must be obtained from the connection Pool ** @param \ Swoole \ Coroutine \ MySQL $conn The connection obtained from the connection Pool * / Swoole \ Coroutine \ Pool \ MySQLPool :: Recycle (\ Swoole \ Coroutine \ MySQL $conn) / ** * Retrieve a connection from the connection pool ** @param string $connName init * @return \ Swoole \ Coroutine \ MySQL return a connection instance * / Swoole \ Coroutine \ pool \ MySQLPool :: extract ($connName)Copy the code

Use the sample

<? PHP requires' mysqlpool.php '; Use Swoole \ Coroutine \ Pool \ MySQLPool; $server = new Swoole \ Http \ server (" 127.0.0.1 ", 9502, SWOOLE_BASE); $server - > group (['worker_num' => 1,]); $server - > on (' request ', function ($request, $response) {MySQLPool :: INIT ([' test '= > [' serverInfo' = > [' host '= >' 192.168.244.128 ', 'user' = > 'mha_manager' and 'password' = > 'mhapass', 'database' =>' tt', 'charset' =>' UTf8 '], 'maxSpareConns' => 5, 'maxConns' => 10],]); $swoole_mysql = MySQLPool :: fetch ('test'); $ret = $swoole_mysql- > query ('select sleep (1) '); MySQLPool :: retrieve ($swoole_mysql); $response- > end ('Test end '); }); $server- > start ();Copy the code

Pressure test command: ab -c 20 -n 100 -s 100 http://127.0.0.1:9502/, 20 concurrent requests, a total of 100 requests.

Pressure test results:

Connection condition during pressure test, 20 concurrent, the maximum number of connections is 10, so only 10 connections will be established with the database at most:

After the pressure test, the maximum number of connections is set to 5, so now when there is no client request, most connections remain at 5:

Here, there is only 1 worker process working on the server, completing 100 requests, and the SQL query of each request is sleep 1 second, which takes about 11 seconds. If the mode is phP-fpm + mysqli, it takes 100 seconds for 1 worker process.

Therefore, this is exactly the advantage of coroutine. By using non-dual IO + coroutine switch, one worker process can process multiple client requests at the same time, greatly improving throughput.

Pay attention and don’t get lost

All right, everybody, that’s all for this article. All the people here are talented. As I said before, there are many technical points in PHP, because there are too many, it is really difficult to write, you will not read too much after writing, so I have compiled it into PDF and document, if necessary

Click on the code: PHP+ “platform”

As long as you can guarantee your salary to rise a step (constantly updated)

I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc