This commit is contained in:
2021-01-04 18:40:11 +08:00
parent b4f6d1be35
commit 0c7a777ab4
3 changed files with 148 additions and 98 deletions
+58 -35
View File
@@ -58,7 +58,7 @@ class Redis extends Pool
public function getByChannel($coroutineName, $config): mixed
{
if ($this->_create < $this->max && !$this->hasItem($coroutineName)) {
$this->createConnect($config, $coroutineName);
$this->newClient($config, $coroutineName);
}
[$time, $clients] = $this->get($coroutineName);
if ($clients === null) {
@@ -68,6 +68,29 @@ class Redis extends Pool
}
private function newClient($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']));
}
if (empty($config['auth']) || !$redis->auth($config['auth'])) {
throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $config['host'], $config['auth']));
}
$this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName));
if (!isset($config['read_timeout'])) {
$config['read_timeout'] = 10;
}
$redis->select($config['databases']);
$redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']);
$redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
return $redis;
});
}
/**
* @param $coroutineName
* @param $client
@@ -85,40 +108,40 @@ class Redis extends Pool
* @param string $coroutineName
* @throws Exception
*/
private function createConnect(array $config, string $coroutineName): void
{
try {
if ($this->_create >= $this->max) {
return;
}
$this->_create += 1;
if (Context::hasContext('at_create_db')) {
return;
}
Context::setContext('at_create_db', 1);
$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']));
}
if (empty($config['auth']) || !$redis->auth($config['auth'])) {
throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $config['host'], $config['auth']));
}
$this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName));
if (!isset($config['read_timeout'])) {
$config['read_timeout'] = 10;
}
$redis->select($config['databases']);
$redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']);
$redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
$this->push($coroutineName, $redis);
} catch (\Throwable $exception) {
$this->addError($exception->getMessage());
} finally {
Context::deleteId('at_create_db');
}
}
// private function createConnect(array $config, string $coroutineName): void
// {
// try {
// if ($this->_create >= $this->max) {
// return;
// }
// $this->_create += 1;
// if (Context::hasContext('at_create_db')) {
// return;
// }
// Context::setContext('at_create_db', 1);
// $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']));
// }
// if (empty($config['auth']) || !$redis->auth($config['auth'])) {
// throw new RedisConnectException(sprintf('Redis Error: %s, Host %s, Auth %s', $redis->getLastError(), $config['host'], $config['auth']));
// }
// $this->success('create redis client -> ' . $config['host'] . ':' . $this->size($coroutineName));
// if (!isset($config['read_timeout'])) {
// $config['read_timeout'] = 10;
// }
// $redis->select($config['databases']);
// $redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']);
// $redis->setOption(SRedis::OPT_PREFIX, $config['prefix']);
//
// $this->push($coroutineName, $redis);
// } catch (\Throwable $exception) {
// $this->addError($exception->getMessage());
// } finally {
// Context::deleteId('at_create_db');
// }
//
// }
/**
* @param array $config