eee
This commit is contained in:
+42
-26
@@ -143,27 +143,48 @@ 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 {
|
||||||
$bool = $pool->get($this->cds);
|
if (($client = $this->pop($pool, false)) === false) {
|
||||||
if ($bool === false) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/** @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]);
|
|
||||||
}
|
|
||||||
$pool->push($this->cds, [$client, time()]);
|
$pool->push($this->cds, [$client, time()]);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$this->logger->error(throwable($exception));
|
if (!str_contains($exception->getMessage(), 'Client timeout.')) {
|
||||||
|
$this->logger->error(throwable($exception));
|
||||||
|
}
|
||||||
$pool->abandon($this->cds);
|
$pool->abandon($this->cds);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Pool $pool
|
||||||
|
* @param bool $isWaite
|
||||||
|
* @return PDO|bool
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function pop(Pool $pool, bool $isWaite): PDO|bool
|
||||||
|
{
|
||||||
|
if ($isWaite) {
|
||||||
|
$bool = $pool->get($this->cds, $this->waite_time);
|
||||||
|
} else {
|
||||||
|
$bool = $pool->get($this->cds);
|
||||||
|
}
|
||||||
|
if ($bool === 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]);
|
||||||
|
}
|
||||||
|
return $client;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
@@ -202,22 +223,17 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
protected function getNormalClientHealth(): PDO
|
protected function getNormalClientHealth(): PDO
|
||||||
{
|
{
|
||||||
$data = $this->pool()->get($this->cds, $this->waite_time);
|
try {
|
||||||
if ($data === false) {
|
$data = $this->pop($this->pool(), true);
|
||||||
throw new Exception('Pool waite timeout at ' . $this->waite_time);
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
[$client, $time] = $data;
|
|
||||||
if ((time() - $time) < $this->idle_time && $this->canUse($client)) {
|
|
||||||
return $client;
|
|
||||||
}
|
|
||||||
|
|
||||||
$client = null;
|
|
||||||
$data = null;
|
|
||||||
$this->logger->error('PDO连接已失效, 空闲超时或已不可用,重新获取.', [$this->cds]);
|
|
||||||
$this->pool()->abandon($this->cds);
|
|
||||||
|
|
||||||
return $this->getNormalClientHealth();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user