diff --git a/Database/Connection.php b/Database/Connection.php index 851eb10d..ede0f383 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -41,7 +41,8 @@ class Connection extends Component public int $timeout = 1900; - public int $maxNumber = 200; + public int $maxNumber = 30; + public int $minNumber = 10; /** * @var bool diff --git a/Database/DatabasesProviders.php b/Database/DatabasesProviders.php index 5efbdc2c..5e08fd7b 100644 --- a/Database/DatabasesProviders.php +++ b/Database/DatabasesProviders.php @@ -25,89 +25,92 @@ class DatabasesProviders extends Providers { - /** - * @param Application $application - * @throws Exception - */ - public function onImport(Application $application) - { - $application->set('db', $this); + /** + * @param Application $application + * @throws Exception + */ + public function onImport(Application $application) + { + $application->set('db', $this); - Event::on(Event::SERVER_TASK_START, [$this, 'createPool']); - Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']); - } + Event::on(Event::SERVER_TASK_START, [$this, 'createPool']); + Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']); + } - /** - * @param $name - * @return Connection - * @throws ConfigException - * @throws Exception - */ - public function get($name): Connection - { - $application = Snowflake::app(); - if ($application->has('databases.' . $name)) { - return $application->get('databases.' . $name); - } - $config = $this->getConfig($name); - return $application->set('databases.' . $name, [ - 'class' => Connection::class, - 'id' => $config['id'], - 'cds' => $config['cds'], - 'username' => $config['username'], - 'password' => $config['password'], - 'tablePrefix' => $config['tablePrefix'], - 'maxNumber' => $config['maxNumber'], - 'database' => $config['database'], - 'charset' => $config['charset'] ?? 'utf8mb4', - 'slaveConfig' => $config['slaveConfig'] - ]); - } + /** + * @param $name + * @return Connection + * @throws ConfigException + * @throws Exception + */ + public function get($name): Connection + { + $application = Snowflake::app(); + if ($application->has('databases.' . $name)) { + return $application->get('databases.' . $name); + } + $config = $this->getConfig($name); + + $max = Config::get('databases.pool.max', 30); + return $application->set('databases.' . $name, [ + 'class' => Connection::class, + 'id' => $config['id'], + 'cds' => $config['cds'], + 'username' => $config['username'], + 'password' => $config['password'], + 'tablePrefix' => $config['tablePrefix'], + 'maxNumber' => $max, + 'database' => $config['database'], + 'charset' => $config['charset'] ?? 'utf8mb4', + 'slaveConfig' => $config['slaveConfig'] + ]); + } - /** - * @throws ConfigException - * @throws Exception - */ - public function createPool() - { - $databases = Config::get('databases', []); - if (empty($databases)) { - return; - } - $application = Snowflake::app(); - foreach ($databases as $name => $database) { + /** + * @throws ConfigException + * @throws Exception + */ + public function createPool() + { + $databases = Config::get('databases.connections', []); + if (empty($databases)) { + return; + } - var_dump($database); + $max = Config::get('databases.pool', ['max' => 10, 'min' => 10]); - /** @var Connection $connection */ - $connection = $application->set('databases.' . $name, [ - 'class' => Connection::class, - 'id' => $database['id'], - 'cds' => $database['cds'], - 'username' => $database['username'], - 'password' => $database['password'], - 'tablePrefix' => $database['tablePrefix'], + $application = Snowflake::app(); + foreach ($databases as $name => $database) { + /** @var Connection $connection */ + $connection = $application->set('databases.' . $name, [ + 'class' => Connection::class, + 'id' => $database['id'], + 'cds' => $database['cds'], + 'username' => $database['username'], + 'password' => $database['password'], + 'tablePrefix' => $database['tablePrefix'], 'database' => $database['database'], - 'maxNumber' => $database['maxNumber'], - 'charset' => $database['charset'] ?? 'utf8mb4', - 'slaveConfig' => $database['slaveConfig'] - ]); - $connection->fill(); - } - } + 'maxNumber' => $max['max'], + 'minNumber' => $max['min'], + 'charset' => $database['charset'] ?? 'utf8mb4', + 'slaveConfig' => $database['slaveConfig'] + ]); + $connection->fill(); + } + } - /** - * @param $name - * @return mixed - * @throws ConfigException - */ - public function getConfig($name): mixed - { - return Config::get('databases.' . $name,null, true); - } + /** + * @param $name + * @return mixed + * @throws ConfigException + */ + public function getConfig($name): mixed + { + return Config::get('databases.' . $name, null, true); + } } diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index c7eae237..4fab4122 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -125,7 +125,9 @@ class Connection extends Pool if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { return $pdo; } + /** @var PDO $connections */ $connections = $this->getFromChannel($coroutineName, $config); + $connections->query('use `' . $config['database'].'`'); if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { $number > 0 && $connections->beginTransaction(); }