This commit is contained in:
2021-12-06 17:01:55 +08:00
parent ad537d1085
commit 014ccb5fb8
3 changed files with 32 additions and 19 deletions
+13 -6
View File
@@ -43,7 +43,11 @@ class Connection extends Component
public string $database = ''; public string $database = '';
public int $timeout = 1900; public int $connect_timeout = 30;
public int $read_timeout = 10;
public array $pool;
/** /**
* @var bool * @var bool
@@ -198,11 +202,14 @@ class Connection extends Component
public function masterInstance(): PDO public function masterInstance(): PDO
{ {
return $this->connections()->get([ return $this->connections()->get([
'cds' => $this->cds, 'cds' => $this->cds,
'username' => $this->username, 'username' => $this->username,
'password' => $this->password, 'password' => $this->password,
'attributes' => $this->attributes, 'attributes' => $this->attributes,
'dbname' => $this->database 'connect_timeout' => $this->connect_timeout,
'read_timeout' => $this->read_timeout,
'dbname' => $this->database,
'pool' => $this->pool
], true); ], true);
} }
+14 -12
View File
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Database; namespace Database;
use Note\Inject;
use Exception; use Exception;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Abstracts\Providers; use Kiri\Abstracts\Providers;
@@ -12,6 +11,7 @@ use Kiri\Application;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Kiri; use Kiri\Kiri;
use Note\Inject;
use Server\Events\OnWorkerStart; use Server\Events\OnWorkerStart;
/** /**
@@ -83,18 +83,20 @@ class DatabasesProviders extends Providers
*/ */
private function _settings($database): array private function _settings($database): array
{ {
$clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60];
return [ return [
'class' => Connection::class, 'id' => $database['id'],
'id' => $database['id'], 'cds' => $database['cds'],
'cds' => $database['cds'], 'username' => $database['username'],
'username' => $database['username'], 'password' => $database['password'],
'password' => $database['password'], 'tablePrefix' => $database['tablePrefix'],
'tablePrefix' => $database['tablePrefix'], 'database' => $database['database'],
'database' => $database['database'], 'connect_timeout' => $database['connect_timeout'] ?? 30,
'maxNumber' => $this->_pooLength['max'], 'read_timeout' => $database['read_timeout'] ?? 10,
'minNumber' => $this->_pooLength['min'], 'pool' => $clientPool,
'charset' => $database['charset'] ?? 'utf8mb4', 'attributes' => $database['attributes'] ?? [],
'slaveConfig' => $database['slaveConfig'] 'charset' => $database['charset'] ?? 'utf8mb4',
'slaveConfig' => $database['slaveConfig']
]; ];
} }
+5 -1
View File
@@ -34,6 +34,8 @@ class PDO implements StopHeartbeatCheck
public string $username; public string $username;
public string $password; public string $password;
public string $charset; public string $charset;
public int $connect_timeout;
public int $read_timeout;
public array $attributes = []; public array $attributes = [];
@@ -48,6 +50,8 @@ class PDO implements StopHeartbeatCheck
$this->cds = $config['cds']; $this->cds = $config['cds'];
$this->username = $config['username']; $this->username = $config['username'];
$this->password = $config['password']; $this->password = $config['password'];
$this->connect_timeout = $config['connect_timeout'] ?? 30;
$this->read_timeout = $config['read_timeout'] ?? 10;
$this->charset = $config['charset'] ?? 'utf8mb4'; $this->charset = $config['charset'] ?? 'utf8mb4';
$this->attributes = $config['attributes'] ?? []; $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, [ $link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [
\PDO::ATTR_EMULATE_PREPARES => false, \PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_CASE => \PDO::CASE_NATURAL, \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_USE_BUFFERED_QUERY => true,
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
]); ]);