eee
This commit is contained in:
+9
-44
@@ -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];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user