在线看片18_亚洲性吧_在线污_神马午夜达达兔_奇米色网_在线免费看污

通用連接池

EasySwoole 實(shí)現(xiàn)的通用的協(xié)程連接池管理。

組件要求

  • php: >=7.1.0
  • ext-json: *
  • easyswoole/component: ^2.2.1
  • easyswoole/spl: ^1.3
  • easyswoole/utility: ^1.1

安裝方法

composer require easyswoole/pool

倉(cāng)庫(kù)地址

easyswoole/pool

池配置

在實(shí)例化一個(gè)連接池對(duì)象時(shí),需要傳入一個(gè)連接池配置對(duì)象 EasySwoole\Pool\Config,該對(duì)象的屬性如下:

配置項(xiàng) 默認(rèn)值 說(shuō)明 備注
$intervalCheckTime 15 * 1000 定時(shí)器執(zhí)行頻率(毫秒),默認(rèn)值為 15 s 用于定時(shí)執(zhí)行連接池對(duì)象回收,創(chuàng)建操作
$maxIdleTime 10 連接池對(duì)象最大閑置時(shí)間(秒) 超過(guò)這個(gè)時(shí)間未使用的對(duì)象將會(huì)被定時(shí)器回收
$maxObjectNum 20 連接池最大數(shù)量 每個(gè)進(jìn)程最多會(huì)創(chuàng)建 $maxObjectNum 個(gè)連接池對(duì)象,如果對(duì)象都在使用,則會(huì)返回空,或者等待連接空閑
$minObjectNum 5 連接池最小數(shù)量(熱啟動(dòng)) 當(dāng)連接池對(duì)象總數(shù)低于 $minObjectNum 時(shí),會(huì)自動(dòng)創(chuàng)建連接,保持連接的活躍性,讓控制器能夠盡快地獲取連接
$getObjectTimeout 3.0 獲取連接池中連接對(duì)象的超時(shí)時(shí)間 當(dāng)連接池為空時(shí),會(huì)等待 $getObjectTimeout 秒,如果期間有連接空閑,則會(huì)返回連接對(duì)象,否則返回 null
$extraConf 額外配置信息 在實(shí)例化連接池前,可以把一些額外配置放到這里,例如數(shù)據(jù)庫(kù)配置信息、redis 配置等等
$loadAverageTime 0.001 負(fù)載閾值 并發(fā)來(lái)臨時(shí),連接池內(nèi)對(duì)象達(dá)到 maxObjectNum,此時(shí)并未達(dá)到 intervalCheckTime 周期檢測(cè),因此設(shè)定了一個(gè) 5s 負(fù)載檢測(cè),當(dāng) 5s 內(nèi),取出總時(shí)間/取出連接總次數(shù),會(huì)得到一個(gè)平均取出時(shí)間,如果小于此閾值,說(shuō)明此次并發(fā)峰值非持續(xù)性,將回收 5% 的連接

池管理器

池管理器可以做全局的連接池管理,例如在 EasySwooleEvent.php 中的 initialize 事件中注冊(cè),然后可以在控制器中獲取連接池然后進(jìn)行獲取連接:

下面以使用實(shí)現(xiàn) easyswoole/redis 組件實(shí)現(xiàn) Redis 連接池為例:

前提:先使用 composer 安裝 easyswoole/redis 組件:

composer require easyswoole/redis

定義 RedisPool 管理器

基于 AbstractPool 實(shí)現(xiàn):

新增文件 \App\Pool\RedisPool.php,內(nèi)容如下:

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://m.luan888.cn
 * @document http://m.luan888.cn
 * @contact http://m.luan888.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\Pool;

use EasySwoole\Pool\AbstractPool;
use EasySwoole\Pool\Config;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\Redis\Redis;

class RedisPool extends AbstractPool
{
    protected $redisConfig;

    /**
     * 重寫(xiě)構(gòu)造函數(shù),為了傳入 redis 配置
     * RedisPool constructor.
     * @param Config      $conf
     * @param RedisConfig $redisConfig
     * @throws \EasySwoole\Pool\Exception\Exception
     */
    public function __construct(Config $conf, RedisConfig $redisConfig)
    {
        parent::__construct($conf);
        $this->redisConfig = $redisConfig;
    }

