From 51b0212527f1ee3ce7a1de0cfdb64be5bfcd9988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 16 Aug 2021 18:24:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Pool/Connection.php | 6 ++- System/Pool/Pool.php | 95 +++----------------------------------- System/Pool/Redis.php | 15 +++--- 3 files changed, 18 insertions(+), 98 deletions(-) diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index 4853dae0..8024936e 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -4,13 +4,13 @@ declare(strict_types=1); namespace Kiri\Pool; use Closure; +use Database\Mysql\PDO; use Exception; use HttpServer\Http\Context; use Kiri\Abstracts\Component; use Kiri\Kiri; use Swoole\Error; use Throwable; -use Database\Mysql\PDO; /** * Class Connection @@ -105,7 +105,9 @@ class Connection extends Component public function create($coroutineName, $config): Closure { 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' + ]); }; } diff --git a/System/Pool/Pool.php b/System/Pool/Pool.php index 920ca8a3..ab5be537 100644 --- a/System/Pool/Pool.php +++ b/System/Pool/Pool.php @@ -27,87 +27,11 @@ class Pool extends Component public int $max = 60; - public int $creates = -1; - private array $_times = []; 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 $retain_number @@ -152,9 +76,6 @@ class Pool extends Component if (Coroutine::getCid() === -1) { return; } - if ($this->creates === -1) { - $this->creates = Timer::tick(60000, [$this, 'Heartbeat_detection']); - } static::$_connections[$name] = new Channel($max); $this->max = $max; } @@ -174,18 +95,16 @@ class Pool extends Component if (static::$_connections[$name]->errCode == SWOOLE_CHANNEL_CLOSED) { throw new Exception('Channel is Close.'); } - if ($this->creates === -1) { - $this->creates = Timer::tick(60000, [$this, 'Heartbeat_detection']); - } return static::$_connections[$name]; } - /** - * @param $name - * @return array - * @throws Exception - */ + /** + * @param $name + * @param $callback + * @return array + * @throws ConfigException + */ public function get($name, $callback): mixed { if (Coroutine::getCid() === -1) { @@ -206,7 +125,7 @@ class Pool extends Component /** * @param $name * @return bool - * @throws \Kiri\Exception\ConfigException + * @throws ConfigException */ public function isNull($name): bool { diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index 748c30e3..0deddf52 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Kiri\Pool; +use Closure; use Exception; use HttpServer\Http\Context; use Redis as SRedis; @@ -42,14 +43,12 @@ class Redis extends Component } - /** - * @param string $name - * @param mixed $config - * @return SRedis - * @throws RedisConnectException - * @throws Exception - */ - public function create(string $name, mixed $config): \Closure + /** + * @param string $name + * @param mixed $config + * @return Closure + */ + public function create(string $name, mixed $config): Closure { return static function () use ($name, $config) { if (Coroutine::getCid() === -1) {