From ab32584cf2e09bb8d3bfc82fa7aecadde25ed3b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 5 Jul 2021 16:07:58 +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 | 39 ++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/System/Pool/ClientsPool.php b/System/Pool/ClientsPool.php index b06688b5..ee32db1c 100644 --- a/System/Pool/ClientsPool.php +++ b/System/Pool/ClientsPool.php @@ -82,34 +82,36 @@ class ClientsPool extends Component $min = Config::get('databases.pool.min', 1); $num = []; + $total = 0; foreach (static::$_connections as $key => $channel) { if (!isset($num[$key])) { $num[$key] = 0; } + if ($channel->length() > $min) { + $this->flush($channel, $min); + } $num[$key] += $channel->length(); + if (str_starts_with('Mysql:', $key)) { + $total += $channel->length(); + } } - var_dump($num); - -// $length = $this->getChannel($name)->length(); -// if ($length > $min) { -// $this->flush($min); -// } -// $this->debug("$name -> ($length:$min)"); + if ($total < 1) { + Timer::clear($this->creates); + $this->creates = -1; + } + $this->debug('use client ' . $total); } } /** + * @param $channel * @param $retain_number * @throws Exception */ - public function flush($retain_number) + public function flush($channel, $retain_number) { - $channels = $this->getChannels(); - foreach ($channels as $name => $channel) { - $names[] = $name; - $this->pop($channel, $name, $retain_number); - } + $this->pop($channel, $retain_number); static::$_connections = []; if ($retain_number == 0) { Timer::clear($this->creates); @@ -120,11 +122,10 @@ class ClientsPool extends Component /** * @param Channel $channel - * @param $name * @param $retain_number * @throws Exception */ - protected function pop(Channel $channel, $name, $retain_number): void + protected function pop(Channel $channel, $retain_number): void { if (Coroutine::getCid() === -1) { return; @@ -134,7 +135,6 @@ class ClientsPool extends Component if ($connection) { unset($connection); } - $this->decrement($name); } } @@ -292,12 +292,7 @@ class ClientsPool extends Component return; } $channel = static::$_connections[$name]; - $this->pop($channel, $name, 0); - if ($this->creates > -1) { - Timer::clear($this->creates); - } - $channel->close(); - static::$_connections[$name] = null; + $this->pop($channel, 0); }