This commit is contained in:
2023-02-07 16:46:13 +08:00
parent a9472ff0a3
commit 4f341594b5
4 changed files with 27 additions and 46 deletions
+1 -3
View File
@@ -96,9 +96,7 @@ class Connection extends Component
*/
public function get(mixed $config, bool $isMaster = false): ?\PDO
{
$minx = Config::get('databases.pool.min', 1);
return $this->pool->get($config['cds'] . ($isMaster ? 'master' : 'slave'), $this->generate($config), $minx);
return $this->pool->get($config['cds'] . ($isMaster ? 'master' : 'slave'), $this->generate($config));
}
+21 -39
View File
@@ -24,9 +24,6 @@ class Pool extends Component
/** @var array<PoolQueue> */
private static array $_connections = [];
public int $max = 60;
/**
* @var WorkerStatus
*/
@@ -112,18 +109,14 @@ class Pool extends Component
/**
* @param $name
* @param int $max
* @throws ConfigException
*/
public function initConnections($name, int $max = 60)
{
if (isset(static::$_connections[$name])) {
$value = static::$_connections[$name];
if ($value instanceof PoolQueue) {
$channel = static::$_connections[$name] ?? null;
if (($channel instanceof PoolQueue) && !$channel->isClose()) {
return;
}
}
$this->newChannel($name, $max);
$this->max = $max;
static::$_connections[$name] = new PoolQueue($max);
}
@@ -135,37 +128,25 @@ class Pool extends Component
*/
public function channel($name): PoolQueue
{
if (!isset(static::$_connections[$name])) {
$this->newChannel($name);
$channel = static::$_connections[$name] ?? null;
if (!($channel instanceof PoolQueue) ) {
throw new Exception('Channel is not exists.');
}
if (static::$_connections[$name]->isClose()) {
if ($channel->isClose()) {
throw new Exception('Channel is Close.');
}
return static::$_connections[$name];
}
/**
* @throws ConfigException
*/
private function newChannel($name, $max = null)
{
if ($max == null) {
$max = Config::get('databases.pool.max', 10);
}
static::$_connections[$name] = new PoolQueue($max);
return $channel;
}
/**
* @param $name
* @param $callback
* @param $minx
* @return array
* @throws ConfigException
* @throws Exception
*/
public function get($name, $callback, $minx): mixed
public function get($name, $callback): mixed
{
$channel = $this->channel($name);
if (!$channel->isEmpty()) {
@@ -217,11 +198,12 @@ class Pool extends Component
*/
public function hasItem(string $name): bool
{
if (isset(static::$_connections[$name])) {
return !static::$_connections[$name]->isEmpty();
}
$channel = static::$_connections[$name] ?? null;
if (!($channel instanceof PoolQueue) || $channel->isClose()) {
return false;
}
return !$channel->isEmpty();
}
/**
@@ -230,10 +212,11 @@ class Pool extends Component
*/
public function size(string $name): int
{
if (!isset(static::$_connections[$name])) {
$channel = static::$_connections[$name] ?? null;
if (!($channel instanceof PoolQueue) || $channel->isClose()) {
return 0;
}
return static::$_connections[$name]->length();
return $channel->length();
}
@@ -248,7 +231,6 @@ class Pool extends Component
if (!$channel->isFull()) {
$channel->push($client);
}
unset($client);
}
@@ -258,17 +240,17 @@ class Pool extends Component
*/
public function clean(string $name)
{
if (!isset(static::$_connections[$name])) {
$channel = static::$_connections[$name] ?? null;
if (!($channel instanceof PoolQueue) || $channel->isClose()) {
return;
}
while (static::$_connections[$name]->length() > 0) {
$client = static::$_connections[$name]->pop();
while ($channel->length() > 0) {
$client = $channel->pop();
if ($client instanceof StopHeartbeatCheck) {
$client->stopHeartbeatCheck();
}
}
static::$_connections[$name] = null;
unset(static::$_connections[$name]);
$channel->close();
}
+2 -2
View File
@@ -82,7 +82,7 @@ class Helper implements StopHeartbeatCheck
/**
* @return \Redis
* @throws RedisConnectException
* @throws Exception
* @throws RedisException
*/
public function _pdo(): \Redis
@@ -96,7 +96,7 @@ class Helper implements StopHeartbeatCheck
/**
* @return \Redis
* @throws RedisConnectException
* @throws Exception
*/
private function newClient(): \Redis
{
+2 -1
View File
@@ -91,6 +91,7 @@ class Redis extends Component
* @param $key
* @param int $timeout
* @return bool
* @throws \RedisException
*/
public function waite($key, int $timeout = 5): bool
{
@@ -189,7 +190,7 @@ SCRIPT;
$config = $this->get_config();
return $this->pool->get($config['host'], static function () use ($config) {
return new Helper($config);
}, 10);
});
}