From df2d887d9702c79a20b57f631c6816d7d779b092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 28 Oct 2021 18:46:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kiri-engine/Pool/Connection.php | 8 ++++++-- kiri-engine/Pool/Pool.php | 11 ++++++----- kiri-engine/Pool/Redis.php | 10 +++++----- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/kiri-engine/Pool/Connection.php b/kiri-engine/Pool/Connection.php index df4ee091..dfb181b6 100644 --- a/kiri-engine/Pool/Connection.php +++ b/kiri-engine/Pool/Connection.php @@ -6,8 +6,9 @@ namespace Kiri\Pool; use Closure; use Database\Mysql\PDO; use Exception; -use Kiri\Context; use Kiri\Abstracts\Component; +use Kiri\Abstracts\Config; +use Kiri\Context; use Kiri\Kiri; use Swoole\Error; use Throwable; @@ -92,8 +93,11 @@ class Connection extends Component if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { return $pdo; } + + $minx = Config::get('databases.pool.min', 1); + /** @var PDO $connections */ - $connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config)); + $connections = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $minx); if (Context::hasContext('begin_' . $coroutineName)) { $connections->beginTransaction(); } diff --git a/kiri-engine/Pool/Pool.php b/kiri-engine/Pool/Pool.php index f421b27c..206f14d7 100644 --- a/kiri-engine/Pool/Pool.php +++ b/kiri-engine/Pool/Pool.php @@ -115,14 +115,16 @@ class Pool extends Component /** * @param $name * @param $callback + * @param $minx * @return array * @throws ConfigException + * @throws Exception */ - public function get($name, $callback): mixed + public function get($name, $callback, $minx): mixed { $channel = $this->getChannel($name); if (!$channel->isEmpty()) { - return $this->maxIdleQuantity($channel); + return $this->maxIdleQuantity($channel, $minx); } return $callback(); } @@ -130,14 +132,13 @@ class Pool extends Component /** * @param $channel + * @param $minx * @return mixed - * @throws ConfigException * @throws Exception */ - private function maxIdleQuantity($channel): mixed + protected function maxIdleQuantity($channel, $minx): mixed { $connection = $channel->pop(); - $minx = Config::get('databases.pool.min', 1); if ($channel->length() > $minx) { $this->pop($channel, $minx); } diff --git a/kiri-engine/Pool/Redis.php b/kiri-engine/Pool/Redis.php index 061e0e58..e37a788a 100644 --- a/kiri-engine/Pool/Redis.php +++ b/kiri-engine/Pool/Redis.php @@ -5,15 +5,12 @@ declare(strict_types=1); namespace Kiri\Pool; -use Annotation\Inject; use Closure; use Exception; -use Kiri\Context; use Kiri\Abstracts\Component; -use Kiri\Events\EventProvider; +use Kiri\Context; use Kiri\Exception\ConfigException; use Kiri\Kiri; -use Server\Events\OnWorkerExit; /** * Class RedisClient @@ -37,7 +34,10 @@ class Redis extends Component if (Context::hasContext($coroutineName)) { return Context::getContext($coroutineName); } - $clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config)); + + $pool = $config['pool'] ?? ['min' => 1, 'max' => 100]; + + $clients = $this->getPool()->get($coroutineName, $this->create($coroutineName, $config), $pool['min'] ?? 1); return Context::setContext($coroutineName, $clients); }