From b2066bfad73e5854f5b6ef10558e972e60cf8bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 19 Apr 2023 10:51:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Kiri.php | 10 ++ function.php | 8 +- kiri-engine/Pool/Connection.php | 193 -------------------------------- kiri-engine/Pool/Pool.php | 10 +- kiri-engine/Pool/PoolQueue.php | 110 ------------------ kiri-engine/Redis/Redis.php | 27 ++--- 6 files changed, 30 insertions(+), 328 deletions(-) delete mode 100644 kiri-engine/Pool/Connection.php delete mode 100644 kiri-engine/Pool/PoolQueue.php diff --git a/Kiri.php b/Kiri.php index 688c2933..ad6c9298 100644 --- a/Kiri.php +++ b/Kiri.php @@ -73,6 +73,16 @@ class Kiri } + /** + * @return \Kiri\Pool\Pool + * @throws ReflectionException + */ + public static function getPool(): \Kiri\Pool\Pool + { + return static::getDi()->get(\Kiri\Pool\Pool::class); + } + + /** * @param $prefix * @return void diff --git a/function.php b/function.php index 4a8838c5..205cf15f 100644 --- a/function.php +++ b/function.php @@ -843,13 +843,13 @@ if (!function_exists('on')) { /** * @param $name * @param $callback - * @param bool $isAppend - * @throws Exception + * @param int $index + * @throws */ - function on($name, $callback, bool $isAppend = TRUE): void + function on($name, $callback, int $index = 0): void { $pro = di(EventProvider::class); - $pro->on($name, $callback, 0); + $pro->on($name, $callback, $index); } } diff --git a/kiri-engine/Pool/Connection.php b/kiri-engine/Pool/Connection.php deleted file mode 100644 index 5a1712f1..00000000 --- a/kiri-engine/Pool/Connection.php +++ /dev/null @@ -1,193 +0,0 @@ -inTransaction(); - } - return false; - } - - - /** - * @param $coroutineName - * @throws Exception - */ - public function beginTransaction($coroutineName) - { - $connection = $this->get($coroutineName); - if ($connection instanceof PDO) { - $connection->beginTransaction(); - } - } - - /** - * @param $coroutineName - * @throws Exception - */ - public function commit($coroutineName) - { - $connection = Context::get($coroutineName); - if ($connection instanceof PDO) { - $connection->commit(); - } - } - - - /** - * @param $coroutineName - * @throws Exception - */ - public function rollback($coroutineName) - { - $connection = Context::get($coroutineName); - if ($connection instanceof PDO) { - $connection->rollBack(); - } - } - - - /** - * @param string $cds - * @return PDO|bool|null - * @throws Exception - */ - public function get(string $cds): null|PDO|bool - { - return $this->pool->get($cds); - } - - - /** - * @param string $name - * @return array - */ - public function check(string $name): array - { - return $this->pool->check($name); - } - - - /** - * @param string $name - * @param PDO $PDO - * @return void - * @throws ConfigException - */ - public function addItem(string $name, PDO $PDO): void - { - $this->pool->push($name, $PDO); - } - - - /** - * @param $coroutineName - * @throws Kiri\Exception\ConfigException - * @throws Exception - */ - public function release($coroutineName) - { - $client = Context::get($coroutineName); - if (!($client instanceof PDO) || $client->inTransaction()) { - return; - } - - $this->pool->push($coroutineName, $client); - Context::remove($coroutineName); - } - - - /** - * @throws Exception - */ - public function flush($coroutineName, $minNumber = 1) - { - $this->pool->flush($coroutineName, $minNumber); - } - - - /** - * batch release - * @throws Exception - */ - public function connection_clear($name) - { - $this->pool->clean($name); - } - - - /** - * @param string $name - * @param mixed $client - * @return bool - * @throws Exception - */ - public function checkCanUse(string $name, mixed $client): bool - { - try { - if (empty($client) || !($client instanceof PDO)) { - $result = false; - } else { - $result = true; - } - } catch (Error|Throwable $exception) { - $result = addError($exception, 'mysql'); - } finally { - return $result; - } - } - - - /** - * @param $coroutineName - * @throws Exception - */ - public function disconnect($coroutineName) - { - Context::remove($coroutineName); - $this->pool->clean($coroutineName); - } - - -} diff --git a/kiri-engine/Pool/Pool.php b/kiri-engine/Pool/Pool.php index a745316d..2226587a 100644 --- a/kiri-engine/Pool/Pool.php +++ b/kiri-engine/Pool/Pool.php @@ -26,7 +26,7 @@ class Pool extends Component * @param $retain_number * @throws Exception */ - public function flush($name, $retain_number) + public function flush($name, $retain_number): void { if ($this->hasChannel($name)) { $channel = $this->channel($name); @@ -90,7 +90,7 @@ class Pool extends Component * @param int $max * @param \Closure $closure */ - public function initConnections($name, int $max, \Closure $closure) + public function initConnections($name, int $max, \Closure $closure): void { if (!isset($this->_connections[$name])) { $this->_connections[$name] = new PoolItem($max, $closure); @@ -189,7 +189,7 @@ class Pool extends Component * @param mixed $client * @throws ConfigException */ - public function push(string $name, mixed $client) + public function push(string $name, mixed $client): void { $this->channel($name)->push($client); } @@ -211,7 +211,7 @@ class Pool extends Component * @param string $name * @throws Exception */ - public function clean(string $name) + public function clean(string $name): void { $channel = $this->_connections[$name] ?? null; if ($channel === null) { @@ -223,7 +223,7 @@ class Pool extends Component /** - * @return PoolQueue[] + * @return PoolItem[] */ protected function channels(): array { diff --git a/kiri-engine/Pool/PoolQueue.php b/kiri-engine/Pool/PoolQueue.php deleted file mode 100644 index ed7b89c5..00000000 --- a/kiri-engine/Pool/PoolQueue.php +++ /dev/null @@ -1,110 +0,0 @@ -queue = new Channel($this->max); - } else { - $this->queue = new SplQueue($this->max); - } - } - - - /** - * @return bool - */ - public function isEmpty(): bool - { - return $this->queue->isEmpty(); - } - - /** - * @param mixed $data - * @param float $timeout - * @return bool - */ - public function push(mixed $data, float $timeout = -1): bool - { - if ($this->isFull()) { - return false; - } - if (!$this->isClose()) { - return $this->queue->push($data, $timeout); - } - return false; - } - - - /** - * @param float $timeout - * @return mixed - */ - public function pop(float $timeout = 0): mixed - { - return $this->queue->pop($timeout); - } - - - /** - * @return array - */ - public function stats(): array - { - return $this->queue->stats(); - } - - /** - * @return bool - */ - public function close(): bool - { - return $this->queue->close(); - } - - - /** - * @return int - */ - public function length(): int - { - return $this->queue->length(); - } - - - /** - * @return bool - */ - public function isFull(): bool - { - return $this->queue->isFull(); - } - - - /** - * @return bool - */ - public function isClose(): bool - { - if ($this->queue instanceof Channel) { - return $this->queue->errCode == SWOOLE_CHANNEL_CLOSED; - } - return false; - } - -} diff --git a/kiri-engine/Redis/Redis.php b/kiri-engine/Redis/Redis.php index 81d70490..ee912113 100644 --- a/kiri-engine/Redis/Redis.php +++ b/kiri-engine/Redis/Redis.php @@ -22,6 +22,8 @@ use Kiri\Server\Events\OnWorkerExit; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; +use RedisException; +use ReflectionException; /** * Class Redis @@ -66,12 +68,8 @@ class Redis extends Component $config = $this->get_config(); $length = Config::get('cache.redis.pool.max', 10); - - $eventProvider = Kiri::getDi()->get(EventProvider::class); - $eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0); - - $pool = Kiri::getDi()->get(Pool::class); - $pool->initConnections($config['host'], $length, static function () use ($config) { + on(OnWorkerExit::class, [$this, 'destroy']); + Kiri::getPool()->initConnections($config['host'], $length, static function () use ($config) { $redis = new \Redis(); if (!$redis->connect($config['host'], $config['port'], $config['timeout'])) { throw new RedisConnectException(sprintf('The Redis Connect %s::%d Fail.', $config['host'], $config['port'])); @@ -113,7 +111,7 @@ class Redis extends Component * @param $key * @param int $timeout * @return bool - * @throws \RedisException + * @throws RedisException */ public function waite($key, int $timeout = 5): bool { @@ -162,12 +160,11 @@ SCRIPT; /** * @return void - * @throws \ReflectionException + * @throws */ public function destroy(): void { - $pool = Kiri::getDi()->get(Pool::class); - $pool->clean($this->host); + Kiri::getPool()->clean($this->host); } @@ -178,7 +175,7 @@ SCRIPT; * @throws ConfigException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface - * @throws \ReflectionException + * @throws ReflectionException */ public function proxy($name, $arguments): mixed { @@ -188,8 +185,7 @@ SCRIPT; } catch (\Throwable $throwable) { $response = addError($throwable, 'redis'); } finally { - $pool = Kiri::getDi()->get(Pool::class); - $pool->push($this->host, $client); + Kiri::getPool()->push($this->host, $client); } return $response; } @@ -198,12 +194,11 @@ SCRIPT; /** * @return \Redis * @throws ConfigException - * @throws \ReflectionException + * @throws ReflectionException */ private function getClient(): \Redis { - $pool = Kiri::getDi()->get(Pool::class); - return $pool->get($this->host); + return Kiri::getPool()->get($this->host); }