{"id":13566631,"url":"https://github.com/open-smf/connection-pool","last_synced_at":"2026-01-11T16:53:06.109Z","repository":{"id":43393840,"uuid":"173313293","full_name":"open-smf/connection-pool","owner":"open-smf","description":"A common connection pool based on Swoole is usually used as a database connection pool.","archived":false,"fork":false,"pushed_at":"2024-05-16T13:42:16.000Z","size":75,"stargazers_count":219,"open_issues_count":11,"forks_count":34,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-09-08T15:35:20.510Z","etag":null,"topics":["connection-pool","swoole"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/open-smf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-01T14:14:03.000Z","updated_at":"2024-09-07T14:31:59.000Z","dependencies_parsed_at":"2024-06-18T12:41:14.539Z","dependency_job_id":"3ab2525a-c202-4e12-9482-5033e932c27f","html_url":"https://github.com/open-smf/connection-pool","commit_stats":{"total_commits":83,"total_committers":5,"mean_commits":16.6,"dds":0.09638554216867468,"last_synced_commit":"f70e47dbf56f1869d3207e15825cf38810b865e0"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-smf%2Fconnection-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-smf%2Fconnection-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-smf%2Fconnection-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-smf%2Fconnection-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/open-smf","download_url":"https://codeload.github.com/open-smf/connection-pool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223075306,"owners_count":17083497,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["connection-pool","swoole"],"created_at":"2024-08-01T13:02:13.548Z","updated_at":"2026-01-11T16:53:06.081Z","avatar_url":"https://github.com/open-smf.png","language":"PHP","funding_links":[],"categories":["PHP","Database"],"sub_categories":[],"readme":"# Connection pool\nA common connection pool based on Swoole is usually used as the database connection pool.\n\n[![Latest Version](https://img.shields.io/github/release/open-smf/connection-pool.svg)](https://github.com/open-smf/connection-pool/releases)\n[![PHP Version](https://img.shields.io/packagist/php-v/open-smf/connection-pool.svg?color=green)](https://secure.php.net)\n[![Total Downloads](https://poser.pugx.org/open-smf/connection-pool/downloads)](https://packagist.org/packages/open-smf/connection-pool)\n[![License](https://poser.pugx.org/open-smf/connection-pool/license)](LICENSE)\n\n## Requirements\n\n| Dependency | Requirement |\n| -------- | -------- |\n| [PHP](https://secure.php.net/manual/en/install.php) | `\u003e=7.0.0` |\n| [Swoole](https://github.com/swoole/swoole-src) | `\u003e=4.2.9` `Recommend 4.2.13+` |\n\n## Install\n\u003e Install package via [Composer](https://getcomposer.org/).\n\n```shell\n# PHP 7.x\ncomposer require \"open-smf/connection-pool:~1.0\"\n# PHP 8.x\ncomposer require \"open-smf/connection-pool:~2.0\"\n```\n\n## Usage\n\u003e See more [examples](examples).\n\n- Available connectors\n\n| Connector | Connection description |\n| -------- | -------- |\n| CoroutineMySQLConnector | Instance of `Swoole\\Coroutine\\MySQL` |\n| CoroutinePostgreSQLConnector | Instance of `Swoole\\Coroutine\\PostgreSQL`, require configuring `Swoole` with `--enable-coroutine-postgresql`|\n| CoroutineRedisConnector | Instance of `Swoole\\Coroutine\\Redis` |\n| PhpRedisConnector | Instance of `Redis`, require [redis](https://pecl.php.net/package/redis) |\n| PDOConnector | Instance of `PDO`, require [PDO](https://www.php.net/manual/en/book.pdo.php) |\n| YourConnector | `YourConnector` must implement interface `ConnectorInterface`, any object can be used as a connection instance |\n\n- Basic usage\n\n```php\nuse Smf\\ConnectionPool\\ConnectionPool;\nuse Smf\\ConnectionPool\\Connectors\\CoroutineMySQLConnector;\nuse Swoole\\Coroutine\\MySQL;\n\ngo(function () {\n    // All MySQL connections: [10, 30]\n    $pool = new ConnectionPool(\n        [\n            'minActive'         =\u003e 10,\n            'maxActive'         =\u003e 30,\n            'maxWaitTime'       =\u003e 5,\n            'maxIdleTime'       =\u003e 20,\n            'idleCheckInterval' =\u003e 10,\n        ],\n        new CoroutineMySQLConnector,\n        [\n            'host'        =\u003e '127.0.0.1',\n            'port'        =\u003e '3306',\n            'user'        =\u003e 'root',\n            'password'    =\u003e 'xy123456',\n            'database'    =\u003e 'mysql',\n            'timeout'     =\u003e 10,\n            'charset'     =\u003e 'utf8mb4',\n            'strict_type' =\u003e true,\n            'fetch_mode'  =\u003e true,\n        ]\n    );\n    echo \"Initializing connection pool\\n\";\n    $pool-\u003einit();\n    defer(function () use ($pool) {\n        echo \"Closing connection pool\\n\";\n        $pool-\u003eclose();\n    });\n\n    echo \"Borrowing the connection from pool\\n\";\n    /**@var MySQL $connection */\n    $connection = $pool-\u003eborrow();\n\n    $status = $connection-\u003equery('SHOW STATUS LIKE \"Threads_connected\"');\n\n    echo \"Return the connection to pool as soon as possible\\n\";\n    $pool-\u003ereturn($connection);\n\n    var_dump($status);\n});\n```\n\n- Usage in Swoole Server\n\n```php\nuse Smf\\ConnectionPool\\ConnectionPool;\nuse Smf\\ConnectionPool\\ConnectionPoolTrait;\nuse Smf\\ConnectionPool\\Connectors\\CoroutineMySQLConnector;\nuse Smf\\ConnectionPool\\Connectors\\PhpRedisConnector;\nuse Swoole\\Coroutine\\MySQL;\nuse Swoole\\Http\\Request;\nuse Swoole\\Http\\Response;\nuse Swoole\\Http\\Server;\n\nclass HttpServer\n{\n    use ConnectionPoolTrait;\n\n    protected $swoole;\n\n    public function __construct(string $host, int $port)\n    {\n        $this-\u003eswoole = new Server($host, $port);\n\n        $this-\u003esetDefault();\n        $this-\u003ebindWorkerEvents();\n        $this-\u003ebindHttpEvent();\n    }\n\n    protected function setDefault()\n    {\n        $this-\u003eswoole-\u003eset([\n            'daemonize'             =\u003e false,\n            'dispatch_mode'         =\u003e 1,\n            'max_request'           =\u003e 8000,\n            'open_tcp_nodelay'      =\u003e true,\n            'reload_async'          =\u003e true,\n            'max_wait_time'         =\u003e 60,\n            'enable_reuse_port'     =\u003e true,\n            'enable_coroutine'      =\u003e true,\n            'http_compression'      =\u003e false,\n            'enable_static_handler' =\u003e false,\n            'buffer_output_size'    =\u003e 4 * 1024 * 1024,\n            'worker_num'            =\u003e 4, // Each worker holds a connection pool\n        ]);\n    }\n\n    protected function bindHttpEvent()\n    {\n        $this-\u003eswoole-\u003eon('Request', function (Request $request, Response $response) {\n            $pool1 = $this-\u003egetConnectionPool('mysql');\n            /**@var MySQL $mysql */\n            $mysql = $pool1-\u003eborrow();\n            $status = $mysql-\u003equery('SHOW STATUS LIKE \"Threads_connected\"');\n            // Return the connection to pool as soon as possible\n            $pool1-\u003ereturn($mysql);\n\n\n            $pool2 = $this-\u003egetConnectionPool('redis');\n            /**@var \\Redis $redis */\n            $redis = $pool2-\u003eborrow();\n            $clients = $redis-\u003einfo('Clients');\n            // Return the connection to pool as soon as possible\n            $pool2-\u003ereturn($redis);\n\n            $json = [\n                'status'  =\u003e $status,\n                'clients' =\u003e $clients,\n            ];\n            // Other logic\n            // ...\n            $response-\u003eheader('Content-Type', 'application/json');\n            $response-\u003eend(json_encode($json));\n        });\n    }\n\n    protected function bindWorkerEvents()\n    {\n        $createPools = function () {\n            // All MySQL connections: [4 workers * 2 = 8, 4 workers * 10 = 40]\n            $pool1 = new ConnectionPool(\n                [\n                    'minActive' =\u003e 2,\n                    'maxActive' =\u003e 10,\n                ],\n                new CoroutineMySQLConnector,\n                [\n                    'host'        =\u003e '127.0.0.1',\n                    'port'        =\u003e '3306',\n                    'user'        =\u003e 'root',\n                    'password'    =\u003e 'xy123456',\n                    'database'    =\u003e 'mysql',\n                    'timeout'     =\u003e 10,\n                    'charset'     =\u003e 'utf8mb4',\n                    'strict_type' =\u003e true,\n                    'fetch_mode'  =\u003e true,\n                ]);\n            $pool1-\u003einit();\n            $this-\u003eaddConnectionPool('mysql', $pool1);\n\n            // All Redis connections: [4 workers * 5 = 20, 4 workers * 20 = 80]\n            $pool2 = new ConnectionPool(\n                [\n                    'minActive' =\u003e 5,\n                    'maxActive' =\u003e 20,\n                ],\n                new PhpRedisConnector,\n                [\n                    'host'     =\u003e '127.0.0.1',\n                    'port'     =\u003e '6379',\n                    'database' =\u003e 0,\n                    'password' =\u003e null,\n                ]);\n            $pool2-\u003einit();\n            $this-\u003eaddConnectionPool('redis', $pool2);\n        };\n        $closePools = function () {\n            $this-\u003ecloseConnectionPools();\n        };\n        $this-\u003eswoole-\u003eon('WorkerStart', $createPools);\n        $this-\u003eswoole-\u003eon('WorkerStop', $closePools);\n        $this-\u003eswoole-\u003eon('WorkerError', $closePools);\n    }\n\n    public function start()\n    {\n        $this-\u003eswoole-\u003estart();\n    }\n}\n\n// Enable coroutine for PhpRedis\nSwoole\\Runtime::enableCoroutine();\n$server = new HttpServer('0.0.0.0', 5200);\n$server-\u003estart();\n```\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-smf%2Fconnection-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopen-smf%2Fconnection-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-smf%2Fconnection-pool/lists"}