Swoole
中优雅的实现 MySQL
链接池数据库链接池指的是程序和数据库之间保持必定数量的链接不断开,
而且各个请求的链接能够相互复用,
减小重复链接数据库带来的资源消耗,
必定程度上提升了程序的并发性能。php
使用 MySQL 协程客户端,是为了能在一个 Worker 阻塞的时候,
让出 CPU 时间片去处理其余的请求,提升整个 Worker 的并发能力。mysql
使用 channel 可以设置等待时间,等待其余的请求释放链接。
而且在等待期间,一样也可让出 CPU 时间片去处理其余的请求。sql
假设选择 array 或 splqueue,没法等待其余的请求释放链接。
那么在高并发下的场景下,可能会出现链接池为空的现象。
若是链接池为空了,那么 pop 就直接返回 null 了,致使链接不可用。数据库
注:所以不建议选择 array 或 splqueue。swoole
<?php use Swoole\Coroutine\Channel; use Swoole\Coroutine\MySQL; class MysqlPool { private $min; // 最小链接数 private $max; // 最大链接数 private $count; // 当前链接数 private $connections; // 链接池 protected $freeTime; // 用于空闲链接回收判断 public static $instance; /** * MysqlPool constructor. */ public function __construct() { $this->min = 10; $this->max = 100; $this->freeTime = 10 * 3600; $this->connections = new Channel($this->max + 1); } /** * @return MysqlPool */ public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } /** * 建立链接 * @return MySQL */ protected function createConnection() { $conn = new MySQL(); $conn->connect([ 'host' => 'mysql', 'port' => '3306', 'user' => 'root', 'password' => 'root', 'database' => 'fastadmin', 'timeout' => 5 ]); return $conn; } /** * 建立链接对象 * @return array|null */ protected function createConnObject() { $conn = $this->createConnection(); return $conn ? ['last_used_time' => time(), 'conn' => $conn] : null; } /** * 初始化链接 * @return $this */ public function init() { for ($i = 0; $i < $this->min; $i++) { $obj = $this->createConnObject(); $this->count++; $this->connections->push($obj); } return $this; } /** * 获取链接 * @param int $timeout * @return mixed */ public function getConn($timeout = 3) { if ($this->connections->isEmpty()) { if ($this->count < $this->max) { $this->count++; $obj = $this->createConnObject(); } else { $obj = $this->connections->pop($timeout); } } else { $obj = $this->connections->pop($timeout); } return $obj['conn']->connected ? $obj['conn'] : $this->getConn(); } /** * 回收链接 * @param $conn */ public function recycle($conn) { if ($conn->connected) { $this->connections->push(['last_used_time' => time(), 'conn' => $conn]); } } /** * 回收空闲链接 */ public function recycleFreeConnection() { // 每 2 分钟检测一下空闲链接 swoole_timer_tick(2 * 60 * 1000, function () { if ($this->connections->length() < intval($this->max * 0.5)) { // 请求链接数还比较多,暂时不回收空闲链接 return; } while (true) { if ($this->connections->isEmpty()) { break; } $connObj = $this->connections->pop(0.001); $nowTime = time(); $lastUsedTime = $connObj['last_used_time']; // 当前链接数大于最小的链接数,而且回收掉空闲的链接 if ($this->count > $this->min && ($nowTime - $lastUsedTime > $this->freeTime)) { $connObj['conn']->close(); $this->count--; } else { $this->connections->push($connObj); } } }); } } $httpServer = new swoole_http_server('127.0.0.1',9501); $httpServer->set(['work_num' => 1]); $httpServer->on('WorkerStart', function ($request, $response) { MysqlPool::getInstance()->init()->recycleFreeConnection(); }); $httpServer->on('Request', function ($request, $response){ $conn = MysqlPool::getInstance()->getConn(); $conn->query('SELECT * FROM fa_admin WHERE id=1'); MysqlPool::getInstance()->recycle($conn); }); $httpServer->start();