From 8a562264bbcf742794b30dfda62bd5dc61003e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 5 Jan 2021 10:23:20 +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 | 14 +++++++++----- System/Pool/Connection.php | 17 ++++++++++------- System/Pool/Redis.php | 21 ++++++++++++++------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index d9e4677e..d1222ae6 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -7,6 +7,8 @@ namespace Snowflake\Abstracts; use Exception; use HttpServer\Http\Context; +use PDO; +use Redis; use Swoole\Coroutine; use Swoole\Coroutine\Channel; @@ -105,22 +107,24 @@ abstract class Pool extends Component * @param array $config * @param string $coroutineName * @param callable $createHandler + * @return PDO|Redis|null + * @throws Exception */ - public function createConnect(array $config, string $coroutineName, callable $createHandler): void + public function createConnect(array $config, string $coroutineName, callable $createHandler): PDO|Redis|null { if ($this->size($coroutineName) > 0) { - return; + return $this->get($coroutineName)[1]; } if (Context::hasContext('create:connect:' . $coroutineName)) { - return; + return $this->get($coroutineName)[1]; } Context::setContext('create:connect:' . $coroutineName, 1); $client = call_user_func($createHandler, ...$config); - $this->push($coroutineName, $client); - Context::deleteId('create:connect:' . $coroutineName); + + return $client; } diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index 79cfb83a..9a831db3 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -207,11 +207,12 @@ class Connection extends Pool return Context::getContext($coroutineName); } if ($this->size($coroutineName) < 1) { - $this->newClient($config, $coroutineName); - } - [$time, $connections] = $this->get($coroutineName); - if (!($connections instanceof PDO)) { - throw new Exception('Database exception.'); + $connections = $this->newClient($config, $coroutineName); + } else { + [$time, $connections] = $this->get($coroutineName); + if (!($connections instanceof PDO)) { + throw new Exception('Database exception.'); + } } return Context::setContext($coroutineName, $connections); } @@ -220,10 +221,12 @@ class Connection extends Pool /** * @param $config * @param $coroutineName + * @return PDO|null + * @throws Exception */ - private function newClient($config, $coroutineName) + private function newClient($config, $coroutineName): PDO|null { - $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, diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index b6a3258a..8c3a44d0 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -58,19 +58,26 @@ class Redis extends Pool public function getByChannel($coroutineName, $config): mixed { if (!$this->hasItem($coroutineName)) { - $this->newClient($config, $coroutineName); - } - [$time, $clients] = $this->get($coroutineName); - if ($clients === null) { - return $this->getByChannel($coroutineName, $config); + $clients = $this->newClient($config, $coroutineName); + } else { + [$time, $clients] = $this->get($coroutineName); + if ($clients === null) { + return $this->getByChannel($coroutineName, $config); + } } return $this->saveClient($coroutineName, $clients); } - private function newClient($config, $coroutineName) + /** + * @param $config + * @param $coroutineName + * @return SRedis|null + * @throws Exception + */ + private function newClient($config, $coroutineName): \Redis|null { - $this->createConnect([$config, $coroutineName], $coroutineName, function ($config, $coroutineName) { + 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']));