From d83f061a1cb4c6a8df05b041fcda45e3e6c63631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 7 Feb 2023 17:16:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kiri-engine/Pool/Connection.php | 6 +++++- kiri-engine/Pool/Pool.php | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/kiri-engine/Pool/Connection.php b/kiri-engine/Pool/Connection.php index f6692c61..897d7926 100644 --- a/kiri-engine/Pool/Connection.php +++ b/kiri-engine/Pool/Connection.php @@ -96,7 +96,11 @@ class Connection extends Component */ public function get(mixed $config, bool $isMaster = false): ?\PDO { - return $this->pool->get($config['cds'] . ($isMaster ? 'master' : 'slave'), $this->generate($config)); + $name = $config['cds'] . ($isMaster ? 'master' : 'slave'); + if (!$this->pool->hasChannel($name, $config['pool']['max'])) { + $this->pool->initConnections($name, $config['pool']['max']); + } + return $this->pool->get($name, $this->generate($config)); } diff --git a/kiri-engine/Pool/Pool.php b/kiri-engine/Pool/Pool.php index 5086a406..4925257a 100644 --- a/kiri-engine/Pool/Pool.php +++ b/kiri-engine/Pool/Pool.php @@ -129,7 +129,7 @@ class Pool extends Component public function channel($name): PoolQueue { $channel = static::$_connections[$name] ?? null; - if (!($channel instanceof PoolQueue) ) { + if (!($channel instanceof PoolQueue)) { throw new Exception('Channel is not exists.'); } if ($channel->isClose()) { @@ -139,6 +139,19 @@ class Pool extends Component } + public function hasChannel($name, $max): bool + { + $channel = static::$_connections[$name] ?? null; + if (!($channel instanceof PoolQueue)) { + return false; + } + if ($channel->isClose()) { + return false; + } + return true; + } + + /** * @param $name * @param $callback