    protected function createObject()
    {
        // 根據(jù)傳入的 redis 配置進(jìn)行 new 一個(gè) redis 連接
        $redis = new Redis($this->redisConfig);
        return $redis;
    }
}

或者基于 MagicPool 實(shí)現(xiàn):

<?php
/**
 * This file is part of EasySwoole.
 *
 * @link http://m.luan888.cn
 * @document http://m.luan888.cn
 * @contact http://m.luan888.cn/Preface/contact.html
 * @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
 */

namespace App\Pool;

use EasySwoole\Pool\Config;
use EasySwoole\Pool\MagicPool;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\Redis\Redis;

class RedisPool1 extends MagicPool
{
    /**
     * 重寫(xiě)構(gòu)造函數(shù),為了傳入 redis 配置
     * RedisPool constructor.
     * @param Config $config 連接池配置
     * @param RedisConfig $redisConfig
     * @throws \EasySwoole\Pool\Exception\Exception
     */
    public function __construct(Config $config, RedisConfig $redisConfig)
    {
        parent::__construct(function () use ($redisConfig) {
            $redis = new Redis($redisConfig);
            return $redis;
        }, $config);
    }
}

不管是基于 AbstractPool 實(shí)現(xiàn)還是基于 MagicPool 實(shí)現(xiàn)效果是一致的。

注冊(cè)連接池管理對(duì)象

EasySwooleEvent.php 中的 initialize/mainServerCreate 事件中注冊(cè),然后可以在控制器中獲取連接池然后進(jìn)行獲取連接:

<?php

public static function initialize()
{
    // TODO: Implement initialize() method.
    date_default_timezone_set('Asia/Shanghai');

    $config = new \EasySwoole\Pool\Config();

    $redisConfig1 = new \EasySwoole\Redis\Config\RedisConfig(Config::getInstance()->getConf('REDIS1'));
    $redisConfig2 = new \EasySwoole\Redis\Config\RedisConfig(Config::getInstance()->getConf('REDIS2'));

    // 注冊(cè)連接池管理對(duì)象
    \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config,$redisConfig1), 'redis1');
    \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config,$redisConfig2), 'redis2');
}

在控制器中獲取連接池中連接對(duì)象,進(jìn)行調(diào)用:

<?php

public function index()
{
    // 取出連接池管理對(duì)象,然后獲取連接對(duì)象(getObject)
    $redis1 = \EasySwoole\Pool\Manager::getInstance()->get('redis1')->getObj();
    $redis2 = \EasySwoole\Pool\Manager::getInstance()->get('redis2')->getObj();

    $redis1->set('name', '仙士可');
    var_dump($redis1->get('name'));

    $redis2->set('name', '仙士可2號(hào)');
    var_dump($redis2->get('name'));

    // 回收連接對(duì)象(將連接對(duì)象重新歸還到連接池,方便后續(xù)使用)
        \EasySwoole\Pool\Manager::getInstance()->get('redis1')->recycleObj($redis1);
        \EasySwoole\Pool\Manager::getInstance()->get('redis2')->recycleObj($redis2);

    // 釋放連接對(duì)象(將連接對(duì)象直接徹底釋放,后續(xù)不再使用)
    // \EasySwoole\Pool\Manager::getInstance()->get('redis1')->unsetObj($redis1);
    // \EasySwoole\Pool\Manager::getInstance()->get('redis2')->unsetObj($redis2);
}

池對(duì)象方法

