首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swoole mysql协程链接池
{{{ namespace Louislivi\Model; class MySQLException extends \Exception { } }}} {{{ namespace Louislivi\Model; class MySQLPool { static protected $init = false; static protected $spareConns = []; static protected $busyConns = []; static protected $connsConfig; static protected $connsNameMap = []; static protected $pendingFetchCount = []; static protected $resumeFetchCount = []; /** * @param array $connsConfig * * @throws MySQLException */ static public function init(array $connsConfig) { if (self::$init) { return; } self::$connsConfig = $connsConfig; foreach ($connsConfig as $name => $config) { self::$spareConns[$name] = []; self::$busyConns[$name] = []; self::$pendingFetchCount[$name] = 0; self::$resumeFetchCount[$name] = 0; if ($config['maxSpareConns'] <= 0 || $config['maxConns'] <= 0) { throw new MySQLException("Invalid maxSpareConns or maxConns in {$name}"); } } self::$init = true; } /** * @param \Swoole\Coroutine\MySQL $conn * * @throws MySQLException */ static public function recycle(\Swoole\Coroutine\MySQL $conn) { if (!self::$init) { throw new MySQLException('Should call MySQLPool::init.'); } $id = spl_object_hash($conn); $connName = self::$connsNameMap[$id]; if (isset(self::$busyConns[$connName][$id])) { unset(self::$busyConns[$connName][$id]); } else { throw new MySQLException('Unknow MySQL connection.'); } $connsPool = &self::$spareConns[$connName]; if ($conn->connected) { if (count($connsPool) >= self::$connsConfig[$connName]['maxSpareConns']) { $conn->close(); } else { $connsPool[] = $conn; if (self::$pendingFetchCount[$connName] > 0) { self::$resumeFetchCount[$connName]++; self::$pendingFetchCount[$connName]--; \Swoole\Coroutine::resume('MySQLPool::' . $connName); } return; } } unset(self::$connsNameMap[$id]); } /** * @param $connName * * @return bool|mixed|\Swoole\Coroutine\MySQL * @throws MySQLException */ static public function fetch($connName) { if (!self::$init) { throw new MySQLException('Should call MySQLPool::init!'); } if (!isset(self::$connsConfig[$connName])) { throw new MySQLException("Unvalid connName: {$connName}."); } $connsPool = &self::$spareConns[$connName]; if (!empty($connsPool) && count($connsPool) > self::$resumeFetchCount[$connName]) { $conn = array_pop($connsPool); self::$busyConns[$connName][spl_object_hash($conn)] = $conn; return $conn; } if (count(self::$busyConns[$connName]) + count($connsPool) == self::$connsConfig[$connName]['maxConns']) { self::$pendingFetchCount[$connName]++; if (\Swoole\Coroutine::suspend('MySQLPool::' . $connName) == false) { self::$pendingFetchCount[$connName]--; throw new MySQLException('Reach max connections! Cann\'t pending fetch!'); } self::$resumeFetchCount[$connName]--; if (!empty($connsPool)) { $conn = array_pop($connsPool); self::$busyConns[$connName][spl_object_hash($conn)] = $conn; return $conn; } else { return false;//should not happen } } $conn = new \Swoole\Coroutine\MySQL(); $id = spl_object_hash($conn); self::$connsNameMap[$id] = $connName; self::$busyConns[$connName][$id] = $conn; if ($conn->connect(self::$connsConfig[$connName]['serverInfo']) == false) { unset(self::$busyConns[$connName][$id]); unset(self::$connsNameMap[$id]); throw new MySQLException('Cann\'t connect to MySQL server: ' . json_encode(self::$connsConfig[$connName]['serverInfo'])); } return $conn; } } }}}
发布于5年前 · 0 次浏览 · 来自
提问
依
依然心痛
{{{ namespace Louislivi\Model; class MySQLException extends \Exception { } }}} {{{ namespace Louislivi\Model; class MySQLPool { static protected $init = false; static protected $spareConns = []; static protected $busyConns = []; static protected $connsConfig; static protected $connsNameMap = []; static protected $pendingFetchCount = []; static protected $resumeFetchCount = []; /** * @param array $connsConfig * * @throws MySQLException */ static public function init(array $connsConfig) { if (self::$init) { return; } self::$connsConfig = $connsConfig; foreach ($connsConfig as $name => $config) { self::$spareConns[$name] = []; self::$busyConns[$name] = []; self::$pendingFetchCount[$name] = 0; self::$resumeFetchCount[$name] = 0; if ($config['maxSpareConns'] <= 0 || $config['maxConns'] <= 0) { throw new MySQLException("Invalid maxSpareConns or maxConns in {$name}"); } } self::$init = true; } /** * @param \Swoole\Coroutine\MySQL $conn * * @throws MySQLException */ static public function recycle(\Swoole\Coroutine\MySQL $conn) { if (!self::$init) { throw new MySQLException('Should call MySQLPool::init.'); } $id = spl_object_hash($conn); $connName = self::$connsNameMap[$id]; if (isset(self::$busyConns[$connName][$id])) { unset(self::$busyConns[$connName][$id]); } else { throw new MySQLException('Unknow MySQL connection.'); } $connsPool = &self::$spareConns[$connName]; if ($conn->connected) { if (count($connsPool) >= self::$connsConfig[$connName]['maxSpareConns']) { $conn->close(); } else { $connsPool[] = $conn; if (self::$pendingFetchCount[$connName] > 0) { self::$resumeFetchCount[$connName]++; self::$pendingFetchCount[$connName]--; \Swoole\Coroutine::resume('MySQLPool::' . $connName); } return; } } unset(self::$connsNameMap[$id]); } /** * @param $connName * * @return bool|mixed|\Swoole\Coroutine\MySQL * @throws MySQLException */ static public function fetch($connName) { if (!self::$init) { throw new MySQLException('Should call MySQLPool::init!'); } if (!isset(self::$connsConfig[$connName])) { throw new MySQLException("Unvalid connName: {$connName}."); } $connsPool = &self::$spareConns[$connName]; if (!empty($connsPool) && count($connsPool) > self::$resumeFetchCount[$connName]) { $conn = array_pop($connsPool); self::$busyConns[$connName][spl_object_hash($conn)] = $conn; return $conn; } if (count(self::$busyConns[$connName]) + count($connsPool) == self::$connsConfig[$connName]['maxConns']) { self::$pendingFetchCount[$connName]++; if (\Swoole\Coroutine::suspend('MySQLPool::' . $connName) == false) { self::$pendingFetchCount[$connName]--; throw new MySQLException('Reach max connections! Cann\'t pending fetch!'); } self::$resumeFetchCount[$connName]--; if (!empty($connsPool)) { $conn = array_pop($connsPool); self::$busyConns[$connName][spl_object_hash($conn)] = $conn; return $conn; } else { return false;//should not happen } } $conn = new \Swoole\Coroutine\MySQL(); $id = spl_object_hash($conn); self::$connsNameMap[$id] = $connName; self::$busyConns[$connName][$id] = $conn; if ($conn->connect(self::$connsConfig[$connName]['serverInfo']) == false) { unset(self::$busyConns[$connName][$id]); unset(self::$connsNameMap[$id]); throw new MySQLException('Cann\'t connect to MySQL server: ' . json_encode(self::$connsConfig[$connName]['serverInfo'])); } return $conn; } } }}}
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2019-10-11
榭
榭寄生Mistletoe
没有问题吧?
赞
0
回复
微信公众号
热门内容
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。