swoole的mysql连接池如何实现,连接池有何优势
Admin 2022-07-21 群英技术资讯 1073 次浏览
关于“swoole的mysql连接池如何实现,连接池有何优势”的知识有一些人不是很理解,对此小编给大家总结了相关内容,具有一定的参考借鉴价值,而且易于学习与理解,希望能对大家有所帮助,有这个方面学习需要的朋友就继续往下看吧。
传统的nginx+FPM模式的PHP程序而言,每次请求FPM的worker都会连接一次mysql,然后请求结束便会断开连接。对于并发小的应用来说这不会有什么问题,但是对于高并发的应用来说,频繁建立连接Connect和销毁连接Close,数据库便会成为瓶颈,相信不少人也遇到过to many connection的mysql报错吧。
连接池采用的是长连接模式,会一直保持与MySQL的连接,用完后会重新放回连接池,从而节省了建立连接和断开连接的消耗,大大降低了系统IO的消耗,一定程度上提高了程序的并发性能。如果连接池空闲,就从连接池分配一个连接,否则,请求将被加入到等待队列中。
我们采用swoole实现mysql连接池
<?php
require_once "MysqlDB.php";class MysqlPool{
private static $instance;
private $pool;
private $config;
private $pool_get_timeout;
/**
* 获取mysql进程池单例
* @param null $config
* @return MysqlPool
*/
public static function getInstance($config = null)
{
if (empty(self::$instance)) {
if (empty($config)) {
throw new RuntimeException("mysql config is empty");
}
self::$instance = new static($config);
}
return self::$instance;
}
public function __construct($config)
{
if (empty($this->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();
}}<?phpclass MysqlDB{
private $connection;
public function connect($config)
{
$connection = new \Swoole\Coroutine\MySQL();
$res = $connection->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;
}}<?php
require_once "MysqlPool.php";\Co\run(function () {
$server = new \Co\Http\Server("0.0.0.0", 9501, false);
$pool = MysqlPool::getInstance([
'pool_size'=>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("<h1>Test</h1>");
});
$server->handle('/test', function ($request, $response) {
$response->end("<h1>Test</h1>");
});
$server->handle('/stop', function ($request, $response) use ($server) {
$response->end("<h1>Stop</h1>");
$server->shutdown();
});
$server->start();});
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
注意:PHP5.5及以上版本才支持生成器。生成器提供了一种更容易的方法来实现简单的对象迭代,但没有实现一个具有 Iterator 接口的类所带来的性能开销和复杂性。生成器允...
php中str_replace的替换:1、以字符替换字符串中的一些字符;2、语法str_replace(find,replace,string,count);3、参数Find、replace、string、count。
在本篇文章里小编给大家整理的是一篇关于PHP7 preg_replace 出错及解决办法,有需要的朋友们可以跟着学习下。
关于设计模式,我们可以理解为是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。PHP设计模式有单例模式、注册模式、策略模式等等,文本主要给大家介绍的策略模式。
在前面我们了解了应该怎样去绘制图形验证码,那接下来我们一起看一看,在PHP中想要实现图像的缩放和裁剪应该怎样操作。不过在了解图像的缩放和裁剪之前我们先来认识一下下面这两个函数:
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008