From 91edb082384ac51241f3e2106e4511da78c6dadd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 29 Aug 2023 22:13:37 +0800 Subject: [PATCH] eee --- Command.php | 16 +++++++-------- Connection.php | 53 +++++++++----------------------------------------- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/Command.php b/Command.php index 5f301a7..28d1f01 100644 --- a/Command.php +++ b/Command.php @@ -77,8 +77,8 @@ class Command extends Component */ public function all(): bool|array { - $client = $this->connection->getConnection(); try { + $client = $this->connection->getConnection(); if (($prepare = $client->prepare($this->sql)) === false) { throw new Exception($client->errorInfo()[1]); } @@ -90,7 +90,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->connection->release($client); + $this->connection->release($client ?? null); } } @@ -100,8 +100,8 @@ class Command extends Component */ public function one(): null|bool|array { - $client = $this->connection->getConnection(); try { + $client = $this->connection->getConnection(); if (($prepare = $client->prepare($this->sql)) === false) { throw new Exception($client->errorInfo()[1]); } @@ -113,7 +113,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->connection->release($client); + $this->connection->release($client ?? null); } } @@ -123,8 +123,8 @@ class Command extends Component */ public function fetchColumn(): mixed { - $client = $this->connection->getConnection(); try { + $client = $this->connection->getConnection(); if (($prepare = $client->prepare($this->sql)) === false) { throw new Exception($client->errorInfo()[1]); } @@ -136,7 +136,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->connection->release($client); + $this->connection->release($client ?? null); } } @@ -158,8 +158,8 @@ class Command extends Component */ private function _execute(): bool|int { - $client = $this->connection->getConnection(); try { + $client = $this->connection->getConnection(); if (($prepare = $client->prepare($this->sql)) === false) { throw new Exception($client->errorInfo()[1]); } @@ -178,7 +178,7 @@ class Command extends Component } return $this->error($throwable); } finally { - $this->connection->release($client); + $this->connection->release($client ?? null); } } diff --git a/Connection.php b/Connection.php index 91dd9e6..016356e 100644 --- a/Connection.php +++ b/Connection.php @@ -143,13 +143,13 @@ class Connection extends Component $length = $pool->size($this->cds); for ($i = 0; $i < $length; $i++) { try { - if (($client = $this->pop($pool, false)) === false) { + if (($client = $this->validator($pool)) === false) { break; } - $pool->push($this->cds, [$client, time()]); + $pool->push($this->cds, $client); } catch (\Throwable $exception) { if (!str_contains($exception->getMessage(), 'Client timeout.')) { - $this->logger->error(throwable($exception)); + $this->logger->error(throwable($exception), [$this->cds]); } $pool->abandon($this->cds); } @@ -159,25 +159,16 @@ class Connection extends Component /** * @param Pool $pool - * @param bool $isWaite * @return PDO|bool * @throws Exception */ - protected function pop(Pool $pool, bool $isWaite): PDO|bool + protected function validator(Pool $pool): PDO|bool { - if ($isWaite) { - $bool = $pool->get($this->cds, $this->waite_time); - } else { - $bool = $pool->get($this->cds); - } - if ($bool === false) { + if (($bool = $pool->get($this->cds)) === false) { return false; } /** @var PDO $client */ [$client, $time] = $bool; - if ((time() - $time) > $this->idle_time) { - throw new Exception('Client timeout.'); - } if ($client->query('select 1') === false) { throw new Exception($client->errorInfo()[1]); } @@ -223,37 +214,11 @@ class Connection extends Component */ protected function getNormalClientHealth(): PDO { - try { - $data = $this->pop($this->pool(), true); - if ($data === false) { - throw new Exception('Pool waite timeout at ' . $this->waite_time); - } - return $data; - }catch (\Throwable $exception) { - $this->logger->error($exception->getMessage(), [$this->cds]); - $this->pool()->abandon($this->cds); - return $this->getNormalClientHealth(); - } - } - - - /** - * @param PDO|null $client - * @return bool - */ - protected function canUse(?PDO $client): bool - { - if (is_null($client)) { - return false; - } - try { - if ($client->query('select 1') === false) { - return false; - } - return true; - } catch (\Throwable $exception) { - return false; + $data = $this->pool()->get($this->cds, $this->waite_time); + if ($data === false) { + throw new Exception('Client Waite timeout.'); } + return $data[0]; }