From 1f828ad01d71dc3cbb6292ed5debd5608898e588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 1 Mar 2021 16:01:32 +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 | 4 +- HttpServer/Abstracts/Callback.php | 3 +- System/Pool/Connection.php | 493 +++++++++++++++--------------- System/Pool/Redis.php | 10 +- 4 files changed, 255 insertions(+), 255 deletions(-) diff --git a/Database/Connection.php b/Database/Connection.php index 141211fd..0c8113c8 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -317,8 +317,8 @@ class Connection extends Component public function disconnect() { $connections = $this->connections(); - $connections->disconnect($this->cds); - $connections->disconnect($this->slaveConfig['cds']); + $connections->disconnect($this->cds, true); + $connections->disconnect($this->slaveConfig['cds'], false); } } diff --git a/HttpServer/Abstracts/Callback.php b/HttpServer/Abstracts/Callback.php index a19dda2a..ec9c10fc 100644 --- a/HttpServer/Abstracts/Callback.php +++ b/HttpServer/Abstracts/Callback.php @@ -35,8 +35,9 @@ abstract class Callback extends HttpService try { fire(Event::SYSTEM_RESOURCE_CLEAN); + \logger()->insert(); + Snowflake::clearProcessId($server->worker_pid); - Timer::clearAll(); $logger = Snowflake::app()->getLogger(); $logger->write($this->_MESSAGE[$message] . $worker_id); diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index 3648723f..b81d2392 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -20,136 +20,136 @@ class Connection extends Pool { - public array $hasCreate = []; + public array $hasCreate = []; - public int $timeout = 1900; + public int $timeout = 1900; - /** @var PDO[] */ - protected array $connections = []; + /** @var PDO[] */ + protected array $connections = []; - /** - * @param $timeout - */ - public function setTimeout($timeout) - { - $this->timeout = $timeout; - } + /** + * @param $timeout + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + } - /** - * @param $value - */ - public function setLength($value) - { - $this->max = $value; - } + /** + * @param $value + */ + public function setLength($value) + { + $this->max = $value; + } - /** - * @param $cds - * @return bool - * - * db is in transaction - */ - public function inTransaction($cds): bool - { - return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0; - } + /** + * @param $cds + * @return bool + * + * db is in transaction + */ + public function inTransaction($cds): bool + { + return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0; + } - /** - * @param $coroutineName - */ - public function beginTransaction($coroutineName) - { - $coroutineName = $this->name('mysql', $coroutineName, true); - if (!Context::hasContext('begin_' . $coroutineName)) { - Context::setContext('begin_' . $coroutineName, 0); - } - Context::increment('begin_' . $coroutineName); - if (!Context::getContext('begin_' . $coroutineName) !== 0) { - return; - } - $connection = Context::getContext($coroutineName); - if ($connection instanceof PDO && !$connection->inTransaction()) { - $connection->beginTransaction(); - } - } + /** + * @param $coroutineName + */ + public function beginTransaction($coroutineName) + { + $coroutineName = $this->name('mysql', $coroutineName, true); + if (!Context::hasContext('begin_' . $coroutineName)) { + Context::setContext('begin_' . $coroutineName, 0); + } + Context::increment('begin_' . $coroutineName); + if (!Context::getContext('begin_' . $coroutineName) !== 0) { + return; + } + $connection = Context::getContext($coroutineName); + if ($connection instanceof PDO && !$connection->inTransaction()) { + $connection->beginTransaction(); + } + } - /** - * @param $coroutineName - */ - public function commit($coroutineName) - { - $coroutineName = $this->name('mysql', $coroutineName, true); - if (!Context::hasContext('begin_' . $coroutineName)) { - return; - } - if (Context::decrement('begin_' . $coroutineName) > 0) { - return; - } - $connection = Context::getContext($coroutineName); - if (!($connection instanceof PDO)) { - return; - } - Context::setContext('begin_' . $coroutineName, 0); - if ($connection->inTransaction()) { - $connection->commit(); - } - } + /** + * @param $coroutineName + */ + public function commit($coroutineName) + { + $coroutineName = $this->name('mysql', $coroutineName, true); + if (!Context::hasContext('begin_' . $coroutineName)) { + return; + } + if (Context::decrement('begin_' . $coroutineName) > 0) { + return; + } + $connection = Context::getContext($coroutineName); + if (!($connection instanceof PDO)) { + return; + } + Context::setContext('begin_' . $coroutineName, 0); + if ($connection->inTransaction()) { + $connection->commit(); + } + } - /** - * @param $name - * @param false $isMaster - * @return array - */ - private function getIndex($name, $isMaster = false): array - { - return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)]; - } + /** + * @param $name + * @param false $isMaster + * @return array + */ + private function getIndex($name, $isMaster = false): array + { + return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)]; + } - /** - * @param $coroutineName - */ - public function rollback($coroutineName) - { - $coroutineName = $this->name('mysql', $coroutineName, true); - if (!Context::hasContext('begin_' . $coroutineName)) { - return; - } - if (Context::decrement('begin_' . $coroutineName) > 0) { - return; - } - if (($connection = Context::getContext($coroutineName)) instanceof PDO) { - if ($connection->inTransaction()) { - $connection->rollBack(); - } - } - Context::setContext('begin_' . $coroutineName, 0); - } + /** + * @param $coroutineName + */ + public function rollback($coroutineName) + { + $coroutineName = $this->name('mysql', $coroutineName, true); + if (!Context::hasContext('begin_' . $coroutineName)) { + return; + } + if (Context::decrement('begin_' . $coroutineName) > 0) { + return; + } + if (($connection = Context::getContext($coroutineName)) instanceof PDO) { + if ($connection->inTransaction()) { + $connection->rollBack(); + } + } + Context::setContext('begin_' . $coroutineName, 0); + } - /** - * @param mixed $config - * @param bool $isMaster - * @return mixed - * @throws Exception - */ - public function get(mixed $config, $isMaster = false): mixed - { - $coroutineName = $this->name('mysql', $config['cds'], $isMaster); - if (!isset($this->hasCreate[$coroutineName])) { - $this->hasCreate[$coroutineName] = 0; - } - if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { - return $pdo; - } - $connections = $this->getFromChannel($coroutineName, $config); - if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { - $number > 0 && $connections->beginTransaction(); - } - return Context::setContext($coroutineName, $connections); - } + /** + * @param mixed $config + * @param bool $isMaster + * @return mixed + * @throws Exception + */ + public function get(mixed $config, $isMaster = false): mixed + { + $coroutineName = $this->name('mysql', $config['cds'], $isMaster); + if (!isset($this->hasCreate[$coroutineName])) { + $this->hasCreate[$coroutineName] = 0; + } + if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { + return $pdo; + } + $connections = $this->getFromChannel($coroutineName, $config); + if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { + $number > 0 && $connections->beginTransaction(); + } + return Context::setContext($coroutineName, $connections); + } /** @@ -158,25 +158,25 @@ class Connection extends Pool * @return PDO * @throws ComponentException */ - public function createClient(string $name, mixed $config): PDO - { - $this->printClients($config['cds'], $name, true); - // TODO: Implement createClient() method. - $link = new PDO($config['cds'], $config['username'], $config['password'], [ - PDO::ATTR_EMULATE_PREPARES => false, - PDO::ATTR_CASE => PDO::CASE_NATURAL, - PDO::ATTR_TIMEOUT => $this->timeout, - ]); - $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); - $link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); + public function createClient(string $name, mixed $config): PDO + { + $this->printClients($config['cds'], $name, true); + // TODO: Implement createClient() method. + $link = new PDO($config['cds'], $config['username'], $config['password'], [ + PDO::ATTR_EMULATE_PREPARES => false, + PDO::ATTR_CASE => PDO::CASE_NATURAL, + PDO::ATTR_TIMEOUT => $this->timeout, + ]); + $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); + $link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); - $charset = $config['charset'] ?? 'utf8mb4'; - if (!empty($charset)) { - $link->query('SET NAMES ' . $charset); - } - return $link; - } + $charset = $config['charset'] ?? 'utf8mb4'; + if (!empty($charset)) { + $link->query('SET NAMES ' . $charset); + } + return $link; + } /** @@ -185,131 +185,132 @@ class Connection extends Pool * @param false $isBefore * @throws ComponentException */ - public function printClients($cds, $coroutineName, $isBefore = false) - { + public function printClients($cds, $coroutineName, $isBefore = false) + { // $this->warning(($isBefore ? 'before ' : '') . 'create client[address: ' . $cds . ', ' . env('workerId') . ', coroutine: ' . Coroutine::getCid() . ', has num: ' . $this->size($coroutineName) . ', has create: ' . $this->hasCreate[$coroutineName] . ']'); - } + } - /** - * @param $coroutineName - * @param $isMaster - */ - public function release($coroutineName, $isMaster) - { - $coroutineName = $this->name('mysql', $coroutineName, $isMaster); - if (!$this->hasClient($coroutineName)) { - return; - } + /** + * @param $coroutineName + * @param $isMaster + */ + public function release($coroutineName, $isMaster) + { + $coroutineName = $this->name('mysql', $coroutineName, $isMaster); + if (!$this->hasClient($coroutineName)) { + return; + } - /** @var PDO $client */ - $client = Context::getContext($coroutineName); - if ($client->inTransaction()) { - $client->commit(); - } - $this->push($coroutineName, $client); - $this->remove($coroutineName); - $this->lastTime = time(); - } + /** @var PDO $client */ + $client = Context::getContext($coroutineName); + if ($client->inTransaction()) { + $client->commit(); + } + $this->push($coroutineName, $client); + $this->remove($coroutineName); + $this->lastTime = time(); + } - /** - * @param $coroutineName - * @return bool - */ - private function hasClient($coroutineName): bool - { - return Context::hasContext($coroutineName); - } + /** + * @param $coroutineName + * @return bool + */ + private function hasClient($coroutineName): bool + { + return Context::hasContext($coroutineName); + } - /** - * batch release - */ - public function connection_clear() - { - $connections = Context::getAllContext(); - foreach ($connections as $name => $connection) { - if (empty($connection) || !($connection instanceof PDO)) { - continue; - } - /** @var PDO $pdoClient */ - if ($connection->inTransaction()) { - $connection->commit(); - } - $this->push($name, $connection); - $this->remove($name); - } - $this->hasCreate = []; - $this->creates = 0; - } + /** + * batch release + */ + public function connection_clear() + { + $connections = Context::getAllContext(); + foreach ($connections as $name => $connection) { + if (empty($connection) || !($connection instanceof PDO)) { + continue; + } + /** @var PDO $pdoClient */ + if ($connection->inTransaction()) { + $connection->commit(); + } + $this->push($name, $connection); + $this->remove($name); + } + $this->hasCreate = []; + $this->creates = 0; + } - /** - * @param $coroutineName - */ - public function remove($coroutineName) - { - Context::remove($coroutineName); - } + /** + * @param $coroutineName + */ + public function remove($coroutineName) + { + Context::remove($coroutineName); + } - /** - * @param $name - * @param $client - * @return bool - */ - public function checkCanUse($name, $client): bool - { - try { - if (empty($client) || !($client instanceof PDO)) { - return $result = false; - } - if (!$client->getAttribute(PDO::ATTR_SERVER_INFO)) { - return $result = false; - } - return $result = true; - } catch (\Swoole\Error | \Throwable $exception) { - return $result = false; - } finally { - if (!$result) { - $this->desc($name); - } - } - } + /** + * @param $name + * @param $client + * @return bool + */ + public function checkCanUse($name, $client): bool + { + try { + if (empty($client) || !($client instanceof PDO)) { + return $result = false; + } + if (!$client->getAttribute(PDO::ATTR_SERVER_INFO)) { + return $result = false; + } + return $result = true; + } catch (\Swoole\Error | \Throwable $exception) { + return $result = false; + } finally { + if (!$result) { + $this->desc($name); + } + } + } - /** - * @param $coroutineName - * @throws Exception - */ - public function disconnect($coroutineName) - { - if (!$this->hasClient($coroutineName)) { - return; - } - $this->remove($coroutineName); - $this->clean($coroutineName); - } + /** + * @param $coroutineName + * @param bool $isMaster + * @throws Exception + */ + public function disconnect($coroutineName, $isMaster = false) + { + $coroutineName = $this->name($coroutineName, $isMaster); + if ($this->hasClient($coroutineName)) { + $this->remove($coroutineName); + } + $this->clean($coroutineName); + } - /** - * @param $coroutineName - */ - public function incr($coroutineName) - { - if (!isset($this->hasCreate[$coroutineName])) { - $this->hasCreate[$coroutineName] = 0; - } - $this->hasCreate[$coroutineName] += 1; - } + /** + * @param $coroutineName + */ + public function incr($coroutineName) + { + if (!isset($this->hasCreate[$coroutineName])) { + $this->hasCreate[$coroutineName] = 0; + } + $this->hasCreate[$coroutineName] += 1; + } - /** - * @param string $name - */ - public function desc(string $name) - { - if (!isset($this->hasCreate[$name])) { - $this->hasCreate[$name] = 0; - } - $this->hasCreate[$name] -= 1; - } + /** + * @param string $name + */ + public function desc(string $name) + { + if (!isset($this->hasCreate[$name])) { + $this->hasCreate[$name] = 0; + } + $this->hasCreate[$name] -= 1; + } } diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index 716dd3b0..ceebaa05 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -104,13 +104,11 @@ class Redis extends Pool { $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $coroutineName = $this->name('redis', 'redis:' . $name, $isMaster); - if (!Context::hasContext($coroutineName)) { - return; + if (Context::hasContext($coroutineName)) { + $this->desc($coroutineName); + + $this->remove($coroutineName); } - - $this->desc($coroutineName); - - $this->remove($coroutineName); $this->clean($coroutineName); }