modify
This commit is contained in:
@@ -41,7 +41,8 @@ class Connection extends Component
|
|||||||
|
|
||||||
public int $timeout = 1900;
|
public int $timeout = 1900;
|
||||||
|
|
||||||
public int $maxNumber = 200;
|
public int $maxNumber = 30;
|
||||||
|
public int $minNumber = 10;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var bool
|
* @var bool
|
||||||
|
|||||||
@@ -25,89 +25,92 @@ class DatabasesProviders extends Providers
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Application $application
|
* @param Application $application
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onImport(Application $application)
|
public function onImport(Application $application)
|
||||||
{
|
{
|
||||||
$application->set('db', $this);
|
$application->set('db', $this);
|
||||||
|
|
||||||
Event::on(Event::SERVER_TASK_START, [$this, 'createPool']);
|
Event::on(Event::SERVER_TASK_START, [$this, 'createPool']);
|
||||||
Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
|
Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return Connection
|
* @return Connection
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get($name): Connection
|
public function get($name): Connection
|
||||||
{
|
{
|
||||||
$application = Snowflake::app();
|
$application = Snowflake::app();
|
||||||
if ($application->has('databases.' . $name)) {
|
if ($application->has('databases.' . $name)) {
|
||||||
return $application->get('databases.' . $name);
|
return $application->get('databases.' . $name);
|
||||||
}
|
}
|
||||||
$config = $this->getConfig($name);
|
$config = $this->getConfig($name);
|
||||||
return $application->set('databases.' . $name, [
|
|
||||||
'class' => Connection::class,
|
$max = Config::get('databases.pool.max', 30);
|
||||||
'id' => $config['id'],
|
return $application->set('databases.' . $name, [
|
||||||
'cds' => $config['cds'],
|
'class' => Connection::class,
|
||||||
'username' => $config['username'],
|
'id' => $config['id'],
|
||||||
'password' => $config['password'],
|
'cds' => $config['cds'],
|
||||||
'tablePrefix' => $config['tablePrefix'],
|
'username' => $config['username'],
|
||||||
'maxNumber' => $config['maxNumber'],
|
'password' => $config['password'],
|
||||||
'database' => $config['database'],
|
'tablePrefix' => $config['tablePrefix'],
|
||||||
'charset' => $config['charset'] ?? 'utf8mb4',
|
'maxNumber' => $max,
|
||||||
'slaveConfig' => $config['slaveConfig']
|
'database' => $config['database'],
|
||||||
]);
|
'charset' => $config['charset'] ?? 'utf8mb4',
|
||||||
}
|
'slaveConfig' => $config['slaveConfig']
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function createPool()
|
public function createPool()
|
||||||
{
|
{
|
||||||
$databases = Config::get('databases', []);
|
$databases = Config::get('databases.connections', []);
|
||||||
if (empty($databases)) {
|
if (empty($databases)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$application = Snowflake::app();
|
|
||||||
foreach ($databases as $name => $database) {
|
|
||||||
|
|
||||||
var_dump($database);
|
$max = Config::get('databases.pool', ['max' => 10, 'min' => 10]);
|
||||||
|
|
||||||
/** @var Connection $connection */
|
$application = Snowflake::app();
|
||||||
$connection = $application->set('databases.' . $name, [
|
foreach ($databases as $name => $database) {
|
||||||
'class' => Connection::class,
|
/** @var Connection $connection */
|
||||||
'id' => $database['id'],
|
$connection = $application->set('databases.' . $name, [
|
||||||
'cds' => $database['cds'],
|
'class' => Connection::class,
|
||||||
'username' => $database['username'],
|
'id' => $database['id'],
|
||||||
'password' => $database['password'],
|
'cds' => $database['cds'],
|
||||||
'tablePrefix' => $database['tablePrefix'],
|
'username' => $database['username'],
|
||||||
|
'password' => $database['password'],
|
||||||
|
'tablePrefix' => $database['tablePrefix'],
|
||||||
'database' => $database['database'],
|
'database' => $database['database'],
|
||||||
'maxNumber' => $database['maxNumber'],
|
'maxNumber' => $max['max'],
|
||||||
'charset' => $database['charset'] ?? 'utf8mb4',
|
'minNumber' => $max['min'],
|
||||||
'slaveConfig' => $database['slaveConfig']
|
'charset' => $database['charset'] ?? 'utf8mb4',
|
||||||
]);
|
'slaveConfig' => $database['slaveConfig']
|
||||||
$connection->fill();
|
]);
|
||||||
}
|
$connection->fill();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
public function getConfig($name): mixed
|
public function getConfig($name): mixed
|
||||||
{
|
{
|
||||||
return Config::get('databases.' . $name,null, true);
|
return Config::get('databases.' . $name, null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,7 +125,9 @@ class Connection extends Pool
|
|||||||
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
|
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
|
||||||
return $pdo;
|
return $pdo;
|
||||||
}
|
}
|
||||||
|
/** @var PDO $connections */
|
||||||
$connections = $this->getFromChannel($coroutineName, $config);
|
$connections = $this->getFromChannel($coroutineName, $config);
|
||||||
|
$connections->query('use `' . $config['database'].'`');
|
||||||
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
|
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
|
||||||
$number > 0 && $connections->beginTransaction();
|
$number > 0 && $connections->beginTransaction();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user