diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 1bfdf367..f992ceb2 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -119,27 +119,19 @@ 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): PDO|Redis|false + public function createConnect(array $config, string $coroutineName, callable $createHandler) { - if ($client = Context::getContext($coroutineName)) { - return $client; - } if (Context::hasContext('create:connect:' . $coroutineName)) { - while ($client = Context::getContext($coroutineName)) { - Coroutine::sleep(0.001); - } - return $client; + return; } + Context::setContext('create:connect:' . $coroutineName, 1); - $client = call_user_func($createHandler, ...$config); + $this->push($coroutineName, call_user_func($createHandler, ...$config)); Context::deleteId('create:connect:' . $coroutineName); - - return Context::setContext($coroutineName, $client); } diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index 7b192afb..15f95942 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -172,7 +172,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) { + $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 +193,7 @@ class Connection extends Pool } return $link; }); + return $this->get($coroutineName)[1]; } diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index ed816359..d8049319 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -67,7 +67,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) { + $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'])); @@ -90,6 +90,7 @@ class Redis extends Pool return $redis; }); + return $this->get($coroutineName)[1]; }