This commit is contained in:
2021-07-05 15:49:37 +08:00
parent 74bcdf257e
commit 1a012150ce
5 changed files with 317 additions and 303 deletions
+3 -3
View File
@@ -107,11 +107,11 @@ class Connection extends Component
public function fill() public function fill()
{ {
$connections = $this->connections(); $connections = $this->connections();
$pool = Config::get('databases.pool.max',10); $pool = Config::get('databases.pool.max', 10);
$connections->initConnections('mysql', $this->cds, true, $pool); $connections->initConnections($this->cds, true, $pool);
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) { if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $pool); $connections->initConnections($this->slaveConfig['cds'], false, $pool);
} }
} }
+1 -2
View File
@@ -53,11 +53,10 @@ class Redis extends Component
$connections = Snowflake::app()->getRedisFromPool(); $connections = Snowflake::app()->getRedisFromPool();
$config = $this->get_config(); $config = $this->get_config();
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$length = (int)env('REDIS.POOL_LENGTH', 100); $length = (int)env('REDIS.POOL_LENGTH', 100);
$connections->initConnections('redis', $name, true, $length); $connections->initConnections('Redis:' . $config['host'], true, $length);
} }
+1 -14
View File
@@ -106,18 +106,6 @@ class ClientsPool extends Component
} }
/**
* @param $name
*/
protected function clearCreateLog($name): void
{
if (!isset(static::$hasCreate[$name])) {
return;
}
static::$hasCreate[$name] = 0;
}
/** /**
* @param Channel $channel * @param Channel $channel
* @param $name * @param $name
@@ -140,12 +128,11 @@ class ClientsPool extends Component
/** /**
* @param $driver
* @param $name * @param $name
* @param false $isMaster * @param false $isMaster
* @param int $max * @param int $max
*/ */
public function initConnections($driver, $name, bool $isMaster = false, int $max = 60) public function initConnections($name, bool $isMaster = false, int $max = 60)
{ {
$name = $this->name($name, $isMaster); $name = $this->name($name, $isMaster);
if (isset(static::$_connections[$name]) && static::$_connections[$name] instanceof Channel) { if (isset(static::$_connections[$name]) && static::$_connections[$name] instanceof Channel) {
+16
View File
@@ -113,11 +113,15 @@ class Connection extends Component
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
return $pdo; return $pdo;
} }
if (Coroutine::getCid() === -1) {
$connections = $this->createClient($coroutineName, $config);
} else {
/** @var PDO $connections */ /** @var PDO $connections */
$connections = $this->getPool()->getFromChannel($coroutineName); $connections = $this->getPool()->getFromChannel($coroutineName);
if (empty($connections)) { if (empty($connections)) {
$connections = $this->createClient($coroutineName, $config); $connections = $this->createClient($coroutineName, $config);
} }
}
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
$number > 0 && $connections->beginTransaction(); $number > 0 && $connections->beginTransaction();
} }
@@ -125,6 +129,18 @@ class Connection extends Component
} }
/**
* @param $name
* @param $isMaster
* @param $max
* @throws Exception
*/
public function initConnections($name, $isMaster, $max)
{
$this->getPool()->initConnections($name, $isMaster, $max);
}
/** /**
* @param string $name * @param string $name
* @param mixed $config * @param mixed $config
+12
View File
@@ -126,4 +126,16 @@ class Redis extends Component
} }
/**
* @param $name
* @param $isMaster
* @param $max
* @throws Exception
*/
public function initConnections($name, $isMaster, $max)
{
$this->getPool()->initConnections($name, $isMaster, $max);
}
} }