From 045e73bd63170042331c1dc275808a757fbddcfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 16 Feb 2021 23:42:13 +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 | 13 +++++-------- System/Pool/Redis.php | 16 +++++++--------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 6f3e550c..65648121 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -127,7 +127,12 @@ abstract class Pool extends Component public function createConnect(array $config, string $coroutineName, callable $createHandler): PDO|Redis|false { if (Context::hasContext('create:connect:' . $coroutineName)) { - return false; + while (true){ + if (Context::hasContext($coroutineName)){ + break; + } + } + return true; } Context::setContext('create:connect:' . $coroutineName, 1); diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index c30b1880..5f413564 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -151,7 +151,10 @@ class Connection extends Pool return Context::getContext($coroutineName); } if (!$this->hasItem($coroutineName)) { - return Context::setContext($coroutineName, $this->newClient($config, $coroutineName)); + if (($client = $this->newClient($config, $coroutineName)) == true) { + return Context::getContext($coroutineName); + } + return Context::setContext($coroutineName, $client); } [$time, $connections] = $this->get($coroutineName); if (!($connections instanceof PDO)) { @@ -169,7 +172,7 @@ class Connection extends Pool */ private function newClient($config, $coroutineName): PDO|null { - $connections = $this->createConnect($this->parseConfig($config, $coroutineName), $coroutineName, function ($cds, $username, $password, $charset, $coroutineName) { + return $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, @@ -190,12 +193,6 @@ class Connection extends Pool } return $link; }); - if ($connections === false) { - Coroutine::sleep(0.003); - return $this->getConnection($config, $coroutineName); - } - $this->printClients($config['cds'], $coroutineName, true); - return $connections; } diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index 8a12ae4b..ceec757d 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -47,11 +47,14 @@ class Redis extends Pool return Context::getContext($coroutineName); } if (!$this->hasItem($coroutineName)) { - return Context::setContext($coroutineName, $this->newClient($config, $coroutineName)); + if (($client = $this->newClient($config, $coroutineName)) == true) { + return Context::getContext($coroutineName); + } + return Context::setContext($coroutineName, $client); } [$time, $clients] = $this->get($coroutineName); if ($clients === null) { - return Context::setContext($coroutineName, $this->newClient($config, $coroutineName)); + throw new Exception('Redis exception.'); } return Context::setContext($coroutineName, $clients); } @@ -65,7 +68,8 @@ class Redis extends Pool */ private function newClient($config, $coroutineName): \Redis|null { - $client = $this->createConnect([$config, $coroutineName], $coroutineName, function ($config, $coroutineName) { + $this->printClients($config['host'], $coroutineName, true); + return $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'])); @@ -88,12 +92,6 @@ class Redis extends Pool return $redis; }); - if ($client === false) { - Coroutine::sleep(0.003); - return $this->getConnection($config, $coroutineName); - } - $this->printClients($config['host'], $coroutineName, true); - return $client; }