This commit is contained in:
2021-02-15 20:45:35 +08:00
parent b862e828fc
commit 74604324b1
3 changed files with 22 additions and 31 deletions
+2 -5
View File
@@ -124,13 +124,10 @@ abstract class Pool extends Component
* @return PDO|Redis|null * @return PDO|Redis|null
* @throws Exception * @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)) { if (Context::hasContext('create:connect:' . $coroutineName)) {
return null; return false;
} }
Context::setContext('create:connect:' . $coroutineName, 1); Context::setContext('create:connect:' . $coroutineName, 1);
+11 -9
View File
@@ -26,7 +26,6 @@ class Connection extends Pool
protected array $connections = []; protected array $connections = [];
/** /**
* @param $timeout * @param $timeout
*/ */
@@ -151,13 +150,12 @@ class Connection extends Pool
if (Context::hasContext($coroutineName)) { if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName); return Context::getContext($coroutineName);
} }
if ($this->size($coroutineName) < 1) { if (!$this->hasItem($coroutineName)) {
$connections = $this->newClient($config, $coroutineName); return Context::setContext($coroutineName, $this->newClient($config, $coroutineName));
} else { }
[$time, $connections] = $this->get($coroutineName); [$time, $connections] = $this->get($coroutineName);
if (!($connections instanceof PDO)) { if (!($connections instanceof PDO)) {
throw new Exception('Database exception.'); throw new Exception('Database exception.');
}
} }
return Context::setContext($coroutineName, $connections); return Context::setContext($coroutineName, $connections);
} }
@@ -172,7 +170,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) { $connections = $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 +191,10 @@ class Connection extends Pool
} }
return $link; return $link;
}); });
if ($connections === false) {
return $this->newClient($config, $coroutineName);
}
return $connections;
} }
+9 -17
View File
@@ -47,13 +47,13 @@ class Redis extends Pool
return Context::getContext($coroutineName); return Context::getContext($coroutineName);
} }
if (!$this->hasItem($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); [$time, $clients] = $this->get($coroutineName);
if ($clients === null) { 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 private function newClient($config, $coroutineName): \Redis|null
{ {
$this->printClients($config['host'], $coroutineName, true); $client = $this->createConnect([$config, $coroutineName], $coroutineName, function ($config, $coroutineName) {
return $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']));
@@ -89,6 +88,11 @@ class Redis extends Pool
return $redis; 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 array $config
* @param bool $isMaster * @param bool $isMaster