This commit is contained in:
2023-08-29 22:27:47 +08:00
parent 2b902f3624
commit a2ceb7310e
2 changed files with 15 additions and 16 deletions
+13 -14
View File
@@ -51,12 +51,14 @@ class Connection extends Component
public string $database = ''; public string $database = '';
public int $connect_timeout = 30; public int $timeout = 30;
public int $waite_time = 3; public int $waite_time = 3;
public int $idle_time = 60; public int $tick_time = 60;
public int $idle_count = 3;
public array $pool = ['max' => 10, 'min' => 1]; public array $pool = ['max' => 10, 'min' => 1];
@@ -72,9 +74,6 @@ class Connection extends Component
public bool $enableCache = false; public bool $enableCache = false;
private ?PDO $_pdo = null;
/** /**
* @var string * @var string
*/ */
@@ -129,7 +128,7 @@ class Connection extends Component
*/ */
public function tick(): void public function tick(): void
{ {
$this->timerId = Timer::tick(120000, fn() => $this->checkClientHealth($this->pool())); $this->timerId = Timer::tick($this->tick_time, fn() => $this->checkClientHealth($this->pool()));
} }
@@ -140,6 +139,7 @@ class Connection extends Component
*/ */
protected function checkClientHealth(Pool $pool): void protected function checkClientHealth(Pool $pool): void
{ {
$pool->flush($this->cds, $this->pool['min'] ?? 1);
$length = $pool->size($this->cds); $length = $pool->size($this->cds);
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
try { try {
@@ -151,7 +151,6 @@ class Connection extends Component
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]);
} }
$pool->abandon($this->cds);
} }
} }
} }
@@ -383,13 +382,13 @@ class Connection extends Component
{ {
return new PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds, return new PDO('mysql:dbname=' . $this->database . ';host=' . $this->cds,
$this->username, $this->password, array_merge($this->attributes, [ $this->username, $this->password, array_merge($this->attributes, [
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,
PDO::ATTR_STRINGIFY_FETCHES => false, PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => true, PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_TIMEOUT => $this->connect_timeout, PDO::ATTR_TIMEOUT => $this->timeout,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
])); ]));
} }
+2 -2
View File
@@ -63,8 +63,8 @@ class DatabasesProviders extends Providers
'password' => $database['password'], 'password' => $database['password'],
'tablePrefix' => $database['tablePrefix'], 'tablePrefix' => $database['tablePrefix'],
'database' => $database['database'], 'database' => $database['database'],
'connect_timeout' => $database['connect_timeout'] ?? 10, 'timeout' => $database['timeout'] ?? 10,
'idle_time' => $database['idle_time'] ?? 60, 'tick_time' => $database['tick_time'] ?? 60,
'waite_time' => $database['waite_time'] ?? 3, 'waite_time' => $database['waite_time'] ?? 3,
'pool' => $clientPool, 'pool' => $clientPool,
'attributes' => $database['attributes'] ?? [], 'attributes' => $database['attributes'] ?? [],