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])) { if (!isset(static::$_items[$name])) {
static::$_items[$name] = new Channel(Config::get('databases.pool.max', 10)); 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]; 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 * @param string $name
* @return bool * @return bool
+2 -2
View File
@@ -55,9 +55,9 @@ class Redis extends Component
$config = $this->get_config(); $config = $this->get_config();
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $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);
} }
+5 -19
View File
@@ -9,6 +9,7 @@ use Exception;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use Redis as SRedis; use Redis as SRedis;
use Snowflake\Abstracts\Pool; use Snowflake\Abstracts\Pool;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\RedisConnectException; use Snowflake\Exception\RedisConnectException;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Runtime; 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 mixed $config
* @param bool $isMaster * @param bool $isMaster
@@ -53,7 +41,7 @@ class Redis extends Pool
*/ */
public function get(mixed $config, bool $isMaster = false): mixed 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)) { if (!Context::hasContext($coroutineName)) {
return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config)); return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
} }
@@ -96,17 +84,16 @@ class Redis extends Pool
/** /**
* @param array $config * @param array $config
* @param bool $isMaster * @param bool $isMaster
* @throws ConfigException
*/ */
public function release(array $config, bool $isMaster = false) 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)) { if (!Context::hasContext($coroutineName)) {
return; return;
} }
$this->push($coroutineName, Context::getContext($coroutineName)); $this->push($coroutineName, Context::getContext($coroutineName));
Context::remove($coroutineName);
$this->lastTime = time();
} }
/** /**
@@ -116,7 +103,7 @@ class Redis extends Pool
*/ */
public function destroy(array $config, bool $isMaster = false) 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)) { if (Context::hasContext($coroutineName)) {
$this->decrement($coroutineName); $this->decrement($coroutineName);
} }
@@ -150,5 +137,4 @@ class Redis extends Pool
} }
} }