workerman how to write mysql connection pool

First of all, we must understand why the connection pool is used and what problems the connection pool can solve for you.

The main role of the connection pool
1. Reduce the overhead of establishing three TCP handshake with the data server and closing the connection and waving four times, thereby reducing the load on the client and mysql server, shortening the response time of the request
2. Reducing the number of concurrent connections to the database Too many connections of the database caused by too many application servers

If it is to solve the problem 1
, database connection pooling is not the most efficient method in workerman. Because PHP is single-process and single-threaded, using PHP to implement a database connection pool must definitely use a separate process to do, then it will involve inter-process communication, so that the process of communicating directly with mysql becomes the connection pool. The communication of mysql increases the load on the application side.

The most efficient way to solve Problem 1 is to establish a single database instance for each business process (such as the DB class provided by workerman) to implement a long database connection, so that all requests for each process use their own long database connection. The life cycle of the process is only the overhead of a TCP handshake and disconnection wave, and the application communicates directly with mysql. There is no intermediate pool of IPC communication between processes in the middle of the connection pool. The performance is the highest, and there is no one.

If it is for question 2,
first look at how many application servers there are, and each server has multiple concurrent connections to mysql. If you only have 10 application servers, 50 processes per server, and 1 database connection per process, then there are only 10 * 50 = 500 concurrent connections (not active connections) to the mysql server, and 500 concurrent connections to mysql It ’s a piece of cake. In order to solve the problem 2, there is no need to use the connection pool.

If you have 1000 application servers, a connection pool is necessary, but this connection pool cannot be a connection pool running on the local application server, because 1000 application servers have 1000 connection pools, even if each connection pool only Open 10 connections, then the number of database connections will be easily filled. So don't expect to open the connection pool implemented by several task processes on the current server to solve this problem.

In a cluster of 1000 application servers, it is also unreliable to engage several processes on each server to implement connection pooling. The method that can really solve Problem 2 is to establish an independent database connection pool server or cluster to manage all database links globally.

In summary,
if the mysql connection pool for PHP is implemented solely for question 1, then the database singleton is a simpler and more efficient approach than the so-called connection pool.
If it is to achieve problem 2, then the business must also have a certain scale. If you really want to use workerman to make a separate connection pool cluster, the following is a simple way to establish some task processes, each process creates a database connection, The task process sends the sql request to the mysql server. After the mysql server returns, the task process sends the result to the sql initiator.

The connection pool code is similar to the following. If it is a connection pool cluster composed of multiple servers, it is best to add an lvs in front of workerman
// task worker,使用Text协议
$task_worker = new Worker('Text://0.0.0.0:1234');
$task_worker->count = 64;
$task_worker->name = 'MysqlTask';
$task_worker->onMessage = function($connection, $sql)
{
     // 执行sql.... 得到结果,这里省略....
     $sql_result = your_mysql_query($sql);
     // 发送结果
     $connection->send(json_encode($sql_result));
};


use \Workerman\Connection\AsyncTcpConnection;

// 与远程连接池服务建立异步链接,ip为远程连接池服务的ip,如果是集群就是lvs的ip
$sql_connection = new AsyncTcpConnection('Text://ip:1234');
// 发送sql
$sql_connection->send("SELECT ... FROM .....");
// 异步获得sql结果
$sql_connection->onMessage = function($sql_connection, $sql_result)
{
     // 这里只是打印结果
     var_dump(json_decode($task_result));
};
// 执行异步链接
$sql_connection->connect();

Published 23 original articles · won praise 2 · Views 5260

Guess you like

Origin blog.csdn.net/bianlitongcn/article/details/79025537