资讯专栏INFORMATION COLUMN

基于Swoole的通用连接池 - 数据库连接池

superPershing / 1757人阅读

摘要:连接池是一个基于的通用连接池,常被用作数据库连接池。依赖依赖版本安装通过安装。使用更多示例。

连接池
open-smf/connection-pool 是一个基于Swoole的通用连接池,常被用作数据库连接池。
依赖
依赖 版本
PHP >=7.0.0
Swoole >=4.2.9 Recommend 4.2.13+
安装
通过Composer安装。
composer require "open-smf/connection-pool:~1.0"
使用
更多示例。

可用的连接器

连接器 说明
CoroutineMySQLConnector SwooleCoroutineMySQL的实例
CoroutinePostgreSQLConnector SwooleCoroutinePostgreSQL的实例,编译Swoole时需要添加参数--enable-coroutine-postgresql
CoroutineRedisConnector SwooleCoroutineRedis的实例
PhpRedisConnector Redis的实例,需要安装redis
YourConnector YourConnector必须实现接口ConnectorInterface,任何对象均可作为连接实例

基本用法

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 "初始化连接池...
";
    $pool->init();
    defer(function () use ($pool) {
        echo "关闭连接池...
";
        $pool->close();
    });

    echo "从连接池中借出连接...
";
    /**@var MySQL $connection */
    $connection = $pool->borrow();
    
    // 执行查询语句
    $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, // 每个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://www.ucloud.cn/yun/31014.html

相关文章

  • SMProxy,让你项目据库操作快三倍!

    摘要:一个基于协议,开发的数据库连接池。也可以通过其自身的管理机制来监视数据库连接的数量使用情况等。超出最大连接数会采用协程挂起,等到有连接关闭再恢复协程继续操作。 SMProxy GITHUB:https://github.com/louislivi/... Swoole MySQL Proxy 一个基于 MySQL 协议,Swoole 开发的MySQL数据库连接池。 原理 将数据库连接作...

    MartinHan 评论0 收藏0
  • Swoole4.x之协程变量访问安全与协程连接实现

    摘要:访问安全问题为什么说有访问安全问题呢传统地,在的的环境中,很少有遇到所谓变量安全访问问题。上下文管理器为了解决这个问题,我们引入协程上下文管理这样的概念,由此来实现每个协程环境内的数据隔离。 访问安全问题 为什么说有访问安全问题呢?传统地,在php的的环境中,很少有Phper遇到所谓变量安全访问问题。举个例子,代码大约如下: class db { protected stati...

    aisuhua 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<