This commit is contained in:
2021-07-05 11:23:57 +08:00
parent ee23b78872
commit 2ca845a65c
3 changed files with 13 additions and 37 deletions
+3 -13
View File
@@ -174,6 +174,9 @@ abstract class Pool extends Component
{
if (!isset(static::$_items[$name])) {
static::$_items[$name] = new Channel(Config::get('databases.pool.max', 10));
if ($this->creates === -1) {
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection'], $name);
}
}
return static::$_items[$name];
}
@@ -250,19 +253,6 @@ abstract class Pool extends Component
}
/**
* @param string $name
* @return bool
*/
public function canCreate(string $name): bool
{
if (!isset(static::$hasCreate[$name])) {
static::$hasCreate[$name] = 0;
}
return static::$hasCreate[$name] < $this->max;
}
/**
* @param string $name
* @return bool
+2 -2
View File
@@ -55,9 +55,9 @@ class Redis extends Component
$config = $this->get_config();
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$length = env('REDIS.POOL_LENGTH', 100);
$length = (int)env('REDIS.POOL_LENGTH', 100);
$connections->initConnections('redis', 'redis:' . $name, true, $length);
$connections->initConnections('redis', $name, true, $length);
}
+8 -22
View File
@@ -9,6 +9,7 @@ use Exception;
use HttpServer\Http\Context;
use Redis as SRedis;
use Snowflake\Abstracts\Pool;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\RedisConnectException;
use Swoole\Coroutine;
use Swoole\Runtime;
@@ -32,19 +33,6 @@ class Redis extends Pool
}
/**
* @param string $name
* @return bool
*/
public function canCreate(string $name): bool
{
if (!isset(static::$hasCreate[$name])) {
static::$hasCreate[$name] = 0;
}
return static::$hasCreate[$name] >= $this->max;
}
/**
* @param mixed $config
* @param bool $isMaster
@@ -53,9 +41,9 @@ class Redis extends Pool
*/
public function get(mixed $config, bool $isMaster = false): mixed
{
$coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster);
$coroutineName = $this->name('redis', $config['host'], $isMaster);
if (!Context::hasContext($coroutineName)) {
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
}
return Context::getContext($coroutineName);
}
@@ -96,17 +84,16 @@ class Redis extends Pool
/**
* @param array $config
* @param bool $isMaster
* @throws ConfigException
*/
public function release(array $config, bool $isMaster = false)
{
$coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster);
$coroutineName = $this->name('redis', $config['host'], $isMaster);
if (!Context::hasContext($coroutineName)) {
return;
}
$this->push($coroutineName, Context::getContext($coroutineName));
Context::remove($coroutineName);
$this->lastTime = time();
}
/**
@@ -116,12 +103,12 @@ class Redis extends Pool
*/
public function destroy(array $config, bool $isMaster = false)
{
$coroutineName = $this->name('redis', 'redis:' . $config['host'], $isMaster);
$coroutineName = $this->name('redis', $config['host'], $isMaster);
if (Context::hasContext($coroutineName)) {
$this->decrement($coroutineName);
}
Context::remove($coroutineName);
$this->flush(0);
Context::remove($coroutineName);
$this->flush(0);
}
/**
@@ -150,5 +137,4 @@ class Redis extends Pool
}
}