Files
kiri-core/System/Pool/Redis.php
T

182 lines
4.2 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Pool;
use HttpServer\Http\Context;
2020-09-03 15:23:11 +08:00
use Redis as SRedis;
2020-08-31 01:27:08 +08:00
use RedisException;
2020-09-03 18:05:54 +08:00
use Snowflake\Exception\RedisConnectException;
2020-08-31 01:27:08 +08:00
use Swoole\Coroutine;
use Exception;
2021-02-15 20:31:01 +08:00
use Swoole\Timer;
2020-08-31 01:27:08 +08:00
/**
* Class RedisClient
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Pool
2020-08-31 01:27:08 +08:00
*/
2020-09-03 15:23:11 +08:00
class Redis extends Pool
2020-08-31 01:27:08 +08:00
{
2021-01-04 16:53:29 +08:00
2021-02-17 00:51:21 +08:00
private int $_create = 0;
2021-01-04 16:53:29 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param $value
*/
public function setLength($value)
{
$this->max = $value;
}
/**
* @param array $config
* @param bool $isMaster
2020-12-17 14:09:14 +08:00
* @return mixed
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function getConnection(array $config, $isMaster = false): mixed
2020-08-31 01:27:08 +08:00
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
2021-02-20 13:35:17 +08:00
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
2021-02-20 12:30:35 +08:00
if (($redis = Context::getContext($coroutineName)) instanceof \Redis) {
return $redis;
2020-08-31 01:27:08 +08:00
}
2021-01-04 18:54:27 +08:00
if (!$this->hasItem($coroutineName)) {
2021-02-20 12:29:12 +08:00
$this->newClient($config, $coroutineName);
2021-02-03 18:35:52 +08:00
}
[$time, $clients] = $this->get($coroutineName);
if ($clients === null) {
2021-02-20 12:29:12 +08:00
return $this->getConnection($config, $coroutineName);
2020-08-31 01:27:08 +08:00
}
2021-02-15 20:45:35 +08:00
return Context::setContext($coroutineName, $clients);
2020-08-31 01:27:08 +08:00
}
2021-01-05 10:23:20 +08:00
/**
* @param $config
* @param $coroutineName
* @throws Exception
*/
2021-02-20 12:31:20 +08:00
private function newClient($config, $coroutineName)
2020-08-31 01:27:08 +08:00
{
2021-02-16 23:42:13 +08:00
$this->printClients($config['host'], $coroutineName, true);
2021-02-20 12:26:31 +08:00
$this->createConnect([$config, $coroutineName], $coroutineName, function ($config, $coroutineName) {
2021-01-04 16:53:29 +08:00
$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']));
}
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']);
2021-01-06 15:36:08 +08:00
$this->_create += 1;
2021-01-04 18:40:11 +08:00
return $redis;
});
}
2021-01-04 16:53:29 +08:00
2021-01-05 18:30:58 +08:00
/**
* @param $cds
* @param $coroutineName
* @param false $isBefore
*/
public function printClients($cds, $coroutineName, $isBefore = false)
{
2021-02-14 21:48:37 +08:00
$this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->_create . ']');
2021-01-05 18:30:58 +08:00
}
2020-08-31 01:27:08 +08:00
/**
* @param array $config
* @param bool $isMaster
*/
public function release(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
2021-02-20 13:35:17 +08:00
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
2020-08-31 01:27:08 +08:00
if (!Context::hasContext($coroutineName)) {
return;
}
2020-12-21 17:22:18 +08:00
$this->push($coroutineName, Context::getContext($coroutineName));
2020-12-21 17:32:44 +08:00
$this->remove($coroutineName);
2021-02-20 13:43:46 +08:00
$this->lastTime = time();
2020-08-31 01:27:08 +08:00
}
/**
* @param array $config
* @param bool $isMaster
2021-01-05 16:33:19 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
public function destroy(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
2021-02-20 13:35:17 +08:00
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
2020-08-31 01:27:08 +08:00
if (!Context::hasContext($coroutineName)) {
return;
}
2021-01-04 16:53:29 +08:00
2021-01-05 16:32:48 +08:00
$this->desc($coroutineName);
2021-01-04 16:53:29 +08:00
2020-08-31 01:27:08 +08:00
$this->remove($coroutineName);
$this->clean($coroutineName);
}
/**
* @param $coroutineName
*/
2020-12-17 14:09:14 +08:00
public function remove(string $coroutineName)
2020-08-31 01:27:08 +08:00
{
Context::deleteId($coroutineName);
}
/**
2020-09-04 19:10:08 +08:00
* @param $name
2020-08-31 01:27:08 +08:00
* @param $time
* @param $client
2020-12-17 14:09:14 +08:00
* @return bool
2020-09-04 19:10:08 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function checkCanUse(string $name, int $time, mixed $client): bool
2020-08-31 01:27:08 +08:00
{
2020-09-04 19:10:08 +08:00
try {
2021-02-20 11:33:52 +08:00
if (!($client instanceof SRedis)) {
2021-02-03 18:49:16 +08:00
$result = false;
} else if (!$client->isConnected() || !$client->ping('connect.')) {
$result = false;
} else {
$result = true;
2020-09-04 19:10:08 +08:00
}
2020-11-06 16:47:17 +08:00
} catch (\Throwable $exception) {
2021-02-20 15:45:48 +08:00
$this->error($exception);
2021-02-03 18:49:16 +08:00
$result = false;
2020-09-04 19:10:08 +08:00
} finally {
if (!$result) {
$this->desc($name);
}
2021-02-03 18:49:16 +08:00
return $result;
2020-08-31 01:27:08 +08:00
}
}
2021-01-04 16:53:29 +08:00
/**
* @param string $name
*/
2020-12-17 14:09:14 +08:00
public function desc(string $name)
2020-08-31 01:27:08 +08:00
{
2021-01-04 16:53:29 +08:00
$this->_create -= 1;
2020-08-31 01:27:08 +08:00
}
}