diff --git a/src/Connection.php b/src/Connection.php index 4c1c1b2..4f7f00f 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -43,7 +43,11 @@ class Connection extends Component public string $database = ''; - public int $timeout = 1900; + public int $connect_timeout = 30; + + public int $read_timeout = 10; + + public array $pool; /** * @var bool @@ -198,11 +202,14 @@ class Connection extends Component public function masterInstance(): PDO { return $this->connections()->get([ - 'cds' => $this->cds, - 'username' => $this->username, - 'password' => $this->password, - 'attributes' => $this->attributes, - 'dbname' => $this->database + 'cds' => $this->cds, + 'username' => $this->username, + 'password' => $this->password, + 'attributes' => $this->attributes, + 'connect_timeout' => $this->connect_timeout, + 'read_timeout' => $this->read_timeout, + 'dbname' => $this->database, + 'pool' => $this->pool ], true); } diff --git a/src/DatabasesProviders.php b/src/DatabasesProviders.php index 85cb65a..e515a0e 100644 --- a/src/DatabasesProviders.php +++ b/src/DatabasesProviders.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Database; -use Note\Inject; use Exception; use Kiri\Abstracts\Config; use Kiri\Abstracts\Providers; @@ -12,6 +11,7 @@ use Kiri\Application; use Kiri\Events\EventProvider; use Kiri\Exception\ConfigException; use Kiri\Kiri; +use Note\Inject; use Server\Events\OnWorkerStart; /** @@ -83,18 +83,20 @@ class DatabasesProviders extends Providers */ private function _settings($database): array { + $clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60]; return [ - 'class' => Connection::class, - 'id' => $database['id'], - 'cds' => $database['cds'], - 'username' => $database['username'], - 'password' => $database['password'], - 'tablePrefix' => $database['tablePrefix'], - 'database' => $database['database'], - 'maxNumber' => $this->_pooLength['max'], - 'minNumber' => $this->_pooLength['min'], - 'charset' => $database['charset'] ?? 'utf8mb4', - 'slaveConfig' => $database['slaveConfig'] + 'id' => $database['id'], + 'cds' => $database['cds'], + 'username' => $database['username'], + 'password' => $database['password'], + 'tablePrefix' => $database['tablePrefix'], + 'database' => $database['database'], + 'connect_timeout' => $database['connect_timeout'] ?? 30, + 'read_timeout' => $database['read_timeout'] ?? 10, + 'pool' => $clientPool, + 'attributes' => $database['attributes'] ?? [], + 'charset' => $database['charset'] ?? 'utf8mb4', + 'slaveConfig' => $database['slaveConfig'] ]; } diff --git a/src/Mysql/PDO.php b/src/Mysql/PDO.php index 04c4006..4ea5085 100644 --- a/src/Mysql/PDO.php +++ b/src/Mysql/PDO.php @@ -34,6 +34,8 @@ class PDO implements StopHeartbeatCheck public string $username; public string $password; public string $charset; + public int $connect_timeout; + public int $read_timeout; public array $attributes = []; @@ -48,6 +50,8 @@ class PDO implements StopHeartbeatCheck $this->cds = $config['cds']; $this->username = $config['username']; $this->password = $config['password']; + $this->connect_timeout = $config['connect_timeout'] ?? 30; + $this->read_timeout = $config['read_timeout'] ?? 10; $this->charset = $config['charset'] ?? 'utf8mb4'; $this->attributes = $config['attributes'] ?? []; } @@ -302,7 +306,7 @@ class PDO implements StopHeartbeatCheck $link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [ \PDO::ATTR_EMULATE_PREPARES => false, \PDO::ATTR_CASE => \PDO::CASE_NATURAL, - \PDO::ATTR_TIMEOUT => 60, + \PDO::ATTR_TIMEOUT => $this->connect_timeout, \PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset ]);