This commit is contained in:
2021-02-20 13:35:17 +08:00
parent b140054ce9
commit 9ce45e39e8
6 changed files with 23 additions and 20 deletions
+4 -4
View File
@@ -98,8 +98,8 @@ class Connection extends Component
public function getConnect($sql = NULL): PDO
{
$connections = $this->connections();
$connections->initConnections($this->cds, true, $this->maxNumber);
$connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber);
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
$connections->setTimeout($this->timeout);
return $this->getPdo($sql);
@@ -113,8 +113,8 @@ class Connection extends Component
public function fill()
{
$connections = $this->connections();
$connections->initConnections($this->cds, true, $this->maxNumber);
$connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber);
$connections->initConnections('mysql', $this->cds, true, $this->maxNumber);
$connections->initConnections('mysql', $this->slaveConfig['cds'], false, $this->maxNumber);
}
+7 -5
View File
@@ -30,13 +30,14 @@ abstract class Pool extends Component
use Timeout;
/**
* @param $driver
* @param $name
* @param false $isMaster
* @param int $max
*/
public function initConnections($name, $isMaster = false, $max = 60)
public function initConnections($driver, $name, $isMaster = false, $max = 60)
{
$name = $this->name($name, $isMaster);
$name = $this->name($driver, $name, $isMaster);
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
return;
}
@@ -71,16 +72,17 @@ abstract class Pool extends Component
/**
* @param $driver
* @param $cds
* @param false $isMaster
* @return string
*/
#[Pure] public function name($cds, $isMaster = false): string
#[Pure] public function name($driver, $cds, $isMaster = false): string
{
if ($isMaster === true) {
return hash('sha256', $cds . 'master');
return $driver . ':' . hash('sha256', $cds . 'master');
} else {
return hash('sha256', $cds . 'slave');
return $driver . ':' . hash('sha256', $cds . 'slave');
}
}
+1 -1
View File
@@ -57,7 +57,7 @@ class Redis extends Component
$length = env('REDIS.POOL_LENGTH', 100);
$connections->initConnections('redis:' . $name, true, $length);
$connections->initConnections('redis', 'redis:' . $name, true, $length);
}
+7 -7
View File
@@ -52,7 +52,7 @@ class Connection extends Pool
*/
public function inTransaction($cds): bool
{
return Context::getContext('begin_' . $this->name($cds, true)) == 0;
return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0;
}
/**
@@ -60,7 +60,7 @@ class Connection extends Pool
*/
public function beginTransaction($coroutineName)
{
$coroutineName = $this->name($coroutineName, true);
$coroutineName = $this->name('mysql', $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) {
Context::setContext('begin_' . $coroutineName, 0);
}
@@ -79,7 +79,7 @@ class Connection extends Pool
*/
public function commit($coroutineName)
{
$coroutineName = $this->name($coroutineName, true);
$coroutineName = $this->name('mysql', $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) {
return;
}
@@ -104,7 +104,7 @@ class Connection extends Pool
*/
private function getIndex($name, $isMaster = false): array
{
return [Coroutine::getCid(), $this->name($name, $isMaster)];
return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)];
}
/**
@@ -112,7 +112,7 @@ class Connection extends Pool
*/
public function rollback($coroutineName)
{
$coroutineName = $this->name($coroutineName, true);
$coroutineName = $this->name('mysql', $coroutineName, true);
if (!Context::hasContext('begin_' . $coroutineName)) {
return;
}
@@ -136,7 +136,7 @@ class Connection extends Pool
*/
public function getConnection(array $config, $isMaster = false): mixed
{
$coroutineName = $this->name($config['cds'], $isMaster);
$coroutineName = $this->name('mysql', $config['cds'], $isMaster);
if (!isset($this->hasCreate[$coroutineName])) {
$this->hasCreate[$coroutineName] = 0;
}
@@ -211,7 +211,7 @@ class Connection extends Pool
*/
public function release($coroutineName, $isMaster)
{
$coroutineName = $this->name($coroutineName, $isMaster);
$coroutineName = $this->name('mysql', $coroutineName, $isMaster);
if (!$this->hasClient($coroutineName)) {
return;
}
+3 -3
View File
@@ -42,7 +42,7 @@ class Redis extends Pool
public function getConnection(array $config, $isMaster = false): mixed
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis:' . $name, $isMaster);
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (($redis = Context::getContext($coroutineName)) instanceof \Redis) {
return $redis;
}
@@ -105,7 +105,7 @@ class Redis extends Pool
public function release(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis:' . $name, $isMaster);
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) {
return;
}
@@ -121,7 +121,7 @@ class Redis extends Pool
public function destroy(array $config, $isMaster = false)
{
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis:' . $name, $isMaster);
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (!Context::hasContext($coroutineName)) {
return;
}
+1
View File
@@ -40,6 +40,7 @@ trait Timeout
{
$channels = $this->getChannels();
foreach ($channels as $name => $channel) {
$this->debug('release ' . $name);
$this->pop($channel, $name, $retain_number);
}
if ($retain_number == 0) {