From a8eebeb902fafd1ca171f66d3f37de44a1b6c321 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Tue, 4 May 2021 03:07:11 +0800 Subject: [PATCH] modify --- HttpServer/Http/Context.php | 18 +++++++++--------- System/Abstracts/Pool.php | 11 ++++++----- System/Error/Logger.php | 2 +- System/Pool/Redis.php | 6 +++--- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/HttpServer/Http/Context.php b/HttpServer/Http/Context.php index 111cfe1a..63104b22 100644 --- a/HttpServer/Http/Context.php +++ b/HttpServer/Http/Context.php @@ -24,9 +24,9 @@ class Context extends BaseContext */ public static function setContext($id, $context, $key = null): mixed { -// if (!static::inCoroutine()) { -// return static::setStatic($id, $context, $key); -// } + if (Coroutine::getCid() === -1) { + return static::setStatic($id, $context, $key); + } return self::setCoroutine($id, $context, $key); } @@ -106,9 +106,9 @@ class Context extends BaseContext */ public static function getContext($id, $key = null): mixed { -// if (!static::inCoroutine()) { -// return static::loadByStatic($id, $key); -// } + if (Coroutine::getCid() === -1) { + return static::loadByStatic($id, $key); + } return static::loadByContext($id, $key); } @@ -154,7 +154,7 @@ class Context extends BaseContext */ public static function getAllContext(): mixed { - if (static::inCoroutine()) { + if (Coroutine::getCid() === -1) { return Coroutine::getContext() ?? []; } else { return static::$_contents ?? []; @@ -170,7 +170,7 @@ class Context extends BaseContext if (!static::hasContext($id, $key)) { return; } - if (!static::inCoroutine()) { + if (Coroutine::getCid() === -1) { if (!empty($key)) { unset(static::$_contents[$id][$key]); } else { @@ -192,7 +192,7 @@ class Context extends BaseContext */ public static function hasContext($id, $key = null): bool { - if (!static::inCoroutine()) { + if (Coroutine::getCid() === -1) { return static::searchByStatic($id, $key); } return static::searchByCoroutine($id, $key); diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 3b8d595d..807b5453 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -8,6 +8,7 @@ use Exception; use HttpServer\Http\Context; use JetBrains\PhpStorm\Pure; use Snowflake\Exception\ConfigException; +use Swoole\Coroutine; use Swoole\Coroutine\Channel; use Swoole\Timer; @@ -149,7 +150,7 @@ abstract class Pool extends Component if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) { return; } - if (!Context::inCoroutine()) { + if (Coroutine::getCid() === -1) { return; } $this->_items[$name] = new Channel((int)$max); @@ -165,7 +166,7 @@ abstract class Pool extends Component */ protected function getFromChannel($name, mixed $callback): mixed { - if (!Context::inCoroutine()) { + if (Coroutine::getCid() === -1) { return $this->createClient($name, $callback); } if (!isset($this->_items[$name])) { @@ -272,7 +273,7 @@ abstract class Pool extends Component */ public function size(string $name): mixed { - if (!Context::inCoroutine()) { + if (Coroutine::getCid() === -1) { return 0; } if (!isset($this->_items[$name])) { @@ -288,7 +289,7 @@ abstract class Pool extends Component */ public function push(string $name, mixed $client) { - if (!Context::inCoroutine()) { + if (Coroutine::getCid() === -1) { return; } if (!isset($this->_items[$name])) { @@ -307,7 +308,7 @@ abstract class Pool extends Component */ public function clean(string $name) { - if (!Context::inCoroutine() || !isset($this->_items[$name])) { + if (Coroutine::getCid() === -1 || !isset($this->_items[$name])) { return; } $channel = $this->_items[$name]; diff --git a/System/Error/Logger.php b/System/Error/Logger.php index c00ff2d6..58d55020 100644 --- a/System/Error/Logger.php +++ b/System/Error/Logger.php @@ -179,7 +179,7 @@ class Logger extends Component $files = glob(storage(null, $dirName) . '/*'); if (count($files) >= 15) { $command = 'find ' . storage(null, $dirName) . '/ -mtime +15 -name "*.log" -exec rm -rf {} \;'; - if (Context::inCoroutine()) { + if (Coroutine::getCid() !== -1) { Coroutine\System::exec($command); } else { \shell_exec($command); diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index 3350fe44..0093b1a2 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -53,10 +53,10 @@ class Redis extends Pool { $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $coroutineName = $this->name('redis', 'redis:' . $name, $isMaster); - if (($redis = Context::getContext($coroutineName)) instanceof \Redis) { - return $redis; + if (!Context::hasContext($coroutineName)) { + return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config)); } - return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config)); + return Context::getContext($coroutineName); }