From 0fb558bd3fa66719a0839e837292ed832240a36e Mon Sep 17 00:00:00 2001 From: Yurun Date: Sat, 25 Nov 2023 09:44:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E3=80=81=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E3=80=81=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/core/requestContext.md | 18 +++++++++++ .../src/Context/CoroutineContextManager.php | 32 ++++--------------- .../Component/Tests/RequestContextTest.php | 18 +++++++---- .../Component/Tests/RequestContextTest.php | 11 ++++--- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/doc/core/requestContext.md b/doc/core/requestContext.md index 0cdeeb2dea..685ed9f770 100644 --- a/doc/core/requestContext.md +++ b/doc/core/requestContext.md @@ -67,3 +67,21 @@ $result = \Imi\RequestContext::remember('myKey3', function () { return 1 + 2; }); ``` + +### 推迟执行 + +当协程释放时触发,先进后出 + +```php +use function Yurun\Swoole\Coroutine\goWait; +$result = []; +goWait(static function () use (&$result): void { + RequestContext::defer(static function () use (&$result): void { + $result[] = 1; + }); + RequestContext::defer(static function () use (&$result): void { + $result[] = 2; + }); +}, -1, true); +var_dump($result); // [2, 1] +``` \ No newline at end of file diff --git a/src/Components/swoole/src/Context/CoroutineContextManager.php b/src/Components/swoole/src/Context/CoroutineContextManager.php index 43f1e9e8f3..beb3765fcc 100644 --- a/src/Components/swoole/src/Context/CoroutineContextManager.php +++ b/src/Components/swoole/src/Context/CoroutineContextManager.php @@ -9,7 +9,6 @@ use Imi\Core\Context\Exception\ContextExistsException; use Imi\Core\Context\Exception\ContextNotFoundException; use Imi\Event\Event; -use Imi\Log\Log; use Imi\Swoole\Util\Coroutine; /** @@ -36,7 +35,7 @@ public function create(string|int $id, array $data = []): ContextData if (!($swooleContext[static::class]['destroyBinded'] ?? false)) { $swooleContext[static::class]['destroyBinded'] = true; - Coroutine::defer($this->__destroy(...)); + Coroutine::defer(fn () => $this->destroy($id)); } $context = $swooleContext[static::class]['context'] ?? null; if ($context) @@ -79,6 +78,8 @@ public function destroy(string|int $id): bool { return false; } + // TODO: 实现新的连接管理器后移除 + Event::trigger('IMI.REQUEST_CONTENT.DESTROY'); /** @var ContextData $context */ $context = $swooleContext[static::class]['context']; $deferCallbacks = $context->getDeferCallbacks(); @@ -92,6 +93,8 @@ public function destroy(string|int $id): bool } elseif (isset($this->contexts[$id])) { + // TODO: 实现新的连接管理器后移除 + Event::trigger('IMI.REQUEST_CONTENT.DESTROY'); $deferCallbacks = $this->contexts[$id]->getDeferCallbacks(); while (!$deferCallbacks->isEmpty()) { @@ -119,7 +122,7 @@ public function get(string|int $id, bool $autoCreate = false): ContextData if (!($swooleContext[static::class]['destroyBinded'] ?? false)) { $swooleContext[static::class]['destroyBinded'] = true; - Coroutine::defer($this->__destroy(...)); + Coroutine::defer(fn () => $this->destroy($id)); } if (!isset($swooleContext[static::class]['context'])) @@ -172,27 +175,4 @@ public function getCurrentId(): string|int { return (string) Coroutine::getCid(); } - - /** - * 销毁当前请求的上下文. - * - * 不要手动调用!不要手动调用!不要手动调用! - */ - public function __destroy(): void - { - try - { - // TODO: 实现新的连接管理器后移除 - Event::trigger('IMI.REQUEST_CONTENT.DESTROY'); - $context = Coroutine::getContext(); - if (!$context) - { - unset($this->contexts[Coroutine::getCid()]); - } - } - catch (\Throwable $th) - { - Log::error($th); - } - } } diff --git a/src/Components/swoole/tests/unit/Component/Tests/RequestContextTest.php b/src/Components/swoole/tests/unit/Component/Tests/RequestContextTest.php index ca48116185..40024b4101 100644 --- a/src/Components/swoole/tests/unit/Component/Tests/RequestContextTest.php +++ b/src/Components/swoole/tests/unit/Component/Tests/RequestContextTest.php @@ -7,6 +7,8 @@ use Imi\RequestContext; use Imi\Test\BaseTest; +use function Yurun\Swoole\Coroutine\goWait; + /** * @testdox RequestContext */ @@ -14,12 +16,16 @@ class RequestContextTest extends BaseTest { public function testDefer(): void { - $success = false; - RequestContext::defer(static function () use (&$success): void { - $success = true; - }); - RequestContext::destroy(); - $this->assertTrue($success); + $result = []; + goWait(static function () use (&$result): void { + RequestContext::defer(static function () use (&$result): void { + $result[] = 1; + }); + RequestContext::defer(static function () use (&$result): void { + $result[] = 2; + }); + }, -1, true); + $this->assertEquals([2, 1], $result); } public function testRemember(): void diff --git a/tests/unit/Component/Tests/RequestContextTest.php b/tests/unit/Component/Tests/RequestContextTest.php index 2d4aa459e9..0864638fcd 100644 --- a/tests/unit/Component/Tests/RequestContextTest.php +++ b/tests/unit/Component/Tests/RequestContextTest.php @@ -14,12 +14,15 @@ class RequestContextTest extends BaseTest { public function testDefer(): void { - $success = false; - RequestContext::defer(static function () use (&$success): void { - $success = true; + $result = []; + RequestContext::defer(static function () use (&$result): void { + $result[] = 1; + }); + RequestContext::defer(static function () use (&$result): void { + $result[] = 2; }); RequestContext::destroy(); - $this->assertTrue($success); + $this->assertEquals([2, 1], $result); } public function testRemember(): void