日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql连接池_基于Swoole的通用连接池 - 数据库连接池(life)

發布時間:2025/4/5 数据库 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql连接池_基于Swoole的通用连接池 - 数据库连接池(life) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
open-smf/connection-pool 是一個基于Swoole的通用連接池,常被用作數據庫連接池。

依賴

依賴版本PHP>=7.0.0Swoole>=4.2.9Recommend 4.2.13+

安裝

通過Composer安裝。composer require "open-smf/connection-pool:~1.0"

使用

更多示例。
  • 可用的連接器

連接器說明CoroutineMySQLConnectorSwooleCoroutineMySQL的實例CoroutinePostgreSQLConnectorSwooleCoroutinePostgreSQL的實例,編譯Swoole時需要添加參數--enable-coroutine-postgresqlCoroutineRedisConnectorSwooleCoroutineRedis的實例PhpRedisConnectorRedis的實例,需要安裝redisYourConnectorYourConnector必須實現接口ConnectorInterface,任何對象均可作為連接實例

程序猿的生活:【粉絲福利】30G-PHP進階資料,拿到你也能30K,免費領取?zhuanlan.zhihu.com
  • 基本用法
use SmfConnectionPoolConnectionPool; use SmfConnectionPoolConnectorsCoroutineMySQLConnector; use SwooleCoroutineMySQL;go(function () {// MySQL連接數區間:[10, 30]$pool = new ConnectionPool(['minActive' => 10,'maxActive' => 30,'maxWaitTime' => 5,'maxIdleTime' => 20,'idleCheckInterval' => 10,],new CoroutineMySQLConnector, // 指明連接器實例,這里使用協程MySQL連接器,這樣就可以創建一個協程MySQL的數據庫連接池['host' => '127.0.0.1','port' => '3306','user' => 'root','password' => 'xy123456','database' => 'mysql','timeout' => 10,'charset' => 'utf8mb4','strict_type' => true,'fetch_mode' => true,]);echo "初始化連接池...n";$pool->init();defer(function () use ($pool) {echo "關閉連接池...n";$pool->close();});echo "從連接池中借出連接...n";/**@var MySQL $connection */$connection = $pool->borrow();// 執行查詢語句$status = $connection->query('SHOW STATUS LIKE "Threads_connected"');echo "用完連接后,盡快歸還...n";$pool->return($connection);var_dump($status); });
  • 在Swoole Server中的用法
use SmfConnectionPoolConnectionPool; use SmfConnectionPoolConnectionPoolTrait; use SmfConnectionPoolConnectorsCoroutineMySQLConnector; use SmfConnectionPoolConnectorsPhpRedisConnector; use SwooleCoroutineMySQL; use SwooleHttpRequest; use SwooleHttpResponse; use SwooleHttpServer;class HttpServer {use ConnectionPoolTrait;protected $swoole;public function __construct(string $host, int $port){$this->swoole = new Server($host, $port);$this->setDefault();$this->bindWorkerEvents();$this->bindHttpEvent();}protected function setDefault(){$this->swoole->set(['daemonize' => false,'dispatch_mode' => 1,'max_request' => 8000,'open_tcp_nodelay' => true,'reload_async' => true,'max_wait_time' => 60,'enable_reuse_port' => true,'enable_coroutine' => true,'http_compression' => false,'enable_static_handler' => false,'buffer_output_size' => 4 * 1024 * 1024,'worker_num' => 4, // 每個Worker持有一個獨立的連接池]);}protected function bindHttpEvent(){$this->swoole->on('Request', function (Request $request, Response $response) {$pool1 = $this->getConnectionPool('mysql');/**@var MySQL $mysql */$mysql = $pool1->borrow();$status = $mysql->query('SHOW STATUS LIKE "Threads_connected"');// 用完連接后,盡快歸還$pool1->return($mysql);$pool2 = $this->getConnectionPool('redis');/**@var Redis $redis */$redis = $pool2->borrow();$clients = $redis->info('Clients');// 用完連接后,盡快歸還$pool2->return($redis);$json = ['status' => $status,'clients' => $clients,];// Other logic// ...$response->header('Content-Type', 'application/json');$response->end(json_encode($json));});}protected function bindWorkerEvents(){$createPools = function () {// 所有的MySQL連接數區間:[4 workers * 2 = 8, 4 workers * 10 = 40]$pool1 = new ConnectionPool(['minActive' => 2,'maxActive' => 10,],new CoroutineMySQLConnector,['host' => '127.0.0.1','port' => '3306','user' => 'root','password' => 'xy123456','database' => 'mysql','timeout' => 10,'charset' => 'utf8mb4','strict_type' => true,'fetch_mode' => true,]);$pool1->init();$this->addConnectionPool('mysql', $pool1);// 所有Redis連接數區間:[4 workers * 5 = 20, 4 workers * 20 = 80]$pool2 = new ConnectionPool(['minActive' => 5,'maxActive' => 20,],new PhpRedisConnector,['host' => '127.0.0.1','port' => '6379','database' => 0,'password' => null,]);$pool2->init();$this->addConnectionPool('redis', $pool2);};$closePools = function () {$this->closeConnectionPools();};// Worker啟動時創建MySQL和Redis連接池$this->swoole->on('WorkerStart', $createPools);// Worker正常退出或錯誤退出時,關閉連接池,釋放連接$this->swoole->on('WorkerStop', $closePools);$this->swoole->on('WorkerError', $closePools);}public function start(){$this->swoole->start();} }// 啟用協程Runtime來讓PhpRedis擴展一鍵協程化 SwooleRuntime::enableCoroutine(true); $server = new HttpServer('0.0.0.0', 5200); $server->start();
  • 本人已用于生產環境,表現穩定

貢獻

Github,歡迎 Star & PR。
原文來自思否https://segmentfault.com/a/1190

總結

以上是生活随笔為你收集整理的mysql连接池_基于Swoole的通用连接池 - 数据库连接池(life)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。