Home> PHP Framework> Swoole> body text

Detailed explanation of mysql connection pool implementation based on swoole

coldplay.xixi
Release: 2020-12-25 17:18:16
forward
6976 people have browsed it

swoole frameworkThe column introduces the method of swoole to implement mysql connection pool

Detailed explanation of mysql connection pool implementation based on swoole

Recommendation (free):swoole framework

Preface

For the traditional nginx FPM mode PHP program, every time the worker requests FPM, it will connect to mysql once, and then the request will end. Disconnect. For applications with small concurrency, this will not be a problem, but for applications with high concurrency, the database will become a bottleneck if the connection is frequently established and the connection is destroyed. I believe many people have also encountered to many connections. Mysql reports an error.

Advantages of connection pool

The connection pool adopts the long connection mode, which will always maintain the connection with MySQL, and will be put back into the connection pool after use, thus It saves the consumption of establishing and disconnecting connections, greatly reduces the consumption of system IO, and improves the concurrency performance of the program to a certain extent. If the connection pool is free, a connection is allocated from the connection pool, otherwise, the request will be added to the waiting queue.

Implementation

We use swoole to implement mysql connection pool

Connection pool class
pool)) { $this->config = $config; $this->pool = new \Swoole\Coroutine\Channel($config['pool_size']); for ($i = 0; $i < $config['pool_size']; $i++) { \go(function() use ($config) { $mysql = new MysqlDB(); $res = $mysql->connect($config['mysql']); if ($res === false) { throw new RuntimeException("Failed to connect mysql server"); } else { $this->pool->push($mysql); } }); } } } public function get() { if ($this->pool->length() > 0) { $mysql = $this->pool->pop($this->config['pool_get_timeout']); if (false === $mysql) { throw new RuntimeException("Pop mysql timeout"); } return $mysql; } else { throw new RuntimeException("Pool length <= 0"); } } public function recycle(MysqlDB $mysql){ $this->pool->push($mysql); } /** * 获取连接池长度 * @return mixed */ public function getPoolSize(){ return $this->pool->length(); }}
Copy after login
Database DB class
connect($config); if ($res === false) { throw new RuntimeException($connection->connect_error, $connection->errno); } else { $this->connection = $connection; } return $res; } public function query($sql){ $result = $this->connection->query($sql); return $result; }}
Copy after login
Create a connection pool in the HTTP coroutine server
5, 'pool_get_timeout'=>1, 'timeout'=>1, 'charset'=>'utf8', 'strict_type'=>false, 'fetch_mode'=>true, 'mysql'=>[ 'host'=>'127.0.0.1', 'port'=>'3306', 'user'=>'homestead', 'password'=>'secret', 'database'=>'blog', ] ]); $server->handle('/', function ($request, $response) use ($pool){ $mysql = $pool->get(); $res = $mysql->query("select id,phone,username from user limit 1"); var_dump($res); $pool->recycle($mysql); $response->end("

Test

"); }); $server->handle('/test', function ($request, $response) { $response->end("

Test

"); }); $server->handle('/stop', function ($request, $response) use ($server) { $response->end("

Stop

"); $server->shutdown(); }); $server->start();});
Copy after login
Source code address swoole-mysql-pool

Related free learning recommendations:mysql video tutorial

The above is the detailed content of Detailed explanation of mysql connection pool implementation based on swoole. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!