This commit is contained in:
2021-02-20 12:26:31 +08:00
parent 4b19b62f9f
commit c718c341ae
3 changed files with 8 additions and 14 deletions
+4 -12
View File
@@ -119,27 +119,19 @@ abstract class Pool extends Component
* @param array $config * @param array $config
* @param string $coroutineName * @param string $coroutineName
* @param callable $createHandler * @param callable $createHandler
* @return PDO|Redis|null
* @throws Exception * @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)) { if (Context::hasContext('create:connect:' . $coroutineName)) {
while ($client = Context::getContext($coroutineName)) { return;
Coroutine::sleep(0.001);
}
return $client;
} }
Context::setContext('create:connect:' . $coroutineName, 1); 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); Context::deleteId('create:connect:' . $coroutineName);
return Context::setContext($coroutineName, $client);
} }
+2 -1
View File
@@ -172,7 +172,7 @@ class Connection extends Pool
private function newClient($config, $coroutineName): PDO|null private function newClient($config, $coroutineName): PDO|null
{ {
$this->printClients($config['cds'], $coroutineName, true); $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, [ $link = new PDO($cds, $username, $password, [
PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_NATURAL, PDO::ATTR_CASE => PDO::CASE_NATURAL,
@@ -193,6 +193,7 @@ class Connection extends Pool
} }
return $link; return $link;
}); });
return $this->get($coroutineName)[1];
} }
+2 -1
View File
@@ -67,7 +67,7 @@ class Redis extends Pool
private function newClient($config, $coroutineName): \Redis|null private function newClient($config, $coroutineName): \Redis|null
{ {
$this->printClients($config['host'], $coroutineName, true); $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(); $redis = new SRedis();
if (!$redis->connect($config['host'], (int)$config['port'], $config['timeout'])) { if (!$redis->connect($config['host'], (int)$config['port'], $config['timeout'])) {
throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port'])); 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 $redis;
}); });
return $this->get($coroutineName)[1];
} }