方法名稱(chēng) 參數(shù) 說(shuō)明 備注
createObject 抽象方法,創(chuàng)建連接對(duì)象
recycleObj $obj 回收一個(gè)連接
getObj float $timeout = null, int $tryTimes = 3 在指定的超時(shí)時(shí)間 $timeout (秒)內(nèi)獲取一個(gè)連接,會(huì)重復(fù)嘗試獲取 $tryTimes 次直到獲取到,獲取失敗則返回 null
unsetObj $obj 直接釋放一個(gè)連接
idleCheck int $idleTime 回收超過(guò) $idleTime 未出隊(duì)使用的連接
itemIntervalCheck ObjectInterface $item 判斷當(dāng)前客戶(hù)端是否還可用
intervalCheck 回收連接,以及熱啟動(dòng)方法,允許外部調(diào)用熱啟動(dòng)
keepMin ?int $num = null 保持最小連接(熱啟動(dòng))
getConfig 獲取連接池的配置信息
status 獲取連接池狀態(tài)信息 獲取當(dāng)前連接池已創(chuàng)建、已使用、最大創(chuàng)建、最小創(chuàng)建數(shù)據(jù)
isPoolObject $obj 查看 $obj 對(duì)象是否由該連接池創(chuàng)建
isInPool $obj 獲取當(dāng)前連接是否在連接池內(nèi)未使用
destroy 銷(xiāo)毀該連接池
reset 重置該連接池
invoke callable $call,float $timeout = null 獲取一個(gè)連接,傳入到 $call 回調(diào)函數(shù)中進(jìn)行處理,回調(diào)結(jié)束后自動(dòng)回收連接
defer float $timeout = null 獲取一個(gè)連接,協(xié)程結(jié)束后自動(dòng)回收

getObj

獲取一個(gè)連接池的對(duì)象:

<?php

go(function () {
    $redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
    $redis = $redisPool->getObj();
    var_dump($redis->echo('仙士可'));
    $redisPool->recycleObj($redis);
});

通過(guò) getObj 方法獲取的對(duì)象,都必須調(diào)用 recycleObj 或者 unsetObj 方法進(jìn)行回收,否則連接池對(duì)象會(huì)越來(lái)越少。

unsetObj

直接釋放一個(gè)連接池的連接對(duì)象,其他協(xié)程不能再獲取到這個(gè)連接對(duì)象,而是會(huì)重新創(chuàng)建一個(gè)連接對(duì)象

釋放之后,并不會(huì)立即銷(xiāo)毀該對(duì)象,而是會(huì)在作用域結(jié)束之后銷(xiāo)毀

recycleObj

回收一個(gè)連接對(duì)象,回收之后,其他協(xié)程可以正常獲取這個(gè)連接對(duì)象。

回收之后,其他協(xié)程可以正常獲取這個(gè)連接,但在此時(shí),該連接還處于當(dāng)前協(xié)程中,如果再次調(diào)用該連接進(jìn)行數(shù)據(jù)操作,將會(huì)造成協(xié)程混亂,所以需要開(kāi)發(fā)人員自行約束,當(dāng)對(duì)這個(gè)連接對(duì)象進(jìn)行 recycleObj 操作后不能再操作這個(gè)對(duì)象

invoke

獲取一個(gè)連接,傳入到 $call 回調(diào)函數(shù)中進(jìn)行處理,回調(diào)結(jié)束后自動(dòng)回收連接:

<?php

go(function () {
    $redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
    $redisPool->invoke(function (\EasySwoole\Redis\Redis $redis) {
        var_dump($redis->echo('仙士可'));
    });
});

通過(guò)該方法無(wú)需手動(dòng)回收連接,在回調(diào)函數(shù)結(jié)束后,則自動(dòng)回收

defer

獲取一個(gè)連接,協(xié)程結(jié)束后自動(dòng)回收

<?php

go(function () {
    $redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
    $redis = $redisPool->defer();
    var_dump($redis->echo('仙士可'));
});

通過(guò)該方法無(wú)需手動(dòng)回收連接,在協(xié)程結(jié)束后,則自動(dòng)回收

需要注意的事,defer 方法是協(xié)程結(jié)束后才回收,如果你當(dāng)前協(xié)程運(yùn)行時(shí)間過(guò)長(zhǎng),則會(huì)一直無(wú)法回收,直到協(xié)程結(jié)束

keepMin

保持最小連接(熱啟動(dòng))。

由于 easyswoole/pool 當(dāng)剛啟動(dòng)服務(wù),出現(xiàn)過(guò)大的并發(fā)時(shí),可能會(huì)突然需要幾十個(gè)甚至上百個(gè)連接,這時(shí)為了讓創(chuàng)建連接的時(shí)間分散,可以通過(guò)調(diào)用 keepMin 方法進(jìn)行預(yù)熱啟動(dòng)連接。

