yii-diandi/connection-pool
Composer 安装命令:
composer require yii-diandi/connection-pool
包简介
swoole连接池扩展类库
README 文档
README
A common connection pool based on Swoole is usually used as the database connection pool.
Requirements
| Dependency | Requirement |
|---|---|
| PHP | >=7.0.0 |
| Swoole | >=4.2.9 Recommend 4.2.13+ |
Install
Install package via Composer.
composer require "open-smf/connection-pool:~1.0"
Usage
See more examples.
- Available connectors
| Connector | Connection description |
|---|---|
| CoroutineMySQLConnector | Instance of Swoole\Coroutine\MySQL |
| CoroutinePostgreSQLConnector | Instance of Swoole\Coroutine\PostgreSQL, require configuring Swoole with --enable-coroutine-postgresql |
| CoroutineRedisConnector | Instance of Swoole\Coroutine\Redis |
| PhpRedisConnector | Instance of Redis, require redis |
| PDOConnector | Instance of PDO, require PDO |
| YourConnector | YourConnector must implement interface ConnectorInterface, any object can be used as a connection instance |
- Basic usage
use Smf\ConnectionPool\ConnectionPool; use Smf\ConnectionPool\Connectors\CoroutineMySQLConnector; use Swoole\Coroutine\MySQL; go(function () { // All MySQL connections: [10, 30] $pool = new ConnectionPool( [ 'minActive' => 10, 'maxActive' => 30, 'maxWaitTime' => 5, 'maxIdleTime' => 20, 'idleCheckInterval' => 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, ] ); echo "Initializing connection pool\n"; $pool->init(); defer(function () use ($pool) { echo "Closing connection pool\n"; $pool->close(); }); echo "Borrowing the connection from pool\n"; /**@var MySQL $connection */ $connection = $pool->borrow(); $status = $connection->query('SHOW STATUS LIKE "Threads_connected"'); echo "Return the connection to pool as soon as possible\n"; $pool->return($connection); var_dump($status); });
- Usage in Swoole Server
use Smf\ConnectionPool\ConnectionPool; use Smf\ConnectionPool\ConnectionPoolTrait; use Smf\ConnectionPool\Connectors\CoroutineMySQLConnector; use Smf\ConnectionPool\Connectors\PhpRedisConnector; use Swoole\Coroutine\MySQL; use Swoole\Http\Request; use Swoole\Http\Response; use Swoole\Http\Server; 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, // Each worker holds a connection pool ]); } 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"'); // Return the connection to pool as soon as possible $pool1->return($mysql); $pool2 = $this->getConnectionPool('redis'); /**@var \Redis $redis */ $redis = $pool2->borrow(); $clients = $redis->info('Clients'); // Return the connection to pool as soon as possible $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 () { // All MySQL connections: [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); // All Redis connections: [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(); }; $this->swoole->on('WorkerStart', $createPools); $this->swoole->on('WorkerStop', $closePools); $this->swoole->on('WorkerError', $closePools); } public function start() { $this->swoole->start(); } } // Enable coroutine for PhpRedis Swoole\Runtime::enableCoroutine(); $server = new HttpServer('0.0.0.0', 5200); $server->start();
License
yii-diandi/connection-pool 适用场景与选型建议
yii-diandi/connection-pool 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 11 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「swoole」 「connection-pool」 「database-connection-pool」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yii-diandi/connection-pool 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yii-diandi/connection-pool 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yii-diandi/connection-pool 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A common connection pool based on Swoole is usually used as the database connection pool.
Swoole-based object pool, connection pool
Swoole-based object pool, connection pool
annotations-scan-plugin
PDO connection pool for PHP with read/write splitting, round-robin load balancing across replicas, and automatic health checks — supports MySQL, PostgreSQL, and SQLite; a building block of the open-source foundation that Jardis-generated DDD code runs on
SForum
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-29