This commit is contained in:
2021-01-05 10:23:20 +08:00
parent 9ad16e7d70
commit 8a562264bb
3 changed files with 33 additions and 19 deletions
+10 -7
View File
@@ -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,
+14 -7
View File
@@ -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']));