如何在 Swoole 中优雅的实现 MySQL 连接池

一、为什么需要连接池 ?

数据库连接池指的是程序和数据库之间保持一定数量的连接不断开,

并且各个请求的连接可以相互复用,

减少重复连接数据库带来的资源消耗,

一定程度上提高了程序的并发性能。

二、连接池实现要点

  • 协程:使用 MySQL 协程客户端。

使用 MySQL 协程客户端,是为了能在一个 Worker 阻塞的时候,

让出 CPU 时间片去处理其他的请求,提高整个 Worker 的并发能力。

  • 连接池存储介质:使用 \swoole\coroutine\channel 通道。

使用 channel 能够设置等待时间,等待其他的请求释放连接。

并且在等待期间,同样也可以让出 CPU 时间片去处理其他的请求。

假设选择 array 或 splqueue,无法等待其他的请求释放连接。

那么在高并发下的场景下,可能会出现连接池为空的现象。

如果连接池为空了,那么 pop 就直接返回 null 了,导致连接不可用。

注:因此不建议选择 array 或 splqueue。

三、连接池的具体实现

<?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();

四、总结

  • 定时维护空闲连接到最小值。
  • 使用用完数据库连接之后,需要手动回收连接到连接池。
  • 使用 channel 作为连接池的存储介质。

最新文章

  1. C++设计模式-Composite组合模式
  2. jQuery中的checkbox问题
  3. JAVA学习之Ecplise IDE 使用技巧(2)第二章:键盘小快手,代码辅助
  4. Python 番外 消息队列设计精要
  5. Linux中下载,压缩,解压等命令
  6. http://begin.lydsy.com/JudgeOnline/problem.php?id=2770(PKU2503 Babelfish)
  7. 文件实时同步(rsync+inotify)
  8. EXT 获取gird各值
  9. html 页面 判断第一个反应的网站并进行跳转 模仿CDN
  10. Scrollanim – CSS3 &amp; JavaScript 创建滚动动画
  11. Android NDK 使用自己的共享库(Import Module)
  12. poj1226
  13. vue中为对象添加值的问题
  14. jquery接触初级-----juqery DOM操作 之二
  15. 实验吧ctf题库web题wp
  16. Java 与 Json的互相转换
  17. 推荐系统 BPR 算法求解过程
  18. Coding 代码管理快速入门(转)
  19. Python的一个命名空间冲突,关于from-import机制
  20. 为什么使用React Native

热门文章

  1. 【BZOJ3992】[SDOI2015]序列统计 NTT+多项式快速幂
  2. (Android)react-native-splash-screen实践-解决react-native打包好后启动白屏的问题
  3. [容易]在O(1)时间复杂度删除链表节点
  4. LeetCode:寻找旋转排序数组中的最小值【153】
  5. TCP服务器端和客户端程序设计【转】
  6. RQNOJ 342 最不听话的机器人:网格dp
  7. HDU 6170 Two strings (dp)
  8. nginx的fastcgi_param参数详解
  9. poj1637 Sightseeing tour[最大流+欧拉回路]
  10. ACM学习历程—BestCoder 2015百度之星资格赛1003 IP聚合(set容器)