This commit is contained in:
2024-05-01 02:02:58 +08:00
parent 55e1f6235e
commit 9fe0698d1c
+15 -14
View File
@@ -28,8 +28,9 @@ use Kiri\Server\Events\OnTaskerStart;
use Kiri\Server\Events\OnAfterRequest; use Kiri\Server\Events\OnAfterRequest;
use Kiri\Di\Inject\Container; use Kiri\Di\Inject\Container;
use Swoole\Timer; use Swoole\Timer;
//use Database\Base\PDO; use Database\Base\PDO;
use PDO;
//use PDO;
/** /**
* Class Connection * Class Connection
@@ -123,14 +124,14 @@ class Connection extends Component
*/ */
protected function checkClientHealth(Pool $pool): void protected function checkClientHealth(Pool $pool): void
{ {
$pool->flush($this->cds, $this->pool['min'] ?? 1); $pool->flush('mysql.' . $this->cds, $this->pool['min'] ?? 1);
$length = $pool->size($this->cds); $length = $pool->size('mysql.' . $this->cds);
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
try { try {
if (($client = $this->validator($pool)) === false) { if (($client = $this->validator($pool)) === false) {
break; break;
} }
$pool->push($this->cds, $client); $pool->push('mysql.' . $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->cds]); $this->logger->error(throwable($exception), [$this->cds]);
@@ -148,7 +149,7 @@ class Connection extends Component
protected function validator(Pool $pool): PDO|bool protected function validator(Pool $pool): PDO|bool
{ {
/** @var $client PDO */ /** @var $client PDO */
if (($client = $pool->get($this->cds)) === false) { if (($client = $pool->get('mysql.' . $this->cds)) === false) {
return false; return false;
} }
if ($client->query('select 1') === false) { if ($client->query('select 1') === false) {
@@ -178,7 +179,7 @@ class Connection extends Component
*/ */
protected function getNormalClientHealth(): PDO protected function getNormalClientHealth(): PDO
{ {
$data = $this->pool()->get($this->cds, $this->waite_time); $data = $this->pool()->get('mysql.' . $this->cds, $this->waite_time);
if ($data === false) { if ($data === false) {
throw new Exception('Client Waite timeout.'); throw new Exception('Client Waite timeout.');
} }
@@ -304,7 +305,7 @@ class Connection extends Component
public function release(PDO $pdo): void public function release(PDO $pdo): void
{ {
if (!$this->inTransaction()) { if (!$this->inTransaction()) {
$this->pool()->push($this->cds, $pdo); $this->pool()->push('mysql.' . $this->cds, $pdo);
} }
} }
@@ -316,7 +317,7 @@ class Connection extends Component
*/ */
public function clear_connection(): void public function clear_connection(): void
{ {
$this->pool()->flush($this->cds, 0); $this->pool()->flush('mysql.' . $this->cds, 0);
} }
@@ -328,7 +329,7 @@ class Connection extends Component
if ($this->timerId > -1) { if ($this->timerId > -1) {
Timer::clear($this->timerId); Timer::clear($this->timerId);
} }
$this->pool()->close($this->cds); $this->pool()->close('mysql.' . $this->cds);
} }
@@ -337,8 +338,8 @@ class Connection extends Component
*/ */
public function newConnect(): \PDO public function newConnect(): \PDO
{ {
// $pdo = new PDO($this->database, $this->cds, $this->username, $this->password, [ $pdo = new PDO($this->database, $this->cds, $this->username, $this->password, [
$pdo = new \PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, $this->username, $this->password, [ // $pdo = new \PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, $this->username, $this->password, [
\PDO::ATTR_CASE => \PDO::CASE_NATURAL, \PDO::ATTR_CASE => \PDO::CASE_NATURAL,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL, \PDO::ATTR_ORACLE_NULLS => \PDO::NULL_NATURAL,
@@ -359,8 +360,8 @@ class Connection extends Component
*/ */
protected function pool(): Pool protected function pool(): Pool
{ {
if (!$this->connections->hasChannel($this->cds)) { if (!$this->connections->hasChannel('mysql.' . $this->cds)) {
$this->connections->created($this->cds, $this->pool['max'] ?? 1, [$this, 'newConnect']); $this->connections->created('mysql.' . $this->cds, $this->pool['max'] ?? 1, [$this, 'newConnect']);
} }
return $this->connections; return $this->connections;
} }