From 1a012150ceb70c015e2746e83357c363179dd565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 5 Jul 2021 15:49:37 +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 | 568 ++++++++++++++++++------------------ System/Cache/Redis.php | 3 +- System/Pool/ClientsPool.php | 15 +- System/Pool/Connection.php | 22 +- System/Pool/Redis.php | 12 + 5 files changed, 317 insertions(+), 303 deletions(-) diff --git a/Database/Connection.php b/Database/Connection.php index ffa40df0..adca8e8f 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -28,237 +28,237 @@ use Snowflake\Snowflake; */ class Connection extends Component { - const TRANSACTION_COMMIT = 'transaction::commit'; - const TRANSACTION_ROLLBACK = 'transaction::rollback'; + const TRANSACTION_COMMIT = 'transaction::commit'; + const TRANSACTION_ROLLBACK = 'transaction::rollback'; - public string $id = 'db'; - public string $cds = ''; - public string $password = ''; - public string $username = ''; - public string $charset = 'utf-8'; - public string $tablePrefix = ''; + public string $id = 'db'; + public string $cds = ''; + public string $password = ''; + public string $username = ''; + public string $charset = 'utf-8'; + public string $tablePrefix = ''; - public string $database = ''; + public string $database = ''; - public int $timeout = 1900; + public int $timeout = 1900; - public int $maxNumber = 30; - public int $minNumber = 10; + public int $maxNumber = 30; + public int $minNumber = 10; - /** - * @var bool - * enable database cache - */ - public bool $enableCache = false; - public string $cacheDriver = 'redis'; + /** + * @var bool + * enable database cache + */ + public bool $enableCache = false; + public string $cacheDriver = 'redis'; - /** - * @var array - * - * @example [ - * 'cds' => 'mysql:dbname=dbname;host=127.0.0.1', - * 'username' => 'root', - * 'password' => 'root' - * ] - */ - public array $slaveConfig = []; + /** + * @var array + * + * @example [ + * 'cds' => 'mysql:dbname=dbname;host=127.0.0.1', + * 'username' => 'root', + * 'password' => 'root' + * ] + */ + public array $slaveConfig = []; - private ?Schema $_schema = null; + private ?Schema $_schema = null; - /** - * @throws Exception - */ - public function init() - { - Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); - Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); - } + /** + * @throws Exception + */ + public function init() + { + Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']); + Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']); + } - /** - * @throws Exception - */ - public function enablingTransactions() - { - if (!Db::transactionsActive()) { - return; - } - $this->beginTransaction(); + /** + * @throws Exception + */ + public function enablingTransactions() + { + if (!Db::transactionsActive()) { + return; + } + $this->beginTransaction(); - Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); - Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); - } + Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true); + Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true); + } - /** - * @param null $sql - * @return PDO - * @throws Exception - */ - public function getConnect($sql = NULL): PDO - { - return $this->getPdo($sql); - } + /** + * @param null $sql + * @return PDO + * @throws Exception + */ + public function getConnect($sql = NULL): PDO + { + return $this->getPdo($sql); + } - /** - * @throws Exception - */ - public function fill() - { - $connections = $this->connections(); - $pool = Config::get('databases.pool.max',10); + /** + * @throws Exception + */ + public function fill() + { + $connections = $this->connections(); + $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); - } - } + $connections->initConnections($this->cds, true, $pool); + if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) { + $connections->initConnections($this->slaveConfig['cds'], false, $pool); + } + } - /** - * @param $sql - * @return PDO - * @throws Exception - */ - private function getPdo($sql): PDO - { - if ($this->isWrite($sql)) { - return $this->masterInstance(); - } else { - return $this->slaveInstance(); - } - } + /** + * @param $sql + * @return PDO + * @throws Exception + */ + private function getPdo($sql): PDO + { + if ($this->isWrite($sql)) { + return $this->masterInstance(); + } else { + return $this->slaveInstance(); + } + } - /** - * @return mixed - * @throws ReflectionException - * @throws NotFindClassException - */ - public function getSchema(): Schema - { - if ($this->_schema === null) { - $this->_schema = Snowflake::createObject([ - 'class' => Schema::class, - 'db' => $this - ]); - } - return $this->_schema; - } + /** + * @return mixed + * @throws ReflectionException + * @throws NotFindClassException + */ + public function getSchema(): Schema + { + if ($this->_schema === null) { + $this->_schema = Snowflake::createObject([ + 'class' => Schema::class, + 'db' => $this + ]); + } + return $this->_schema; + } - /** - * @param $sql - * @return bool - */ - #[Pure] public function isWrite($sql): bool - { - if (empty($sql)) return false; - if (str_starts_with(strtolower($sql), 'select')) { - return false; - } - return true; - } + /** + * @param $sql + * @return bool + */ + #[Pure] public function isWrite($sql): bool + { + if (empty($sql)) return false; + if (str_starts_with(strtolower($sql), 'select')) { + return false; + } + return true; + } - /** - * @return mixed - * @throws Exception - */ - public function getCacheDriver(): mixed - { - if (!$this->enableCache) { - return null; - } - return Snowflake::app()->get($this->cacheDriver); - } + /** + * @return mixed + * @throws Exception + */ + public function getCacheDriver(): mixed + { + if (!$this->enableCache) { + return null; + } + return Snowflake::app()->get($this->cacheDriver); + } - /** - * @return PDO - * @throws Exception - */ - public function masterInstance(): PDO - { - return $this->connections()->get([ - 'cds' => $this->cds, - 'username' => $this->username, - 'password' => $this->password, - 'database' => $this->database - ], true); - } + /** + * @return PDO + * @throws Exception + */ + public function masterInstance(): PDO + { + return $this->connections()->get([ + 'cds' => $this->cds, + 'username' => $this->username, + 'password' => $this->password, + 'database' => $this->database + ], true); + } - /** - * @return PDO - * @throws Exception - */ - public function slaveInstance(): PDO - { - if (empty($this->slaveConfig) || Db::transactionsActive()) { - return $this->masterInstance(); - } - return $this->connections()->get($this->slaveConfig, false); - } + /** + * @return PDO + * @throws Exception + */ + public function slaveInstance(): PDO + { + if (empty($this->slaveConfig) || Db::transactionsActive()) { + return $this->masterInstance(); + } + return $this->connections()->get($this->slaveConfig, false); + } - /** - * @return \Snowflake\Pool\Connection - * @throws Exception - */ - private function connections(): \Snowflake\Pool\Connection - { - return Snowflake::app()->getMysqlFromPool(); - } + /** + * @return \Snowflake\Pool\Connection + * @throws Exception + */ + private function connections(): \Snowflake\Pool\Connection + { + return Snowflake::app()->getMysqlFromPool(); + } - /** - * @return $this - * @throws Exception - */ - public function beginTransaction(): static - { - $this->connections()->beginTransaction($this->cds); - return $this; - } + /** + * @return $this + * @throws Exception + */ + public function beginTransaction(): static + { + $this->connections()->beginTransaction($this->cds); + return $this; + } - /** - * @return $this|bool - * @throws Exception - */ - public function inTransaction(): bool|static - { - return $this->connections()->inTransaction($this->cds); - } + /** + * @return $this|bool + * @throws Exception + */ + public function inTransaction(): bool|static + { + return $this->connections()->inTransaction($this->cds); + } - /** - * @throws Exception - * 事务回滚 - */ - public function rollback() - { - $this->connections()->rollback($this->cds); - } + /** + * @throws Exception + * 事务回滚 + */ + public function rollback() + { + $this->connections()->rollback($this->cds); + } - /** - * @throws Exception - * 事务提交 - */ - public function commit() - { - $this->connections()->commit($this->cds); - } + /** + * @throws Exception + * 事务提交 + */ + public function commit() + { + $this->connections()->commit($this->cds); + } - /** - * @param $sql - * @return PDO - * @throws Exception - */ - public function refresh($sql): PDO - { - if ($this->isWrite($sql)) { - $instance = $this->masterInstance(); - } else { - $instance = $this->slaveInstance(); - } - return $instance; - } + /** + * @param $sql + * @return PDO + * @throws Exception + */ + public function refresh($sql): PDO + { + if ($this->isWrite($sql)) { + $instance = $this->masterInstance(); + } else { + $instance = $this->slaveInstance(); + } + return $instance; + } /** * @param null $sql @@ -267,14 +267,14 @@ class Connection extends Component * @return Command * @throws Exception */ - public function createCommand($sql = null, string $dbname = '', array $attributes = []): Command - { - if (!empty($dbname)) { - $sql = $this->clear($dbname, $sql); - } - $command = new Command(['db' => $this, 'sql' => $this->innerJoinMatch($dbname, $sql)]); - return $command->bindValues($attributes); - } + public function createCommand($sql = null, string $dbname = '', array $attributes = []): Command + { + if (!empty($dbname)) { + $sql = $this->clear($dbname, $sql); + } + $command = new Command(['db' => $this, 'sql' => $this->innerJoinMatch($dbname, $sql)]); + return $command->bindValues($attributes); + } /** @@ -282,28 +282,28 @@ class Connection extends Component * @param $sql * @return array|string|null */ - private function clear($dbname, $sql): array|string|null - { - $substr = strtoupper(substr($sql, 0, 6)); - return match ($substr) { - 'SELECT', 'SHOW F', 'DELETE' => $this->selectMatch($dbname, $sql), - 'UPDATE' => $this->updateMatch($dbname, $sql), - 'INSERT' => $this->insertMatch($dbname, $sql), - 'TRUNCA' => $this->truncateMatch($dbname, $sql), - default => $sql - }; - } + private function clear($dbname, $sql): array|string|null + { + $substr = strtoupper(substr($sql, 0, 6)); + return match ($substr) { + 'SELECT', 'SHOW F', 'DELETE' => $this->selectMatch($dbname, $sql), + 'UPDATE' => $this->updateMatch($dbname, $sql), + 'INSERT' => $this->insertMatch($dbname, $sql), + 'TRUNCA' => $this->truncateMatch($dbname, $sql), + default => $sql + }; + } - /** - * @param $dbname - * @param $sql - * @return array|string|string[]|null - */ - private function selectMatch($dbname, $sql) - { - return preg_replace('/FROM\s+(\w+)\s+/', 'FROM `' . $dbname . '`.$1 ', $sql); - } + /** + * @param $dbname + * @param $sql + * @return array|string|string[]|null + */ + private function selectMatch($dbname, $sql) + { + return preg_replace('/FROM\s+(\w+)\s+/', 'FROM `' . $dbname . '`.$1 ', $sql); + } /** @@ -311,10 +311,10 @@ class Connection extends Component * @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); - } + private function innerJoinMatch($dbname, $sql): array|string|null + { + return preg_replace('/(INNER|LEFT|RIGHT) JOIN\s+(\w+)\s/', '$1 JOIN `' . $dbname . '`.$2 ', $sql); + } /** @@ -322,10 +322,10 @@ class Connection extends Component * @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); - } + private function updateMatch($dbname, $sql): array|string|null + { + return preg_replace('/UPDATE\s+(\w+)\s+SET/', 'UPDATE `' . $dbname . '`.$1 SET', $sql); + } /** @@ -333,10 +333,10 @@ class Connection extends Component * @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); - } + private function insertMatch($dbname, $sql): array|string|null + { + return preg_replace('/INSERT INTO\s+(\w+)/', 'INSERT INTO `' . $dbname . '`.$1', $sql); + } /** @@ -344,59 +344,59 @@ class Connection extends Component * @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); - } + private function truncateMatch($dbname, $sql): array|string|null + { + return preg_replace('/TRUNCATE\s+(\w+)\s+/', 'TRUNCATE `' . $dbname . '`.$1', $sql); + } - /** - * - * 回收链接 - * @throws - */ - public function release() - { - $connections = $this->connections(); + /** + * + * 回收链接 + * @throws + */ + public function release() + { + $connections = $this->connections(); - $connections->release($this->cds, true); - $connections->release($this->slaveConfig['cds'], false); - } + $connections->release($this->cds, true); + $connections->release($this->slaveConfig['cds'], false); + } - /** - * @throws Exception - */ - public function recovery() - { - $connections = $this->connections(); + /** + * @throws Exception + */ + public function recovery() + { + $connections = $this->connections(); - $connections->release($this->cds, true); - $connections->release($this->slaveConfig['cds'], false); - } + $connections->release($this->cds, true); + $connections->release($this->slaveConfig['cds'], false); + } - /** - * - * 回收链接 - * @throws - */ - public function clear_connection() - { - $connections = $this->connections(); + /** + * + * 回收链接 + * @throws + */ + public function clear_connection() + { + $connections = $this->connections(); - $connections->release($this->cds, true); - $connections->release($this->slaveConfig['cds'], false); - } + $connections->release($this->cds, true); + $connections->release($this->slaveConfig['cds'], false); + } - /** - * @throws Exception - */ - public function disconnect() - { - $connections = $this->connections(); - $connections->disconnect($this->cds, true); - $connections->disconnect($this->slaveConfig['cds'], false); - } + /** + * @throws Exception + */ + public function disconnect() + { + $connections = $this->connections(); + $connections->disconnect($this->cds, true); + $connections->disconnect($this->slaveConfig['cds'], false); + } } diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index f6753d4e..bf888b8f 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -53,11 +53,10 @@ class Redis extends Component $connections = Snowflake::app()->getRedisFromPool(); $config = $this->get_config(); - $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $length = (int)env('REDIS.POOL_LENGTH', 100); - $connections->initConnections('redis', $name, true, $length); + $connections->initConnections('Redis:' . $config['host'], true, $length); } diff --git a/System/Pool/ClientsPool.php b/System/Pool/ClientsPool.php index 70915b05..834cccc4 100644 --- a/System/Pool/ClientsPool.php +++ b/System/Pool/ClientsPool.php @@ -106,18 +106,6 @@ class ClientsPool extends Component } - /** - * @param $name - */ - protected function clearCreateLog($name): void - { - if (!isset(static::$hasCreate[$name])) { - return; - } - static::$hasCreate[$name] = 0; - } - - /** * @param Channel $channel * @param $name @@ -140,12 +128,11 @@ class ClientsPool extends Component /** - * @param $driver * @param $name * @param false $isMaster * @param int $max */ - public function initConnections($driver, $name, bool $isMaster = false, int $max = 60) + public function initConnections($name, bool $isMaster = false, int $max = 60) { $name = $this->name($name, $isMaster); if (isset(static::$_connections[$name]) && static::$_connections[$name] instanceof Channel) { diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index 859e394f..06f4c8cf 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -113,10 +113,14 @@ class Connection extends Component if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { return $pdo; } - /** @var PDO $connections */ - $connections = $this->getPool()->getFromChannel($coroutineName); - if (empty($connections)) { + if (Coroutine::getCid() === -1) { $connections = $this->createClient($coroutineName, $config); + } else { + /** @var PDO $connections */ + $connections = $this->getPool()->getFromChannel($coroutineName); + if (empty($connections)) { + $connections = $this->createClient($coroutineName, $config); + } } if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { $number > 0 && $connections->beginTransaction(); @@ -125,6 +129,18 @@ class Connection extends Component } + /** + * @param $name + * @param $isMaster + * @param $max + * @throws Exception + */ + public function initConnections($name, $isMaster, $max) + { + $this->getPool()->initConnections($name, $isMaster, $max); + } + + /** * @param string $name * @param mixed $config diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index c0323e07..354935db 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -126,4 +126,16 @@ class Redis extends Component } + /** + * @param $name + * @param $isMaster + * @param $max + * @throws Exception + */ + public function initConnections($name, $isMaster, $max) + { + $this->getPool()->initConnections($name, $isMaster, $max); + } + + }