From ab8a7d7fff8855f2e5f5807b833f9f073e45f5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 6 Jul 2021 15:38:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Pool/ClientsPool.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/System/Pool/ClientsPool.php b/System/Pool/ClientsPool.php index 38574890..b76d3603 100644 --- a/System/Pool/ClientsPool.php +++ b/System/Pool/ClientsPool.php @@ -29,6 +29,9 @@ class ClientsPool extends Component public int $creates = -1; + + private array $_times = []; + protected static array $hasCreate = []; @@ -91,28 +94,28 @@ class ClientsPool extends Component */ private function heartbeat_flush() { - $min = Config::get('databases.pool.min', 1); - $num = []; $total = 0; + $min = Config::get('databases.pool.min', 1); foreach (static::$_connections as $key => $channel) { if (!isset($num[$key])) { $num[$key] = 0; } - $length = $channel->length(); - if ($length > $min) { + if (time() - $this->_times[$key] ?? time() > 300) { + $this->flush($channel, 0); + } else if ($channel->length() > $min) { $this->flush($channel, $min); } - $num[$key] += $length; + $num[$key] += ($length = $channel->length()); if (str_starts_with($key, 'Mysql') && (Snowflake::isWorker() || Snowflake::isTask()) && $length > 0) { $this->debug('Worker #' . env('worker') . ' use client -> ' . $key . ':' . $length); } $total += $length; } - write(var_export($num,true),'connections'); + write(var_export($num, true), 'connections'); if ($total < 1) { Timer::clear($this->creates); - if (Snowflake::isWorker() || Snowflake::isTask()){ + if (Snowflake::isWorker() || Snowflake::isTask()) { $this->debug('Worker #' . env('worker') . ' clear time tick.'); } $this->creates = -1; @@ -197,6 +200,7 @@ class ClientsPool extends Component */ public function getFromChannel($name): mixed { + $this->_times[$name] = ''; $channel = $this->getChannel($name); if (!$channel->isEmpty()) { $connection = $channel->pop();