調(diào)用此方法后,將會(huì)預(yù)先創(chuàng)建 N 個(gè)連接,用于服務(wù)啟動(dòng)之后的控制器直接獲取連接:

預(yù)熱使用示例如下:

EasySwooleEvent.php 中的 mainServerCreate 中,當(dāng) Worker 進(jìn)程啟動(dòng)后,熱啟動(dòng)連接:

<?php

public static function mainServerCreate(EventRegister $register)
{
    $register->add($register::onWorkerStart, function (\swoole_server $server, int $workerId) {
        if ($server->taskworker == false) {
            //每個(gè)worker進(jìn)程都預(yù)創(chuàng)建連接
            \EasySwoole\Pool\Manager::getInstance()->get('redis')->keepMin(10);
            var_dump(\EasySwoole\Pool\Manager::getInstance()->get('redis')->status());
        }
    });
}

將會(huì)輸出:

array(4) {
  ["created"]=>
  int(10)
  ["inuse"]=>
  int(0)
  ["max"]=>
  int(20)
  ["min"]=>
  int(5)
}

keepMin 是根據(jù)不同進(jìn)程,創(chuàng)建不同的連接的,比如你有 10 個(gè) Worker 進(jìn)程,將會(huì)輸出 10 次,總共創(chuàng)建 10 * 10 = 100 個(gè)連接

getConfig

獲取連接池的配置:

<?php

$redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
var_dump($redisPool->getConfig());    

destroy

銷(xiāo)毀連接池。

調(diào)用之后,連接池剩余的所有鏈接都會(huì)被執(zhí)行 unsetObj,并且將關(guān)閉連接隊(duì)列,調(diào)用之后 getObj 等方法都將失效:

<?php

go(function () {
    $redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
    var_dump($redisPool->getObj());
    $redisPool->destroy();
    var_dump($redisPool->getObj());
});

reset

重置連接池。

調(diào)用 reset 之后,會(huì)自動(dòng)調(diào)用 destroy 銷(xiāo)毀連接池,并在下一次 getObj 時(shí)重新初始化該連接池:

<?php

go(function (){
    $redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
    var_dump($redisPool->getObj());
    $redisPool->reset();
    var_dump($redisPool->getObj());
});

status

獲取連接池當(dāng)前狀態(tài),調(diào)用之后將輸出:

<?php

go(function () {
    $redisPool = new \App\Pool\RedisPool(new \EasySwoole\Pool\Config(), new \EasySwoole\Redis\Config\RedisConfig(\EasySwoole\EasySwoole\Config::getInstance()->getConf('REDIS')));
    var_dump($redisPool->status());
});
array(4) {
  ["created"]=>
  int(10)
  ["inuse"]=>
  int(0)
  ["max"]=>
  int(20)
  ["min"]=>
  int(5)
}

idleCheck

回收空閑超時(shí)的連接

intervalCheck

調(diào)用此方法后,將調(diào)用 idleCheckkeepMin 方法,用于手動(dòng)回收空閑連接和手動(dòng)熱啟動(dòng)連接

<?php

public function intervalCheck()
{
    $this->idleCheck($this->getConfig()->getMaxIdleTime());
    $this->keepMin($this->getConfig()->getMinObjectNum());
}

itemIntervalCheck

在內(nèi)部定時(shí)器丟棄超時(shí)客戶(hù)端(閑置了超過(guò)指定時(shí)間,就先斷開(kāi))時(shí),會(huì)觸發(fā) itemIntervalCheck 函數(shù),并將客戶(hù)端傳入,用戶(hù)通過(guò)這個(gè)函數(shù)可以實(shí)現(xiàn)判斷客戶(hù)端是否可用的邏輯。

該函數(shù)如果返回 true 代表可用(默認(rèn)情況),返回false 將會(huì)導(dǎo)致該客戶(hù)端直接被丟棄。

可用于:維持客戶(hù)端心跳等。如 orm 中對(duì)其使用場(chǎng)景如下:維持 mysql 連接,減少 mysql 掉線(xiàn) gone away 的幾率

<?php
/**
 * @param MysqliClient $item
 * @return bool
 */
