This commit is contained in:
2023-08-29 22:13:37 +08:00
parent 5158b709bd
commit 91edb08238
2 changed files with 17 additions and 52 deletions
+8 -8
View File
@@ -77,8 +77,8 @@ class Command extends Component
*/ */
public function all(): bool|array public function all(): bool|array
{ {
$client = $this->connection->getConnection();
try { try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -90,7 +90,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } finally {
$this->connection->release($client); $this->connection->release($client ?? null);
} }
} }
@@ -100,8 +100,8 @@ class Command extends Component
*/ */
public function one(): null|bool|array public function one(): null|bool|array
{ {
$client = $this->connection->getConnection();
try { try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -113,7 +113,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } finally {
$this->connection->release($client); $this->connection->release($client ?? null);
} }
} }
@@ -123,8 +123,8 @@ class Command extends Component
*/ */
public function fetchColumn(): mixed public function fetchColumn(): mixed
{ {
$client = $this->connection->getConnection();
try { try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -136,7 +136,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } finally {
$this->connection->release($client); $this->connection->release($client ?? null);
} }
} }
@@ -158,8 +158,8 @@ class Command extends Component
*/ */
private function _execute(): bool|int private function _execute(): bool|int
{ {
$client = $this->connection->getConnection();
try { try {
$client = $this->connection->getConnection();
if (($prepare = $client->prepare($this->sql)) === false) { if (($prepare = $client->prepare($this->sql)) === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -178,7 +178,7 @@ class Command extends Component
} }
return $this->error($throwable); return $this->error($throwable);
} finally { } finally {
$this->connection->release($client); $this->connection->release($client ?? null);
} }
} }
+9 -44
View File
@@ -143,13 +143,13 @@ class Connection extends Component
$length = $pool->size($this->cds); $length = $pool->size($this->cds);
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
try { try {
if (($client = $this->pop($pool, false)) === false) { if (($client = $this->validator($pool)) === false) {
break; break;
} }
$pool->push($this->cds, [$client, time()]); $pool->push($this->cds, $client);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
if (!str_contains($exception->getMessage(), 'Client timeout.')) { if (!str_contains($exception->getMessage(), 'Client timeout.')) {
$this->logger->error(throwable($exception)); $this->logger->error(throwable($exception), [$this->cds]);
} }
$pool->abandon($this->cds); $pool->abandon($this->cds);
} }
@@ -159,25 +159,16 @@ class Connection extends Component
/** /**
* @param Pool $pool * @param Pool $pool
* @param bool $isWaite
* @return PDO|bool * @return PDO|bool
* @throws Exception * @throws Exception
*/ */
protected function pop(Pool $pool, bool $isWaite): PDO|bool protected function validator(Pool $pool): PDO|bool
{ {
if ($isWaite) { if (($bool = $pool->get($this->cds)) === false) {
$bool = $pool->get($this->cds, $this->waite_time);
} else {
$bool = $pool->get($this->cds);
}
if ($bool === false) {
return false; return false;
} }
/** @var PDO $client */ /** @var PDO $client */
[$client, $time] = $bool; [$client, $time] = $bool;
if ((time() - $time) > $this->idle_time) {
throw new Exception('Client timeout.');
}
if ($client->query('select 1') === false) { if ($client->query('select 1') === false) {
throw new Exception($client->errorInfo()[1]); throw new Exception($client->errorInfo()[1]);
} }
@@ -223,37 +214,11 @@ class Connection extends Component
*/ */
protected function getNormalClientHealth(): PDO protected function getNormalClientHealth(): PDO
{ {
try { $data = $this->pool()->get($this->cds, $this->waite_time);
$data = $this->pop($this->pool(), true); if ($data === false) {
if ($data === false) { throw new Exception('Client Waite timeout.');
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;
} }
return $data[0];
} }