摘要:連接池是一個(gè)基于的通用連接池,常被用作數(shù)據(jù)庫連接池。依賴依賴版本安裝通過安裝。使用更多示例。
連接池
open-smf/connection-pool 是一個(gè)基于Swoole的通用連接池,常被用作數(shù)據(jù)庫連接池。依賴
依賴 | 版本 |
---|---|
PHP | >=7.0.0 |
Swoole | >=4.2.9 Recommend 4.2.13+ |
通過Composer安裝。
composer require "open-smf/connection-pool:~1.0"使用
更多示例。
可用的連接器
連接器 | 說明 |
---|---|
CoroutineMySQLConnector | SwooleCoroutineMySQL的實(shí)例 |
CoroutinePostgreSQLConnector | SwooleCoroutinePostgreSQL的實(shí)例,編譯Swoole時(shí)需要添加參數(shù)--enable-coroutine-postgresql |
CoroutineRedisConnector | SwooleCoroutineRedis的實(shí)例 |
PhpRedisConnector | Redis的實(shí)例,需要安裝redis |
YourConnector | YourConnector必須實(shí)現(xiàn)接口ConnectorInterface,任何對(duì)象均可作為連接實(shí)例 |
基本用法
use SmfConnectionPoolConnectionPool; use SmfConnectionPoolConnectorsCoroutineMySQLConnector; use SwooleCoroutineMySQL; go(function () { // MySQL連接數(shù)區(qū)間:[10, 30] $pool = new ConnectionPool( [ "minActive" => 10, "maxActive" => 30, "maxWaitTime" => 5, "maxIdleTime" => 20, "idleCheckInterval" => 10, ], new CoroutineMySQLConnector, // 指明連接器實(shí)例,這里使用協(xié)程MySQL連接器,這樣就可以創(chuàng)建一個(gè)協(xié)程MySQL的數(shù)據(jù)庫連接池 [ "host" => "127.0.0.1", "port" => "3306", "user" => "root", "password" => "xy123456", "database" => "mysql", "timeout" => 10, "charset" => "utf8mb4", "strict_type" => true, "fetch_mode" => true, ] ); echo "初始化連接池... "; $pool->init(); defer(function () use ($pool) { echo "關(guān)閉連接池... "; $pool->close(); }); echo "從連接池中借出連接... "; /**@var MySQL $connection */ $connection = $pool->borrow(); // 執(zhí)行查詢語句 $status = $connection->query("SHOW STATUS LIKE "Threads_connected""); echo "用完連接后,盡快歸還... "; $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, // 每個(gè)Worker持有一個(gè)獨(dú)立的連接池 ]); } 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連接數(shù)區(qū)間:[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連接數(shù)區(qū)間:[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啟動(dòng)時(shí)創(chuàng)建MySQL和Redis連接池 $this->swoole->on("WorkerStart", $createPools); // Worker正常退出或錯(cuò)誤退出時(shí),關(guān)閉連接池,釋放連接 $this->swoole->on("WorkerStop", $closePools); $this->swoole->on("WorkerError", $closePools); } public function start() { $this->swoole->start(); } } // 啟用協(xié)程Runtime來讓PhpRedis擴(kuò)展一鍵協(xié)程化 SwooleRuntime::enableCoroutine(true); $server = new HttpServer("0.0.0.0", 5200); $server->start();
本人已用于生產(chǎn)環(huán)境,表現(xiàn)穩(wěn)定
貢獻(xiàn)Github,歡迎 Star & PR。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/31014.html
摘要:一個(gè)基于協(xié)議,開發(fā)的數(shù)據(jù)庫連接池。也可以通過其自身的管理機(jī)制來監(jiān)視數(shù)據(jù)庫連接的數(shù)量使用情況等。超出最大連接數(shù)會(huì)采用協(xié)程掛起,等到有連接關(guān)閉再恢復(fù)協(xié)程繼續(xù)操作。 SMProxy GITHUB:https://github.com/louislivi/... Swoole MySQL Proxy 一個(gè)基于 MySQL 協(xié)議,Swoole 開發(fā)的MySQL數(shù)據(jù)庫連接池。 原理 將數(shù)據(jù)庫連接作...
摘要:訪問安全問題為什么說有訪問安全問題呢傳統(tǒng)地,在的的環(huán)境中,很少有遇到所謂變量安全訪問問題。上下文管理器為了解決這個(gè)問題,我們引入?yún)f(xié)程上下文管理這樣的概念,由此來實(shí)現(xiàn)每個(gè)協(xié)程環(huán)境內(nèi)的數(shù)據(jù)隔離。 訪問安全問題 為什么說有訪問安全問題呢?傳統(tǒng)地,在php的的環(huán)境中,很少有Phper遇到所謂變量安全訪問問題。舉個(gè)例子,代碼大約如下: class db { protected stati...
閱讀 2508·2023-04-25 19:31
閱讀 2244·2021-11-04 16:11
閱讀 2811·2021-10-08 10:05
閱讀 1520·2021-09-30 09:48
閱讀 2319·2019-08-30 15:56
閱讀 2414·2019-08-30 15:56
閱讀 2177·2019-08-30 15:53
閱讀 2272·2019-08-30 15:44