This commit is contained in:
2021-07-06 15:38:57 +08:00
parent 31a5053bed
commit ab8a7d7fff
+9 -5
View File
@@ -29,6 +29,9 @@ class ClientsPool extends Component
public int $creates = -1; public int $creates = -1;
private array $_times = [];
protected static array $hasCreate = []; protected static array $hasCreate = [];
@@ -91,19 +94,19 @@ class ClientsPool extends Component
*/ */
private function heartbeat_flush() private function heartbeat_flush()
{ {
$min = Config::get('databases.pool.min', 1);
$num = []; $num = [];
$total = 0; $total = 0;
$min = Config::get('databases.pool.min', 1);
foreach (static::$_connections as $key => $channel) { foreach (static::$_connections as $key => $channel) {
if (!isset($num[$key])) { if (!isset($num[$key])) {
$num[$key] = 0; $num[$key] = 0;
} }
$length = $channel->length(); if (time() - $this->_times[$key] ?? time() > 300) {
if ($length > $min) { $this->flush($channel, 0);
} else if ($channel->length() > $min) {
$this->flush($channel, $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) { if (str_starts_with($key, 'Mysql') && (Snowflake::isWorker() || Snowflake::isTask()) && $length > 0) {
$this->debug('Worker #' . env('worker') . ' use client -> ' . $key . ':' . $length); $this->debug('Worker #' . env('worker') . ' use client -> ' . $key . ':' . $length);
} }
@@ -197,6 +200,7 @@ class ClientsPool extends Component
*/ */
public function getFromChannel($name): mixed public function getFromChannel($name): mixed
{ {
$this->_times[$name] = '';
$channel = $this->getChannel($name); $channel = $this->getChannel($name);
if (!$channel->isEmpty()) { if (!$channel->isEmpty()) {
$connection = $channel->pop(); $connection = $channel->pop();