public function itemIntervalCheck($item): bool
{
    /*
     * 如果最后一次使用時(shí)間超過(guò) autoPing 間隔
     */
    /** @var Config $config */
    $config = $this->getConfig();
    if ($config->getAutoPing() > 0 && (time() - $item->__lastUseTime > $config->getAutoPing())) {
        try {
            // 執(zhí)行一個(gè)sql觸發(fā)活躍信息
            $item->rawQuery('select 1');
            // 標(biāo)記使用時(shí)間,避免被再次 gc
            $item->__lastUseTime = time();
            return true;
        } catch (\Throwable $throwable) {
            // 異常說(shuō)明該鏈接出錯(cuò)了,return 進(jìn)行回收
            return false;
        }
    } else {
        return true;
    }
}

基本使用

定義池對(duì)象

<?php

class Std implements \EasySwoole\Pool\ObjectInterface
{
    function gc()
    {
        /*
         * 本對(duì)象被 pool 執(zhí)行 unset 的時(shí)候
         */
    }

    function objectRestore()
    {
        /*
         * 回歸到連接池的時(shí)候
         */
    }

    function beforeUse(): ?bool
    {
        /*
         * 取出連接池的時(shí)候,若返回false,則當(dāng)前對(duì)象被棄用回收
         */
        return true;
    }

    public function who()
    {
        return spl_object_id($this);
    }
}

定義池

<?php

class StdPool extends \EasySwoole\Pool\AbstractPool
{
    protected function createObject()
    {
        return new Std();
    }
}

不一定非要在創(chuàng)建對(duì)象方法 createObject() 中返回 EasySwoole\Pool\ObjectInterface 對(duì)象,任意類(lèi)型對(duì)象均可

pool 組件版本 >= 1.0.2 后,提供了 魔術(shù)池 支持,可以快速進(jìn)行定義池。

<?php

use \EasySwoole\Pool\MagicPool;

$magic = new MagicPool(function () {
    return new \stdClass(); // 示例,可以返回實(shí)現(xiàn)了 ObjectInterface 的對(duì)象
});

// 注冊(cè)后獲取
$test = $magic->getObj();
// 歸還
$magic->recycleObj($test);

魔術(shù)池構(gòu)造方法的第二個(gè)參數(shù),可以接收一個(gè) configEasySwoole\Pool\Config 類(lèi)),用于定義池?cái)?shù)量等配置。

簡(jiǎn)單示例

<?php

$config = new \EasySwoole\Pool\Config();
$pool = new StdPool($config);

go(function () use ($pool) {
    $obj = $pool->getObj();
    $obj2 = $pool->getObj();
    var_dump($obj->who());
    var_dump($obj2->who());
});

進(jìn)階使用

基于 pool 實(shí)現(xiàn)的 Redis 連接池

[基于 pool 實(shí)現(xiàn)的 MySql 連接池]()

相關(guān)倉(cāng)庫(kù)

easyswoole/redis-pool

主站蜘蛛池模板: 在线免费观看av片 | 久精品在线观看 | 制服丝袜亚洲色图 | 强制高潮抽搐sm调教高h | 91精品国产成人 | 亚洲三级a | 午夜精品福利在线观看 | 国产一级大片在线观看 | 亚洲欧美日本一区 | 91亚洲国产成人精品性色 | 都市激情中文字幕 | 女人十八岁毛片 | www.成人在线 | 天天爽天天| 91成人福利 | 五月婷婷亚洲综合 | 日批视频在线看 | av黄色在线播放 | 国产精品午夜影院 | 日啪 | 精品综合久久 | 久久黄色小视频 | 成人久久久精品乱码一区二区三区 | 日韩av免费在线播放 | 日韩一区二区三区三四区视频在线观看 | 四虎网址在线观看 | 亚洲成人精品一区二区 | 91在线精品一区二区三区 | 中文字幕有码视频 | 亚洲人体视频 | 久热国产视频 | 在线看免费av | 久久久精品综合 | 深夜视频在线 | 亚洲成人免费观看 | 一级成人免费视频 | 免费在线观看黄色 | 亚洲一区二区三区在线播放 | 中国成人毛片 | 亚洲免费观看高清 | 特级毛片在线 |