From 74604324b198daaac3e9cf2786ebb13167947f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 15 Feb 2021 20:45:35 +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 | 7 ++----- System/Pool/Connection.php | 20 +++++++++++--------- System/Pool/Redis.php | 26 +++++++++----------------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index c391d49c..6f3e550c 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -124,13 +124,10 @@ abstract class Pool extends Component * @return PDO|Redis|null * @throws Exception */ - public function createConnect(array $config, string $coroutineName, callable $createHandler): PDO|Redis|null + public function createConnect(array $config, string $coroutineName, callable $createHandler): PDO|Redis|false { - if ($this->hasItem($coroutineName)) { - return $this->get($coroutineName)[1]; - } if (Context::hasContext('create:connect:' . $coroutineName)) { - return null; + return false; } Context::setContext('create:connect:' . $coroutineName, 1); diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index 3985c3ef..fba0ec58 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -26,7 +26,6 @@ class Connection extends Pool protected array $connections = []; - /** * @param $timeout */ @@ -151,13 +150,12 @@ class Connection extends Pool if (Context::hasContext($coroutineName)) { return Context::getContext($coroutineName); } - if ($this->size($coroutineName) < 1) { - $connections = $this->newClient($config, $coroutineName); - } else { - [$time, $connections] = $this->get($coroutineName); - if (!($connections instanceof PDO)) { - throw new Exception('Database exception.'); - } + if (!$this->hasItem($coroutineName)) { + return Context::setContext($coroutineName, $this->newClient($config, $coroutineName)); + } + [$time, $connections] = $this->get($coroutineName); + if (!($connections instanceof PDO)) { + throw new Exception('Database exception.'); } return Context::setContext($coroutineName, $connections); } @@ -172,7 +170,7 @@ class Connection extends Pool private function newClient($config, $coroutineName): PDO|null { $this->printClients($config['cds'], $coroutineName, true); - return $this->createConnect($this->parseConfig($config, $coroutineName), $coroutineName, function ($cds, $username, $password, $charset, $coroutineName) { + $connections = $this->createConnect($this->parseConfig($config, $coroutineName), $coroutineName, function ($cds, $username, $password, $charset, $coroutineName) { $link = new PDO($cds, $username, $password, [ PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_CASE => PDO::CASE_NATURAL, @@ -193,6 +191,10 @@ class Connection extends Pool } return $link; }); + if ($connections === false) { + return $this->newClient($config, $coroutineName); + } + return $connections; } diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index c6a699a4..d69fbf98 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -47,13 +47,13 @@ class Redis extends Pool return Context::getContext($coroutineName); } if (!$this->hasItem($coroutineName)) { - return $this->saveClient($coroutineName, $this->newClient($config, $coroutineName)); + return Context::setContext($coroutineName, $this->newClient($config, $coroutineName)); } [$time, $clients] = $this->get($coroutineName); if ($clients === null) { - return $this->saveClient($coroutineName, $this->newClient($config, $coroutineName)); + return Context::setContext($coroutineName, $this->newClient($config, $coroutineName)); } - return $this->saveClient($coroutineName, $clients); + return Context::setContext($coroutineName, $clients); } @@ -65,8 +65,7 @@ class Redis extends Pool */ private function newClient($config, $coroutineName): \Redis|null { - $this->printClients($config['host'], $coroutineName, true); - return $this->createConnect([$config, $coroutineName], $coroutineName, function ($config, $coroutineName) { + $client = $this->createConnect([$config, $coroutineName], $coroutineName, function ($config, $coroutineName) { $redis = new SRedis(); if (!$redis->connect($config['host'], (int)$config['port'], $config['timeout'])) { throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port'])); @@ -89,6 +88,11 @@ class Redis extends Pool return $redis; }); + if ($client === false) { + return $this->newClient($config, $coroutineName); + } + $this->printClients($config['host'], $coroutineName, true); + return $client; } @@ -103,18 +107,6 @@ class Redis extends Pool } - /** - * @param $coroutineName - * @param $client - * @return mixed - * @throws Exception - */ - private function saveClient($coroutineName, $client): mixed - { - return Context::setContext($coroutineName, $client); - } - - /** * @param array $config * @param bool $isMaster