• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

connection-pool: 基于Swoole的通用连接池,常被用作数据库连接池。 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

connection-pool

开源软件地址:

https://gitee.com/hhxsv5/connection-pool

开源软件介绍:

Connection pool

A common connection pool based on Swoole is usually used as the database connection pool.

Latest VersionPHP VersionTotal DownloadsLicense

Requirements

DependencyRequirement
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
ConnectorConnection description
CoroutineMySQLConnectorInstance of Swoole\Coroutine\MySQL
CoroutinePostgreSQLConnectorInstance of Swoole\Coroutine\PostgreSQL, require configuring Swoole with --enable-coroutine-postgresql
CoroutineRedisConnectorInstance of Swoole\Coroutine\Redis
PhpRedisConnectorInstance of Redis, require redis
PDOConnectorInstance of PDO, require PDO
YourConnectorYourConnector 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 PhpRedisSwoole\Runtime::enableCoroutine();$server = new HttpServer('0.0.0.0', 5200);$server->start();

License

MIT


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap