From 2ca845a65cdec8dcc5208fb5711c29ccfc163e4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 5 Jul 2021 11:23:57 +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 --- System/Abstracts/Pool.php | 16 +++------------- System/Cache/Redis.php | 4 ++-- System/Pool/Redis.php | 30 ++++++++---------------------- 3 files changed, 13 insertions(+), 37 deletions(-) diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 24d3364f..b227db35 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -174,6 +174,9 @@ abstract class Pool extends Component { if (!isset(static::$_items[$name])) { static::$_items[$name] = new Channel(Config::get('databases.pool.max', 10)); + if ($this->creates === -1) { + $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection'], $name); + } } return static::$_items[$name]; } @@ -250,19 +253,6 @@ abstract class Pool extends Component } - /** - * @param string $name - * @return bool - */ - public function canCreate(string $name): bool - { - if (!isset(static::$hasCreate[$name])) { - static::$hasCreate[$name] = 0; - } - return static::$hasCreate[$name] < $this->max; - } - - /** * @param string $name * @return bool diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index aeb5872f..f6753d4e 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -55,9 +55,9 @@ class Redis extends Component $config = $this->get_config(); $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; - $length = env('REDIS.POOL_LENGTH', 100); + $length = (int)env('REDIS.POOL_LENGTH', 100); - $connections->initConnections('redis', 'redis:' . $name, true, $length); + $connections->initConnections('redis', $name, true, $length); } diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index 8c060720..4113997c 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -9,6 +9,7 @@ use Exception; use HttpServer\Http\Context; use Redis as SRedis; use Snowflake\Abstracts\Pool; +use Snowflake\Exception\ConfigException; use Snowflake\Exception\RedisConnectException; use Swoole\Coroutine; use Swoole\Runtime; @@ -32,19 +33,6 @@ class Redis extends Pool } - /** - * @param string $name - * @return bool - */ - public function canCreate(string $name): bool - { - if (!isset(static::$hasCreate[$name])) { - static::$hasCreate[$name] = 0; - } - return static::$hasCreate[$name] >= $this->max; - } - - /** * @param mixed $config * @param bool $isMaster @@ -53,9 +41,9 @@ class Redis extends Pool */ public function get(mixed $config, bool $isMaster = false): mixed { - $coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster); + $coroutineName = $this->name('redis', $config['host'], $isMaster); if (!Context::hasContext($coroutineName)) { - return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config)); + return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config)); } return Context::getContext($coroutineName); } @@ -96,17 +84,16 @@ class Redis extends Pool /** * @param array $config * @param bool $isMaster + * @throws ConfigException */ public function release(array $config, bool $isMaster = false) { - $coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster); + $coroutineName = $this->name('redis', $config['host'], $isMaster); if (!Context::hasContext($coroutineName)) { return; } $this->push($coroutineName, Context::getContext($coroutineName)); - Context::remove($coroutineName); - $this->lastTime = time(); } /** @@ -116,12 +103,12 @@ class Redis extends Pool */ public function destroy(array $config, bool $isMaster = false) { - $coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster); + $coroutineName = $this->name('redis', $config['host'], $isMaster); if (Context::hasContext($coroutineName)) { $this->decrement($coroutineName); } - Context::remove($coroutineName); - $this->flush(0); + Context::remove($coroutineName); + $this->flush(0); } /** @@ -150,5 +137,4 @@ class Redis extends Pool } - }