diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 3b09ec21..a7db273a 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -12,6 +12,7 @@ use PDO; use Redis; use Swoole\Coroutine; use Swoole\Coroutine\Channel; +use Swoole\Timer; /** * Class Pool @@ -205,6 +206,64 @@ abstract class Pool extends Component } } + private int $creates = 0; + + + public int $lastTime = 0; + + + /** + * @param $timer + * @throws Exception + */ + public function Heartbeat_detection($timer) + { + $this->creates = $timer; + if ($this->lastTime == 0) { + return; + } + if ($this->lastTime + 60 < time()) { + $this->flush(0); + } else if ($this->lastTime + 30 < time()) { + $this->flush(2); + } + } + + + /** + * @param $retain_number + * @throws Exception + */ + protected function flush($retain_number) + { + $channels = $this->getChannels(); + foreach ($channels as $name => $channel) { + $this->pop($channel, $name, $retain_number); + } + if ($retain_number == 0) { + $this->debug('release Timer::tick'); + Timer::clear($this->creates); + $this->creates = 0; + } + } + + + /** + * @param $channel + * @param $name + * @param $retain_number + * @throws Exception + */ + protected function pop($channel, $name, $retain_number) + { + while ($channel->length() > $retain_number) { + [$timer, $connection] = $channel->pop(); + if ($connection) { + unset($connection); + } + $this->desc($name); + } + } /** * @return Channel[] diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index 2c7e336b..3985c3ef 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -26,61 +26,6 @@ class Connection extends Pool protected array $connections = []; - private int $creates = 0; - - - public int $lastTime = 0; - - /** - * @param $timer - */ - public function Heartbeat_detection($timer) - { - $this->creates = $timer; - if ($this->lastTime == 0) { - return; - } - if ($this->lastTime + 600 < time()) { - $this->flush(0); - } else if ($this->lastTime + 300 < time()) { - $this->flush(2); - } - } - - - /** - * @param $retain_number - */ - protected function flush($retain_number) - { - $channels = $this->getChannels(); - foreach ($channels as $name => $channel) { - $this->pop($channel, $name, $retain_number); - } - if ($retain_number == 0) { - $this->debug('release Timer::tick'); - Timer::clear($this->creates); - $this->creates = 0; - } - } - - - /** - * @param $channel - * @param $name - * @param $retain_number - */ - protected function pop($channel, $name, $retain_number) - { - while ($channel->length() > $retain_number) { - [$timer, $connection] = $channel->pop(); - if ($connection) { - unset($connection); - } - $this->desc($name); - } - } - /** * @param $timeout @@ -244,7 +189,7 @@ class Connection extends Pool $number > 0 && $link->beginTransaction(); } if ($this->creates === 0) { - $this->creates = Timer::tick(10000, [$this, 'Heartbeat_detection']); + $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); } return $link; }); diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index 88985d34..ec672bbc 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -11,6 +11,7 @@ use RedisException; use Snowflake\Exception\RedisConnectException; use Swoole\Coroutine; use Exception; +use Swoole\Timer; /** * Class RedisClient @@ -23,6 +24,9 @@ class Redis extends Pool private int $_create = 0; + private int $creates = 0; + + /** * @param $value */ @@ -80,6 +84,10 @@ class Redis extends Pool $redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']); $redis->setOption(SRedis::OPT_PREFIX, $config['prefix']); + if ($this->creates === 0) { + $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); + } + $this->_create += 1; return $redis;