From a67171989662f186221851fd7347c14c26ede4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 5 Jul 2021 10:37:29 +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 --- Database/Connection.php | 79 ++++++++++++++++++++++----------------- System/Abstracts/Pool.php | 47 +++++++++++++++-------- 2 files changed, 75 insertions(+), 51 deletions(-) diff --git a/Database/Connection.php b/Database/Connection.php index 5ff33460..ffa40df0 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -17,6 +17,7 @@ use JetBrains\PhpStorm\Pure; use PDO; use ReflectionException; use Snowflake\Abstracts\Component; +use Snowflake\Abstracts\Config; use Snowflake\Event; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -106,9 +107,11 @@ class Connection extends Component public function fill() { $connections = $this->connections(); - $connections->initConnections('mysql', $this->cds, true, $this->maxNumber); - if (!empty($this->slaveConfig)) { - $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); + $pool = Config::get('databases.pool.max',10); + + $connections->initConnections('mysql', $this->cds, true, $pool); + if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) { + $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $pool); } } @@ -257,13 +260,14 @@ class Connection extends Component return $instance; } - /** - * @param $sql - * @param array $attributes - * @return Command - * @throws - */ - public function createCommand($sql = null, $dbname = '', array $attributes = []): Command + /** + * @param null $sql + * @param string $dbname + * @param array $attributes + * @return Command + * @throws Exception + */ + public function createCommand($sql = null, string $dbname = '', array $attributes = []): Command { if (!empty($dbname)) { $sql = $this->clear($dbname, $sql); @@ -273,7 +277,12 @@ class Connection extends Component } - private function clear($dbname, $sql) + /** + * @param $dbname + * @param $sql + * @return array|string|null + */ + private function clear($dbname, $sql): array|string|null { $substr = strtoupper(substr($sql, 0, 6)); return match ($substr) { @@ -297,45 +306,45 @@ class Connection extends Component } - /** - * @param $dbname - * @param $sql - * @return array|string|string[]|null - */ - private function innerJoinMatch($dbname, $sql) + /** + * @param $dbname + * @param $sql + * @return array|string|null + */ + private function innerJoinMatch($dbname, $sql): array|string|null { return preg_replace('/(INNER|LEFT|RIGHT) JOIN\s+(\w+)\s/', '$1 JOIN `' . $dbname . '`.$2 ', $sql); } - /** - * @param $dbname - * @param $sql - * @return array|string|string[]|null - */ - private function updateMatch($dbname, $sql) + /** + * @param $dbname + * @param $sql + * @return array|string|null + */ + private function updateMatch($dbname, $sql): array|string|null { return preg_replace('/UPDATE\s+(\w+)\s+SET/', 'UPDATE `' . $dbname . '`.$1 SET', $sql); } - /** - * @param $dbname - * @param $sql - * @return array|string|string[]|null - */ - private function insertMatch($dbname, $sql) + /** + * @param $dbname + * @param $sql + * @return array|string|null + */ + private function insertMatch($dbname, $sql): array|string|null { return preg_replace('/INSERT INTO\s+(\w+)/', 'INSERT INTO `' . $dbname . '`.$1', $sql); } - /** - * @param $dbname - * @param $sql - * @return array|string|string[]|null - */ - private function truncateMatch($dbname, $sql) + /** + * @param $dbname + * @param $sql + * @return array|string|null + */ + private function truncateMatch($dbname, $sql): array|string|null { return preg_replace('/TRUNCATE\s+(\w+)\s+/', 'TRUNCATE `' . $dbname . '`.$1', $sql); } diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 0b6ffd32..29b2d690 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -73,17 +73,16 @@ abstract class Pool extends Component /** * @throws Exception */ - public function Heartbeat_detection() + public function Heartbeat_detection(string $name) { if (env('state') == 'exit') { Timer::clear($this->creates); $this->creates = -1; - } else if ($this->lastTime != 0) { - [$firstClear, $lastClear] = $this->getClearTime(); - if ($this->lastTime + $firstClear < time()) { - $this->flush(0); - } else if ($this->lastTime + $lastClear < time()) { - $this->flush(2); + } else { + $min = Config::get('databases.pool.min', 1); + $channel = $this->getChannel($name); + if ($channel->length() > $min) { + $this->flush($min); } } } @@ -147,7 +146,7 @@ abstract class Pool extends Component * @param false $isMaster * @param int $max */ - public function initConnections($driver, $name, $isMaster = false, $max = 60) + public function initConnections($driver, $name, bool $isMaster = false, int $max = 60) { $name = $this->name($driver, $name, $isMaster); if (isset(static::$_items[$name]) && static::$_items[$name] instanceof Channel) { @@ -156,8 +155,23 @@ abstract class Pool extends Component if (Coroutine::getCid() === -1) { return; } - static::$_items[$name] = new Channel((int)$max); - $this->max = (int)$max; + static::$_items[$name] = new Channel($max); + $this->max = $max; + } + + + /** + * @param $name + * @return Channel + * @throws ConfigException + */ + private function getChannel($name): Channel + { + $channel = static::$_items[$name] ?? new Channel(Config::get('databases.pool.max', 10)); + if (!((static::$_items[$name] ?? null) instanceof Channel)) { + static::$_items[$name] = $channel; + } + return $channel; } @@ -172,16 +186,17 @@ abstract class Pool extends Component if (Coroutine::getCid() === -1) { return $this->createClient($name, $callback); } - $channel = static::$_items[$name] ?? new Channel($this->max); - if (!((static::$_items[$name] ?? null) instanceof Channel)) { - static::$_items[$name] = $channel; - } + + $channel = $this->getChannel($name); if (!$channel->isEmpty()) { $connection = $channel->pop(); if ($this->checkCanUse($name, $connection)) { return $connection; } } + if ($this->creates === -1 && !is_callable($callback)) { + $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection'], $name); + } return $this->createClient($name, $callback); } @@ -221,7 +236,7 @@ abstract class Pool extends Component /** * @param string $name - * @param $client + * @param mixed $client * @return bool * 检查连接可靠性 */ @@ -233,7 +248,7 @@ abstract class Pool extends Component /** * @param array $config - * @param $isMaster + * @param bool $isMaster * @return mixed * @throws Exception */