改名
This commit is contained in:
@@ -4,13 +4,13 @@ declare(strict_types=1);
|
|||||||
namespace Kiri\Pool;
|
namespace Kiri\Pool;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use Database\Mysql\PDO;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Http\Context;
|
use HttpServer\Http\Context;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Swoole\Error;
|
use Swoole\Error;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use Database\Mysql\PDO;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Connection
|
* Class Connection
|
||||||
@@ -105,7 +105,9 @@ class Connection extends Component
|
|||||||
public function create($coroutineName, $config): Closure
|
public function create($coroutineName, $config): Closure
|
||||||
{
|
{
|
||||||
return static function () use ($coroutineName, $config) {
|
return static function () use ($coroutineName, $config) {
|
||||||
return new PDO($config['database'], $config['cds'], $config['username'], $config['password'], $config['charset'] ?? 'utf8mb4');
|
return Kiri::getDi()->newObject(PDO::class, [
|
||||||
|
$config['database'], $config['cds'], $config['username'], $config['password'], $config['charset'] ?? 'utf8mb4'
|
||||||
|
]);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-84
@@ -27,87 +27,11 @@ class Pool extends Component
|
|||||||
|
|
||||||
public int $max = 60;
|
public int $max = 60;
|
||||||
|
|
||||||
public int $creates = -1;
|
|
||||||
|
|
||||||
private array $_times = [];
|
private array $_times = [];
|
||||||
|
|
||||||
use Alias;
|
use Alias;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
* @throws ConfigException
|
|
||||||
*/
|
|
||||||
private function getClearTime(): array
|
|
||||||
{
|
|
||||||
$firstClear = Config::get('pool.clear.start', 600);
|
|
||||||
$lastClear = Config::get('pool.clear.end', 300);
|
|
||||||
return [$firstClear, $lastClear];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function Heartbeat_detection($ticker)
|
|
||||||
{
|
|
||||||
if (env('state') == 'exit') {
|
|
||||||
Timer::clear($this->creates);
|
|
||||||
foreach (static::$_connections as $channel) {
|
|
||||||
$this->flush($channel, 0);
|
|
||||||
$channel->close();
|
|
||||||
}
|
|
||||||
static::$_connections = [];
|
|
||||||
$this->creates = -1;
|
|
||||||
} else {
|
|
||||||
$this->heartbeat_flush();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function heartbeat_flush()
|
|
||||||
{
|
|
||||||
$num = [];
|
|
||||||
$total = 0;
|
|
||||||
$min = Config::get('databases.pool.min', 1);
|
|
||||||
foreach (static::$_connections as $key => $channel) {
|
|
||||||
if (!isset($num[$key])) {
|
|
||||||
$num[$key] = 0;
|
|
||||||
}
|
|
||||||
if (time() - ($this->_times[$key] ?? time()) > 120) {
|
|
||||||
$this->flush($channel, 0);
|
|
||||||
} else if ($channel->length() > $min) {
|
|
||||||
$this->flush($channel, $min);
|
|
||||||
}
|
|
||||||
$num[$key] += ($length = $channel->length());
|
|
||||||
$total += $length;
|
|
||||||
}
|
|
||||||
$this->clear($total, $num);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $total
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
private function clear($total, $num)
|
|
||||||
{
|
|
||||||
write(var_export($num, true), 'connections');
|
|
||||||
if ($total >= 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Timer::clear($this->creates);
|
|
||||||
if (Kiri::isWorker() || Kiri::isTask()) {
|
|
||||||
$this->debug('Worker #' . env('worker') . ' clear time tick.');
|
|
||||||
}
|
|
||||||
$this->creates = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $channel
|
* @param $channel
|
||||||
* @param $retain_number
|
* @param $retain_number
|
||||||
@@ -152,9 +76,6 @@ class Pool extends Component
|
|||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($this->creates === -1) {
|
|
||||||
$this->creates = Timer::tick(60000, [$this, 'Heartbeat_detection']);
|
|
||||||
}
|
|
||||||
static::$_connections[$name] = new Channel($max);
|
static::$_connections[$name] = new Channel($max);
|
||||||
$this->max = $max;
|
$this->max = $max;
|
||||||
}
|
}
|
||||||
@@ -174,17 +95,15 @@ class Pool extends Component
|
|||||||
if (static::$_connections[$name]->errCode == SWOOLE_CHANNEL_CLOSED) {
|
if (static::$_connections[$name]->errCode == SWOOLE_CHANNEL_CLOSED) {
|
||||||
throw new Exception('Channel is Close.');
|
throw new Exception('Channel is Close.');
|
||||||
}
|
}
|
||||||
if ($this->creates === -1) {
|
|
||||||
$this->creates = Timer::tick(60000, [$this, 'Heartbeat_detection']);
|
|
||||||
}
|
|
||||||
return static::$_connections[$name];
|
return static::$_connections[$name];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
|
* @param $callback
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
public function get($name, $callback): mixed
|
public function get($name, $callback): mixed
|
||||||
{
|
{
|
||||||
@@ -206,7 +125,7 @@ class Pool extends Component
|
|||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Kiri\Exception\ConfigException
|
* @throws ConfigException
|
||||||
*/
|
*/
|
||||||
public function isNull($name): bool
|
public function isNull($name): bool
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Kiri\Pool;
|
namespace Kiri\Pool;
|
||||||
|
|
||||||
|
|
||||||
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Http\Context;
|
use HttpServer\Http\Context;
|
||||||
use Redis as SRedis;
|
use Redis as SRedis;
|
||||||
@@ -45,11 +46,9 @@ class Redis extends Component
|
|||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param mixed $config
|
* @param mixed $config
|
||||||
* @return SRedis
|
* @return Closure
|
||||||
* @throws RedisConnectException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public function create(string $name, mixed $config): \Closure
|
public function create(string $name, mixed $config): Closure
|
||||||
{
|
{
|
||||||
return static function () use ($name, $config) {
|
return static function () use ($name, $config) {
|
||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
|
|||||||
Reference in New Issue
Block a user