diff --git a/Connection.php b/Connection.php index 6d28b32..a6d3219 100644 --- a/Connection.php +++ b/Connection.php @@ -51,12 +51,14 @@ class Connection extends Component public string $database = ''; - public int $connect_timeout = 30; + public int $timeout = 30; 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]; @@ -72,9 +74,6 @@ class Connection extends Component public bool $enableCache = false; - private ?PDO $_pdo = null; - - /** * @var string */ @@ -129,7 +128,7 @@ class Connection extends Component */ 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 { + $pool->flush($this->cds, $this->pool['min'] ?? 1); $length = $pool->size($this->cds); for ($i = 0; $i < $length; $i++) { try { @@ -151,7 +151,6 @@ class Connection extends Component if (!str_contains($exception->getMessage(), 'Client timeout.')) { $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, $this->username, $this->password, array_merge($this->attributes, [ - PDO::ATTR_CASE => PDO::CASE_NATURAL, - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, - PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, - PDO::ATTR_STRINGIFY_FETCHES => false, - PDO::ATTR_EMULATE_PREPARES => true, - PDO::ATTR_TIMEOUT => $this->connect_timeout, - PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset + PDO::ATTR_CASE => PDO::CASE_NATURAL, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, + PDO::ATTR_STRINGIFY_FETCHES => false, + PDO::ATTR_EMULATE_PREPARES => true, + PDO::ATTR_TIMEOUT => $this->timeout, + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset ])); } diff --git a/DatabasesProviders.php b/DatabasesProviders.php index 68ff1ef..e2034b9 100644 --- a/DatabasesProviders.php +++ b/DatabasesProviders.php @@ -63,8 +63,8 @@ class DatabasesProviders extends Providers 'password' => $database['password'], 'tablePrefix' => $database['tablePrefix'], 'database' => $database['database'], - 'connect_timeout' => $database['connect_timeout'] ?? 10, - 'idle_time' => $database['idle_time'] ?? 60, + 'timeout' => $database['timeout'] ?? 10, + 'tick_time' => $database['tick_time'] ?? 60, 'waite_time' => $database['waite_time'] ?? 3, 'pool' => $clientPool, 'attributes' => $database['attributes'] ?? [],