From 4f341594b525b9b867f35430cf8b12842520d9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 7 Feb 2023 16:46:13 +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 | 4 +-- kiri-engine/Pool/Pool.php | 62 ++++++++++++--------------------- kiri-engine/Redis/Helper.php | 4 +-- kiri-engine/Redis/Redis.php | 3 +- 4 files changed, 27 insertions(+), 46 deletions(-) diff --git a/kiri-engine/Pool/Connection.php b/kiri-engine/Pool/Connection.php index ace15a9b..f6692c61 100644 --- a/kiri-engine/Pool/Connection.php +++ b/kiri-engine/Pool/Connection.php @@ -96,9 +96,7 @@ class Connection extends Component */ public function get(mixed $config, bool $isMaster = false): ?\PDO { - $minx = Config::get('databases.pool.min', 1); - - return $this->pool->get($config['cds'] . ($isMaster ? 'master' : 'slave'), $this->generate($config), $minx); + return $this->pool->get($config['cds'] . ($isMaster ? 'master' : 'slave'), $this->generate($config)); } diff --git a/kiri-engine/Pool/Pool.php b/kiri-engine/Pool/Pool.php index fb6375b8..5086a406 100644 --- a/kiri-engine/Pool/Pool.php +++ b/kiri-engine/Pool/Pool.php @@ -24,9 +24,6 @@ class Pool extends Component /** @var array */ private static array $_connections = []; - public int $max = 60; - - /** * @var WorkerStatus */ @@ -112,18 +109,14 @@ class Pool extends Component /** * @param $name * @param int $max - * @throws ConfigException */ public function initConnections($name, int $max = 60) { - if (isset(static::$_connections[$name])) { - $value = static::$_connections[$name]; - if ($value instanceof PoolQueue) { - return; - } + $channel = static::$_connections[$name] ?? null; + if (($channel instanceof PoolQueue) && !$channel->isClose()) { + return; } - $this->newChannel($name, $max); - $this->max = $max; + static::$_connections[$name] = new PoolQueue($max); } @@ -135,37 +128,25 @@ class Pool extends Component */ public function channel($name): PoolQueue { - if (!isset(static::$_connections[$name])) { - $this->newChannel($name); + $channel = static::$_connections[$name] ?? null; + if (!($channel instanceof PoolQueue) ) { + throw new Exception('Channel is not exists.'); } - if (static::$_connections[$name]->isClose()) { + if ($channel->isClose()) { throw new Exception('Channel is Close.'); } - return static::$_connections[$name]; - } - - - /** - * @throws ConfigException - */ - private function newChannel($name, $max = null) - { - if ($max == null) { - $max = Config::get('databases.pool.max', 10); - } - static::$_connections[$name] = new PoolQueue($max); + return $channel; } /** * @param $name * @param $callback - * @param $minx * @return array * @throws ConfigException * @throws Exception */ - public function get($name, $callback, $minx): mixed + public function get($name, $callback): mixed { $channel = $this->channel($name); if (!$channel->isEmpty()) { @@ -217,10 +198,11 @@ class Pool extends Component */ public function hasItem(string $name): bool { - if (isset(static::$_connections[$name])) { - return !static::$_connections[$name]->isEmpty(); + $channel = static::$_connections[$name] ?? null; + if (!($channel instanceof PoolQueue) || $channel->isClose()) { + return false; } - return false; + return !$channel->isEmpty(); } @@ -230,10 +212,11 @@ class Pool extends Component */ public function size(string $name): int { - if (!isset(static::$_connections[$name])) { + $channel = static::$_connections[$name] ?? null; + if (!($channel instanceof PoolQueue) || $channel->isClose()) { return 0; } - return static::$_connections[$name]->length(); + return $channel->length(); } @@ -248,7 +231,6 @@ class Pool extends Component if (!$channel->isFull()) { $channel->push($client); } - unset($client); } @@ -258,17 +240,17 @@ class Pool extends Component */ public function clean(string $name) { - if (!isset(static::$_connections[$name])) { + $channel = static::$_connections[$name] ?? null; + if (!($channel instanceof PoolQueue) || $channel->isClose()) { return; } - while (static::$_connections[$name]->length() > 0) { - $client = static::$_connections[$name]->pop(); + while ($channel->length() > 0) { + $client = $channel->pop(); if ($client instanceof StopHeartbeatCheck) { $client->stopHeartbeatCheck(); } } - static::$_connections[$name] = null; - unset(static::$_connections[$name]); + $channel->close(); } diff --git a/kiri-engine/Redis/Helper.php b/kiri-engine/Redis/Helper.php index 0a663257..0ed9e74d 100644 --- a/kiri-engine/Redis/Helper.php +++ b/kiri-engine/Redis/Helper.php @@ -82,7 +82,7 @@ class Helper implements StopHeartbeatCheck /** * @return \Redis - * @throws RedisConnectException + * @throws Exception * @throws RedisException */ public function _pdo(): \Redis @@ -96,7 +96,7 @@ class Helper implements StopHeartbeatCheck /** * @return \Redis - * @throws RedisConnectException + * @throws Exception */ private function newClient(): \Redis { diff --git a/kiri-engine/Redis/Redis.php b/kiri-engine/Redis/Redis.php index bd39f56c..b526a3b4 100644 --- a/kiri-engine/Redis/Redis.php +++ b/kiri-engine/Redis/Redis.php @@ -91,6 +91,7 @@ class Redis extends Component * @param $key * @param int $timeout * @return bool + * @throws \RedisException */ public function waite($key, int $timeout = 5): bool { @@ -189,7 +190,7 @@ SCRIPT; $config = $this->get_config(); return $this->pool->get($config['host'], static function () use ($config) { return new Helper($config); - }, 10); + }); }