From 9ce45e39e820e0ec4da42c2ad98fec7d77383a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sat, 20 Feb 2021 13:35:17 +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 | 8 ++++---- System/Abstracts/Pool.php | 12 +++++++----- System/Cache/Redis.php | 2 +- System/Pool/Connection.php | 14 +++++++------- System/Pool/Redis.php | 6 +++--- System/Pool/Timeout.php | 1 + 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/Database/Connection.php b/Database/Connection.php index 6308f943..c5a5c3ea 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -98,8 +98,8 @@ class Connection extends Component public function getConnect($sql = NULL): PDO { $connections = $this->connections(); - $connections->initConnections($this->cds, true, $this->maxNumber); - $connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber); + $connections->initConnections('mysql', $this->cds, true, $this->maxNumber); + $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); $connections->setTimeout($this->timeout); return $this->getPdo($sql); @@ -113,8 +113,8 @@ class Connection extends Component public function fill() { $connections = $this->connections(); - $connections->initConnections($this->cds, true, $this->maxNumber); - $connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber); + $connections->initConnections('mysql', $this->cds, true, $this->maxNumber); + $connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber); } diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 6090b029..6c6292c3 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -30,13 +30,14 @@ abstract class Pool extends Component use Timeout; /** + * @param $driver * @param $name * @param false $isMaster * @param int $max */ - public function initConnections($name, $isMaster = false, $max = 60) + public function initConnections($driver, $name, $isMaster = false, $max = 60) { - $name = $this->name($name, $isMaster); + $name = $this->name($driver, $name, $isMaster); if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) { return; } @@ -71,16 +72,17 @@ abstract class Pool extends Component /** + * @param $driver * @param $cds * @param false $isMaster * @return string */ - #[Pure] public function name($cds, $isMaster = false): string + #[Pure] public function name($driver, $cds, $isMaster = false): string { if ($isMaster === true) { - return hash('sha256', $cds . 'master'); + return $driver . ':' . hash('sha256', $cds . 'master'); } else { - return hash('sha256', $cds . 'slave'); + return $driver . ':' . hash('sha256', $cds . 'slave'); } } diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index 36b9c1d3..1eefcc97 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -57,7 +57,7 @@ class Redis extends Component $length = env('REDIS.POOL_LENGTH', 100); - $connections->initConnections('redis:' . $name, true, $length); + $connections->initConnections('redis', 'redis:' . $name, true, $length); } diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index a65d77af..52d1ce5e 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -52,7 +52,7 @@ class Connection extends Pool */ public function inTransaction($cds): bool { - return Context::getContext('begin_' . $this->name($cds, true)) == 0; + return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0; } /** @@ -60,7 +60,7 @@ class Connection extends Pool */ public function beginTransaction($coroutineName) { - $coroutineName = $this->name($coroutineName, true); + $coroutineName = $this->name('mysql', $coroutineName, true); if (!Context::hasContext('begin_' . $coroutineName)) { Context::setContext('begin_' . $coroutineName, 0); } @@ -79,7 +79,7 @@ class Connection extends Pool */ public function commit($coroutineName) { - $coroutineName = $this->name($coroutineName, true); + $coroutineName = $this->name('mysql', $coroutineName, true); if (!Context::hasContext('begin_' . $coroutineName)) { return; } @@ -104,7 +104,7 @@ class Connection extends Pool */ private function getIndex($name, $isMaster = false): array { - return [Coroutine::getCid(), $this->name($name, $isMaster)]; + return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)]; } /** @@ -112,7 +112,7 @@ class Connection extends Pool */ public function rollback($coroutineName) { - $coroutineName = $this->name($coroutineName, true); + $coroutineName = $this->name('mysql', $coroutineName, true); if (!Context::hasContext('begin_' . $coroutineName)) { return; } @@ -136,7 +136,7 @@ class Connection extends Pool */ public function getConnection(array $config, $isMaster = false): mixed { - $coroutineName = $this->name($config['cds'], $isMaster); + $coroutineName = $this->name('mysql', $config['cds'], $isMaster); if (!isset($this->hasCreate[$coroutineName])) { $this->hasCreate[$coroutineName] = 0; } @@ -211,7 +211,7 @@ class Connection extends Pool */ public function release($coroutineName, $isMaster) { - $coroutineName = $this->name($coroutineName, $isMaster); + $coroutineName = $this->name('mysql', $coroutineName, $isMaster); if (!$this->hasClient($coroutineName)) { return; } diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index ab6bd424..665bf650 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -42,7 +42,7 @@ class Redis extends Pool public function getConnection(array $config, $isMaster = false): mixed { $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; - $coroutineName = $this->name('redis:' . $name, $isMaster); + $coroutineName = $this->name('redis', 'redis:' . $name, $isMaster); if (($redis = Context::getContext($coroutineName)) instanceof \Redis) { return $redis; } @@ -105,7 +105,7 @@ class Redis extends Pool public function release(array $config, $isMaster = false) { $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; - $coroutineName = $this->name('redis:' . $name, $isMaster); + $coroutineName = $this->name('redis', 'redis:' . $name, $isMaster); if (!Context::hasContext($coroutineName)) { return; } @@ -121,7 +121,7 @@ class Redis extends Pool public function destroy(array $config, $isMaster = false) { $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; - $coroutineName = $this->name('redis:' . $name, $isMaster); + $coroutineName = $this->name('redis', 'redis:' . $name, $isMaster); if (!Context::hasContext($coroutineName)) { return; } diff --git a/System/Pool/Timeout.php b/System/Pool/Timeout.php index 23380dfe..077f8afa 100644 --- a/System/Pool/Timeout.php +++ b/System/Pool/Timeout.php @@ -40,6 +40,7 @@ trait Timeout { $channels = $this->getChannels(); foreach ($channels as $name => $channel) { + $this->debug('release ' . $name); $this->pop($channel, $name, $retain_number); } if ($retain_number == 0) {