From 7410df11260addbff9a3fc1fe019bf90246f51f5 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Tue, 6 Jun 2023 09:52:53 +0800 Subject: [PATCH 01/22] [3.1]Bumps dependencies (#5796) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 李铭昕 <715557344@qq.com> --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 1d60f65..961bf25 100644 --- a/composer.json +++ b/composer.json @@ -16,10 +16,10 @@ "source": "https://github.com/hyperf/hyperf" }, "require": { - "php": ">=8.0", + "php": ">=8.1", "hyperf/context": "~3.0.0", "hyperf/contract": "~3.0.0", - "hyperf/engine": "^1.2|^2.0" + "hyperf/engine": "^2.0" }, "autoload": { "psr-4": { From 26e7c8a0aafe1a4069558455255835c7c94ee3c8 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Tue, 6 Jun 2023 13:40:38 +0800 Subject: [PATCH 02/22] Upgrade hyperf packages to version `~3.1.0` (#5801) --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 961bf25..bed6531 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,8 @@ }, "require": { "php": ">=8.1", - "hyperf/context": "~3.0.0", - "hyperf/contract": "~3.0.0", + "hyperf/context": "~3.1.0", + "hyperf/contract": "~3.1.0", "hyperf/engine": "^2.0" }, "autoload": { From 7d3337729d44783af2046d28263937245d1e9356 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 7 Jun 2023 10:16:25 +0800 Subject: [PATCH 03/22] Bumps `phpunit` to `10.x` (#5802) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 李铭昕 <715557344@qq.com> --- tests/Channel/CallerTest.php | 2 ++ tests/Channel/ChannelManagerTest.php | 2 ++ tests/ConcurrentTest.php | 2 ++ tests/CoroutineTest.php | 6 +++--- tests/FunctionTest.php | 6 +++--- tests/LockerTest.php | 2 ++ tests/ParallelTest.php | 3 ++- tests/WaitGroupTest.php | 2 ++ tests/WaiterTest.php | 2 ++ 9 files changed, 20 insertions(+), 7 deletions(-) diff --git a/tests/Channel/CallerTest.php b/tests/Channel/CallerTest.php index 9c0f62a..bb390f4 100644 --- a/tests/Channel/CallerTest.php +++ b/tests/Channel/CallerTest.php @@ -13,6 +13,7 @@ use Hyperf\Coroutine\Channel\Caller; use Hyperf\Coroutine\Exception\WaitTimeoutException; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use stdClass; @@ -22,6 +23,7 @@ * @internal * @coversNothing */ +#[CoversNothing] class CallerTest extends TestCase { public function testCallerWithNull() diff --git a/tests/Channel/ChannelManagerTest.php b/tests/Channel/ChannelManagerTest.php index 3c04882..2a34074 100644 --- a/tests/Channel/ChannelManagerTest.php +++ b/tests/Channel/ChannelManagerTest.php @@ -13,6 +13,7 @@ use Hyperf\Coroutine\Channel\Manager as ChannelManager; use Hyperf\Engine\Channel; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use function Hyperf\Coroutine\go; @@ -21,6 +22,7 @@ * @internal * @coversNothing */ +#[CoversNothing] class ChannelManagerTest extends TestCase { public function testChannelManager() diff --git a/tests/ConcurrentTest.php b/tests/ConcurrentTest.php index 6363152..9286945 100644 --- a/tests/ConcurrentTest.php +++ b/tests/ConcurrentTest.php @@ -15,6 +15,7 @@ use Hyperf\Context\ApplicationContext; use Hyperf\Coroutine\Concurrent; use Mockery; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use Swoole\Coroutine; @@ -23,6 +24,7 @@ * @internal * @coversNothing */ +#[CoversNothing] class ConcurrentTest extends TestCase { protected function setUp(): void diff --git a/tests/CoroutineTest.php b/tests/CoroutineTest.php index 947b1e9..6498284 100644 --- a/tests/CoroutineTest.php +++ b/tests/CoroutineTest.php @@ -19,6 +19,7 @@ use Hyperf\Engine\Exception\CoroutineDestroyedException; use Hyperf\ExceptionHandler\Formatter\FormatterInterface; use Mockery; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use Throwable; @@ -31,6 +32,7 @@ * @internal * @coversNothing */ +#[CoversNothing] class CoroutineTest extends TestCase { protected function tearDown(): void @@ -68,9 +70,7 @@ public function testCoroutineParentIdHasBeenDestroyed() } } - /** - * @group NonCoroutine - */ + #[\PHPUnit\Framework\Attributes\Group('NonCoroutine')] public function testCoroutineInTopCoroutine() { run(function () { diff --git a/tests/FunctionTest.php b/tests/FunctionTest.php index 7d3b40b..342f687 100644 --- a/tests/FunctionTest.php +++ b/tests/FunctionTest.php @@ -13,6 +13,7 @@ use Hyperf\Coroutine\Coroutine; use Hyperf\Engine\Channel; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use Swoole\Runtime; @@ -25,6 +26,7 @@ * @internal * @coversNothing */ +#[CoversNothing] class FunctionTest extends TestCase { public function testReturnOfGo() @@ -38,9 +40,7 @@ public function testReturnOfGo() $this->assertSame('Hyperf', $uniqid); } - /** - * @group NonCoroutine - */ + #[\PHPUnit\Framework\Attributes\Group('NonCoroutine')] public function testRun() { $asserts = [ diff --git a/tests/LockerTest.php b/tests/LockerTest.php index c3c2fe0..e20cc76 100644 --- a/tests/LockerTest.php +++ b/tests/LockerTest.php @@ -13,6 +13,7 @@ use Hyperf\Coroutine\Locker; use Hyperf\Engine\Channel; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use function Hyperf\Coroutine\go; @@ -21,6 +22,7 @@ * @internal * @coversNothing */ +#[CoversNothing] class LockerTest extends TestCase { public function testLockAndUnlock() diff --git a/tests/ParallelTest.php b/tests/ParallelTest.php index 29f9983..f267161 100644 --- a/tests/ParallelTest.php +++ b/tests/ParallelTest.php @@ -23,8 +23,9 @@ /** * @internal - * @covers \Hyperf\Coroutine\Parallel + * @coversNothing */ +#[\PHPUnit\Framework\Attributes\CoversClass(\Hyperf\Coroutine\Parallel::class)] class ParallelTest extends TestCase { public function testParallel() diff --git a/tests/WaitGroupTest.php b/tests/WaitGroupTest.php index 898d457..7670d01 100644 --- a/tests/WaitGroupTest.php +++ b/tests/WaitGroupTest.php @@ -12,6 +12,7 @@ namespace HyperfTest\Coroutine; use Hyperf\Coroutine\WaitGroup; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use Swoole\Coroutine; @@ -19,6 +20,7 @@ * @internal * @coversNothing */ +#[CoversNothing] class WaitGroupTest extends TestCase { public function testWaitAgain() diff --git a/tests/WaiterTest.php b/tests/WaiterTest.php index fbec168..5f93850 100644 --- a/tests/WaiterTest.php +++ b/tests/WaiterTest.php @@ -17,6 +17,7 @@ use Hyperf\Coroutine\Waiter; use Hyperf\Engine\Channel; use Mockery; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use RuntimeException; @@ -27,6 +28,7 @@ * @internal * @coversNothing */ +#[CoversNothing] class WaiterTest extends TestCase { protected function setUp(): void From 5cce1100e4aacdf39c6de9c993335b260d170c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 16 Jun 2023 17:09:43 +0800 Subject: [PATCH 04/22] Moved `go`, `co` and `defer` to `hyperf/coroutine`. (#5840) --- composer.json | 3 ++- src/Helper.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/Helper.php diff --git a/composer.json b/composer.json index bed6531..18fc5e7 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ "Hyperf\\Coroutine\\": "src/" }, "files": [ - "src/Functions.php" + "src/Functions.php", + "src/Helper.php" ] }, "autoload-dev": { diff --git a/src/Helper.php b/src/Helper.php new file mode 100644 index 0000000..6888700 --- /dev/null +++ b/src/Helper.php @@ -0,0 +1,31 @@ + Date: Fri, 16 Jun 2023 19:57:19 +0800 Subject: [PATCH 05/22] Adds `hyperf/polyfill-coroutine` component (#5841) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 李铭昕 <715557344@qq.com> --- composer.json | 3 +-- src/Helper.php | 31 ------------------------------- 2 files changed, 1 insertion(+), 33 deletions(-) delete mode 100644 src/Helper.php diff --git a/composer.json b/composer.json index 18fc5e7..bed6531 100644 --- a/composer.json +++ b/composer.json @@ -26,8 +26,7 @@ "Hyperf\\Coroutine\\": "src/" }, "files": [ - "src/Functions.php", - "src/Helper.php" + "src/Functions.php" ] }, "autoload-dev": { diff --git a/src/Helper.php b/src/Helper.php deleted file mode 100644 index 6888700..0000000 --- a/src/Helper.php +++ /dev/null @@ -1,31 +0,0 @@ - Date: Sat, 15 Jul 2023 09:09:38 +0800 Subject: [PATCH 06/22] Optimized `Hyperf\Coroutine\Locker`. (#5948) --- src/Locker.php | 51 +++++++++++------------------------------ tests/ContainerTest.php | 31 +++++++++++++++++++++++++ tests/Stub/Bar.php | 19 +++++++++++++++ tests/Stub/Foo.php | 19 +++++++++++++++ 4 files changed, 83 insertions(+), 37 deletions(-) create mode 100644 tests/ContainerTest.php create mode 100644 tests/Stub/Bar.php create mode 100644 tests/Stub/Foo.php diff --git a/src/Locker.php b/src/Locker.php index 3a4678a..b8deaa2 100644 --- a/src/Locker.php +++ b/src/Locker.php @@ -11,56 +11,33 @@ */ namespace Hyperf\Coroutine; -use Hyperf\Coroutine\Traits\Container; -use Hyperf\Engine\Constant; -use Hyperf\Engine\Coroutine as Co; -use Swoole\Coroutine as SwooleCoroutine; +use Hyperf\Engine\Channel; class Locker { - use Container; - - public static function add(string $key, int $id): void - { - self::$container[$key][] = $id; - } - - public static function clear(string $key): void - { - unset(self::$container[$key]); - } + /** + * @var Channel[] + */ + protected static array $channels = []; public static function lock(string $key): bool { - if (! self::has($key)) { - self::add($key, 0); + if (! isset(static::$channels[$key])) { + static::$channels[$key] = new Channel(1); return true; } - self::add($key, Coroutine::id()); - // TODO: When the verion of `hyperf/engine` >= 2.0, use `Co::yield()` instead. - match (Constant::ENGINE) { - 'Swoole' => SwooleCoroutine::yield(), - /* @phpstan-ignore-next-line */ - default => Co::yield(), - }; + + $channel = static::$channels[$key]; + $channel->pop(-1); return false; } public static function unlock(string $key): void { - if (self::has($key)) { - $ids = self::get($key); - foreach ($ids as $id) { - if ($id > 0) { - // TODO: When the verion of `hyperf/engine` >= 2.0, use `Co::resumeById()` instead. - match (Constant::ENGINE) { - 'Swoole' => SwooleCoroutine::resume($id), - /* @phpstan-ignore-next-line */ - default => Co::resumeById($id), - }; - } - } - self::clear($key); + if (isset(static::$channels[$key])) { + $channel = static::$channels[$key]; + static::$channels[$key] = null; + $channel->close(); } } } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php new file mode 100644 index 0000000..d585df2 --- /dev/null +++ b/tests/ContainerTest.php @@ -0,0 +1,31 @@ +assertSame($id, Foo::get('test')); + $this->assertFalse(Bar::has('test')); + $this->assertEmpty(Bar::list()); + } +} diff --git a/tests/Stub/Bar.php b/tests/Stub/Bar.php new file mode 100644 index 0000000..8333716 --- /dev/null +++ b/tests/Stub/Bar.php @@ -0,0 +1,19 @@ + Date: Thu, 17 Aug 2023 14:22:08 +0800 Subject: [PATCH 07/22] Removed deprecated `Hyperf\Coroutine\Traits\Container`. (#6043) --- src/Traits/Container.php | 61 ---------------------------------------- tests/Stub/Bar.php | 2 +- tests/Stub/Foo.php | 2 +- 3 files changed, 2 insertions(+), 63 deletions(-) delete mode 100644 src/Traits/Container.php diff --git a/src/Traits/Container.php b/src/Traits/Container.php deleted file mode 100644 index fbea4da..0000000 --- a/src/Traits/Container.php +++ /dev/null @@ -1,61 +0,0 @@ - Date: Fri, 24 Nov 2023 11:10:53 +0800 Subject: [PATCH 08/22] Changed the `branch-alias` of all components to `3.1-dev`. (#6320) --- .github/workflows/close-pull-request.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/close-pull-request.yml b/.github/workflows/close-pull-request.yml index 591822c..07d8be8 100644 --- a/.github/workflows/close-pull-request.yml +++ b/.github/workflows/close-pull-request.yml @@ -2,7 +2,7 @@ name: Close Pull Request on: pull_request_target: - types: [opened] + types: [ opened ] jobs: run: diff --git a/composer.json b/composer.json index bed6531..54125f5 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ }, "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-master": "3.1-dev" } } } From cd5bad67724c5c7a7ad749d8e9eb045470488d75 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Fri, 1 Dec 2023 14:59:45 +0800 Subject: [PATCH 09/22] Added `Coroutine::fork()` and `Coroutine::pid()` (#6342) --- src/Coroutine.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Coroutine.php b/src/Coroutine.php index 79453d2..39ad513 100644 --- a/src/Coroutine.php +++ b/src/Coroutine.php @@ -12,6 +12,7 @@ namespace Hyperf\Coroutine; use Hyperf\Context\ApplicationContext; +use Hyperf\Context\Context; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Engine\Coroutine as Co; use Hyperf\Engine\Exception\CoroutineDestroyedException; @@ -57,6 +58,16 @@ public static function parentId(?int $coroutineId = null): int return Co::pid($coroutineId); } + /** + * The alias of Coroutine::parentId(). + * @throws CoroutineDestroyedException when running in non-coroutine context + * @throws RunningInNonCoroutineException when the coroutine has been destroyed + */ + public static function pid(?int $coroutineId = null): int + { + return Co::pid($coroutineId); + } + /** * @return int Returns the coroutine ID of the coroutine just created. * Returns -1 when coroutine create failed. @@ -78,6 +89,20 @@ public static function create(callable $callable): int } } + /** + * Create a coroutine with a copy of the parent coroutine context. + */ + public static function fork(callable $callable, array $keys = []): int + { + $cid = static::id(); + $callable = static function () use ($callable, $cid, $keys) { + Context::copy($cid, $keys); + $callable(); + }; + + return static::create($callable); + } + public static function inCoroutine(): bool { return Co::id() > 0; From 8f4c573a9457646db3e629dacabe064eebaf8cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Sat, 23 Mar 2024 19:28:51 +0800 Subject: [PATCH 10/22] Format code by the latest `cs-fixer`. (#6617) --- src/Channel/Caller.php | 1 + src/Channel/Manager.php | 1 + src/Channel/Pool.php | 1 + src/Concurrent.php | 1 + src/Coroutine.php | 3 ++- src/Exception/ChannelClosedException.php | 1 + src/Exception/ExceptionThrower.php | 1 + src/Exception/InvalidArgumentException.php | 1 + src/Exception/ParallelExecutionException.php | 1 + src/Exception/TimeoutException.php | 1 + src/Exception/WaitTimeoutException.php | 1 + src/Functions.php | 6 ++++-- src/Locker.php | 1 + src/Parallel.php | 1 + src/WaitGroup.php | 1 + src/Waiter.php | 1 + tests/Channel/CallerTest.php | 1 + tests/Channel/ChannelManagerTest.php | 1 + tests/ConcurrentTest.php | 1 + tests/ContainerTest.php | 1 + tests/CoroutineTest.php | 4 +++- tests/FunctionTest.php | 4 +++- tests/LockerTest.php | 1 + tests/ParallelTest.php | 4 +++- tests/Stub/Bar.php | 1 + tests/Stub/Foo.php | 1 + tests/WaitGroupTest.php | 1 + tests/WaiterTest.php | 1 + 28 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Channel/Caller.php b/src/Channel/Caller.php index b4ecbbc..245da99 100644 --- a/src/Channel/Caller.php +++ b/src/Channel/Caller.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Channel; use Closure; diff --git a/src/Channel/Manager.php b/src/Channel/Manager.php index 7ff85b9..7ec4646 100644 --- a/src/Channel/Manager.php +++ b/src/Channel/Manager.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Channel; use Hyperf\Engine\Channel; diff --git a/src/Channel/Pool.php b/src/Channel/Pool.php index 43da649..d64f463 100644 --- a/src/Channel/Pool.php +++ b/src/Channel/Pool.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Channel; use Hyperf\Engine\Channel; diff --git a/src/Concurrent.php b/src/Concurrent.php index c66a14b..968a43b 100644 --- a/src/Concurrent.php +++ b/src/Concurrent.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; use Hyperf\Context\ApplicationContext; diff --git a/src/Coroutine.php b/src/Coroutine.php index 39ad513..a65dfb2 100644 --- a/src/Coroutine.php +++ b/src/Coroutine.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; use Hyperf\Context\ApplicationContext; @@ -84,7 +85,7 @@ public static function create(callable $callable): int try { return $coroutine->getId(); - } catch (\Throwable) { + } catch (Throwable) { return -1; } } diff --git a/src/Exception/ChannelClosedException.php b/src/Exception/ChannelClosedException.php index a90c444..d352918 100644 --- a/src/Exception/ChannelClosedException.php +++ b/src/Exception/ChannelClosedException.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Exception; use RuntimeException; diff --git a/src/Exception/ExceptionThrower.php b/src/Exception/ExceptionThrower.php index 5b2f94e..4568efc 100644 --- a/src/Exception/ExceptionThrower.php +++ b/src/Exception/ExceptionThrower.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Exception; use Throwable; diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index c9b8614..0676d71 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Exception; class InvalidArgumentException extends \InvalidArgumentException diff --git a/src/Exception/ParallelExecutionException.php b/src/Exception/ParallelExecutionException.php index c674dca..304a43d 100644 --- a/src/Exception/ParallelExecutionException.php +++ b/src/Exception/ParallelExecutionException.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Exception; use RuntimeException; diff --git a/src/Exception/TimeoutException.php b/src/Exception/TimeoutException.php index fae8576..77e353c 100644 --- a/src/Exception/TimeoutException.php +++ b/src/Exception/TimeoutException.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Exception; use RuntimeException; diff --git a/src/Exception/WaitTimeoutException.php b/src/Exception/WaitTimeoutException.php index 8393231..1ad1296 100644 --- a/src/Exception/WaitTimeoutException.php +++ b/src/Exception/WaitTimeoutException.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine\Exception; class WaitTimeoutException extends TimeoutException diff --git a/src/Functions.php b/src/Functions.php index e4c97ac..aafaf8e 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -9,11 +9,13 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; use Closure; use Hyperf\Context\ApplicationContext; use RuntimeException; +use Swoole\Runtime; /** * @param callable[] $callables @@ -65,11 +67,11 @@ function run($callbacks, int $flags = SWOOLE_HOOK_ALL): bool throw new RuntimeException('Function \'run\' only execute in non-coroutine environment.'); } - \Swoole\Runtime::enableCoroutine($flags); + Runtime::enableCoroutine($flags); /* @phpstan-ignore-next-line */ $result = \Swoole\Coroutine\run(...(array) $callbacks); - \Swoole\Runtime::enableCoroutine(false); + Runtime::enableCoroutine(false); return $result; } diff --git a/src/Locker.php b/src/Locker.php index b8deaa2..3d59473 100644 --- a/src/Locker.php +++ b/src/Locker.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; use Hyperf\Engine\Channel; diff --git a/src/Parallel.php b/src/Parallel.php index 22d4083..14f464a 100644 --- a/src/Parallel.php +++ b/src/Parallel.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; use Hyperf\Coroutine\Exception\ParallelExecutionException; diff --git a/src/WaitGroup.php b/src/WaitGroup.php index 67837d9..fa1fa0c 100644 --- a/src/WaitGroup.php +++ b/src/WaitGroup.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; use BadMethodCallException; diff --git a/src/Waiter.php b/src/Waiter.php index 7d41ed0..febb0ec 100644 --- a/src/Waiter.php +++ b/src/Waiter.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; use Closure; diff --git a/tests/Channel/CallerTest.php b/tests/Channel/CallerTest.php index bb390f4..9a480ac 100644 --- a/tests/Channel/CallerTest.php +++ b/tests/Channel/CallerTest.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine\Channel; use Hyperf\Coroutine\Channel\Caller; diff --git a/tests/Channel/ChannelManagerTest.php b/tests/Channel/ChannelManagerTest.php index 2a34074..19c830f 100644 --- a/tests/Channel/ChannelManagerTest.php +++ b/tests/Channel/ChannelManagerTest.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine\Channel; use Hyperf\Coroutine\Channel\Manager as ChannelManager; diff --git a/tests/ConcurrentTest.php b/tests/ConcurrentTest.php index 9286945..9b5168b 100644 --- a/tests/ConcurrentTest.php +++ b/tests/ConcurrentTest.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine; use Exception; diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index d585df2..6943fcf 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine; use HyperfTest\Coroutine\Stub\Bar; diff --git a/tests/CoroutineTest.php b/tests/CoroutineTest.php index 6498284..a8c67dc 100644 --- a/tests/CoroutineTest.php +++ b/tests/CoroutineTest.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine; use Exception; @@ -20,6 +21,7 @@ use Hyperf\ExceptionHandler\Formatter\FormatterInterface; use Mockery; use PHPUnit\Framework\Attributes\CoversNothing; +use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; use Throwable; @@ -70,7 +72,7 @@ public function testCoroutineParentIdHasBeenDestroyed() } } - #[\PHPUnit\Framework\Attributes\Group('NonCoroutine')] + #[Group('NonCoroutine')] public function testCoroutineInTopCoroutine() { run(function () { diff --git a/tests/FunctionTest.php b/tests/FunctionTest.php index 342f687..b1dcc6c 100644 --- a/tests/FunctionTest.php +++ b/tests/FunctionTest.php @@ -9,11 +9,13 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine; use Hyperf\Coroutine\Coroutine; use Hyperf\Engine\Channel; use PHPUnit\Framework\Attributes\CoversNothing; +use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; use Swoole\Runtime; @@ -40,7 +42,7 @@ public function testReturnOfGo() $this->assertSame('Hyperf', $uniqid); } - #[\PHPUnit\Framework\Attributes\Group('NonCoroutine')] + #[Group('NonCoroutine')] public function testRun() { $asserts = [ diff --git a/tests/LockerTest.php b/tests/LockerTest.php index e20cc76..91fc2a8 100644 --- a/tests/LockerTest.php +++ b/tests/LockerTest.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine; use Hyperf\Coroutine\Locker; diff --git a/tests/ParallelTest.php b/tests/ParallelTest.php index f267161..00d7475 100644 --- a/tests/ParallelTest.php +++ b/tests/ParallelTest.php @@ -9,12 +9,14 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine; use Exception; use Hyperf\Coroutine\Coroutine; use Hyperf\Coroutine\Exception\ParallelExecutionException; use Hyperf\Coroutine\Parallel; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use RuntimeException; use Throwable; @@ -25,7 +27,7 @@ * @internal * @coversNothing */ -#[\PHPUnit\Framework\Attributes\CoversClass(\Hyperf\Coroutine\Parallel::class)] +#[CoversClass(Parallel::class)] class ParallelTest extends TestCase { public function testParallel() diff --git a/tests/Stub/Bar.php b/tests/Stub/Bar.php index 0f66e10..e59fae5 100644 --- a/tests/Stub/Bar.php +++ b/tests/Stub/Bar.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine\Stub; use Hyperf\Support\Traits\Container; diff --git a/tests/Stub/Foo.php b/tests/Stub/Foo.php index bf9d907..192764c 100644 --- a/tests/Stub/Foo.php +++ b/tests/Stub/Foo.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine\Stub; use Hyperf\Support\Traits\Container; diff --git a/tests/WaitGroupTest.php b/tests/WaitGroupTest.php index 7670d01..09f57aa 100644 --- a/tests/WaitGroupTest.php +++ b/tests/WaitGroupTest.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine; use Hyperf\Coroutine\WaitGroup; diff --git a/tests/WaiterTest.php b/tests/WaiterTest.php index 5f93850..f81268c 100644 --- a/tests/WaiterTest.php +++ b/tests/WaiterTest.php @@ -9,6 +9,7 @@ * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace HyperfTest\Coroutine; use Hyperf\Context\ApplicationContext; From 9575665dae33561036ddd7e984adaf2b60396f6c Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Mon, 17 Jun 2024 09:51:06 +0800 Subject: [PATCH 11/22] Added composer.json normalize support (#6887) --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 54125f5..1fb83bf 100644 --- a/composer.json +++ b/composer.json @@ -10,10 +10,10 @@ ], "homepage": "https://hyperf.io", "support": { - "docs": "https://hyperf.wiki", "issues": "https://github.com/hyperf/hyperf/issues", - "pull-request": "https://github.com/hyperf/hyperf/pulls", - "source": "https://github.com/hyperf/hyperf" + "source": "https://github.com/hyperf/hyperf", + "docs": "https://hyperf.wiki", + "pull-request": "https://github.com/hyperf/hyperf/pulls" }, "require": { "php": ">=8.1", From 0f266971e4b3492a4a03d77fc80357fa92629efb Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Thu, 4 Jul 2024 10:13:45 +0800 Subject: [PATCH 12/22] Optimized phpdoc (#6927) --- src/Functions.php | 6 ++++++ src/Waiter.php | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/Functions.php b/src/Functions.php index aafaf8e..4746836 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -30,6 +30,12 @@ function parallel(array $callables, int $concurrent = 0): array return $parallel->wait(); } +/** + * @template TReturn + * + * @param Closure():TReturn $closure + * @return TReturn + */ function wait(Closure $closure, ?float $timeout = null) { if (ApplicationContext::hasContainer()) { diff --git a/src/Waiter.php b/src/Waiter.php index febb0ec..c0231d5 100644 --- a/src/Waiter.php +++ b/src/Waiter.php @@ -30,7 +30,11 @@ public function __construct(float $timeout = 10.0) } /** + * @template TReturn + * + * @param Closure():TReturn $closure * @param null|float $timeout seconds + * @return TReturn */ public function wait(Closure $closure, ?float $timeout = null) { From 58fd6ffdf1479e2364e6d342c05646aded91cd3c Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 25 Sep 2024 10:05:50 +0800 Subject: [PATCH 13/22] Improve actions (#7088) --- .github/workflows/close-pull-request.yml | 6 +----- .github/workflows/release.yml | 16 +--------------- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/.github/workflows/close-pull-request.yml b/.github/workflows/close-pull-request.yml index 07d8be8..b072371 100644 --- a/.github/workflows/close-pull-request.yml +++ b/.github/workflows/close-pull-request.yml @@ -6,8 +6,4 @@ on: jobs: run: - runs-on: ubuntu-latest - steps: - - uses: superbrothers/close-pull-request@v3 - with: - comment: "Hi, this is a READ-ONLY repository, please submit your PR on the https://github.com/hyperf/hyperf repository.

This Pull Request will close automatically.

Thanks! " + uses: hyperf/.github/.github/workflows/close-pull-request.yml@main diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 4ebb5d0..e891ae9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,18 +8,4 @@ name: Release jobs: release: - name: Release - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Create Release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: Release ${{ github.ref }} - draft: false - prerelease: false + uses: hyperf/.github/.github/workflows/release.yml@main From 1dd56202a97bce60df644ea1a8c87ea7a0888a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 25 Sep 2024 10:54:12 +0800 Subject: [PATCH 14/22] update github actions for all components. (#7090) --- .github/workflows/close-pull-request.yml | 2 +- .github/workflows/release.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/close-pull-request.yml b/.github/workflows/close-pull-request.yml index b072371..4cbca96 100644 --- a/.github/workflows/close-pull-request.yml +++ b/.github/workflows/close-pull-request.yml @@ -6,4 +6,4 @@ on: jobs: run: - uses: hyperf/.github/.github/workflows/close-pull-request.yml@main + uses: hyperf/.github/.github/workflows/close-pull-request.yml@master diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e891ae9..8ec4a62 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,4 +8,4 @@ name: Release jobs: release: - uses: hyperf/.github/.github/workflows/release.yml@main + uses: hyperf/.github/.github/workflows/release.yml@master From 959ea68ac700061d8f7b76708bc52f588b66979b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Fri, 13 Dec 2024 15:51:52 +0800 Subject: [PATCH 15/22] Compatible with swoole version 6.0 (#7206) --- src/Functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Functions.php b/src/Functions.php index 4746836..35ded47 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -78,6 +78,6 @@ function run($callbacks, int $flags = SWOOLE_HOOK_ALL): bool /* @phpstan-ignore-next-line */ $result = \Swoole\Coroutine\run(...(array) $callbacks); - Runtime::enableCoroutine(false); + Runtime::enableCoroutine(0); return $result; } From c353b3fbd86e30b5b51219e8867d479ea11e6811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Tue, 24 Dec 2024 16:59:48 +0800 Subject: [PATCH 16/22] Added `Hyperf\Coroutine\Mutex`. (#7224) * Added `Hyperf\Coroutine\Mutex`. * Update CHANGELOG-3.1.md --- src/Mutex.php | 61 +++++++++++++++++++++++++++++++++++++++++++++ tests/MutexTest.php | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/Mutex.php create mode 100644 tests/MutexTest.php diff --git a/src/Mutex.php b/src/Mutex.php new file mode 100644 index 0000000..ed2d3b5 --- /dev/null +++ b/src/Mutex.php @@ -0,0 +1,61 @@ +push(1, $timeout); + if ($channel->isTimeout() || $channel->isClosing()) { + return false; + } + + return true; + } + + public static function unlock(string $key, float $timeout = 5): bool + { + if (isset(static::$channels[$key])) { + $channel = static::$channels[$key]; + $channel->pop($timeout); + if ($channel->isTimeout()) { + // unlock more than once + return false; + } + } + + return true; + } + + public static function clear(string $key): void + { + if (isset(static::$channels[$key])) { + $channel = static::$channels[$key]; + static::$channels[$key] = null; + $channel->close(); + } + } +} diff --git a/tests/MutexTest.php b/tests/MutexTest.php new file mode 100644 index 0000000..679e458 --- /dev/null +++ b/tests/MutexTest.php @@ -0,0 +1,60 @@ +push($value); + } finally { + Mutex::unlock('test'); + } + } + }; + + $wg = new WaitGroup(5); + foreach (['h', 'e', 'l', 'l', 'o'] as $value) { + go(function () use ($func, $value, $wg) { + $func($value); + $wg->done(); + }); + } + + $res = ''; + $wg->wait(1); + for ($i = 0; $i < 5; ++$i) { + $res .= $chan->pop(1); + } + + $this->assertSame('hello', $res); + } +} From 19c16ece44269e0cf16d8dd06875e0072f668110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=2E=E5=94=90?= Date: Mon, 20 Jan 2025 10:23:38 +0800 Subject: [PATCH 17/22] Add `Hyperf\Coroutine\WaitConcurrent` (#7263) Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/WaitConcurrent.php | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/WaitConcurrent.php diff --git a/src/WaitConcurrent.php b/src/WaitConcurrent.php new file mode 100644 index 0000000..da96408 --- /dev/null +++ b/src/WaitConcurrent.php @@ -0,0 +1,48 @@ +wg = new WaitGroup(); + } + + public function create(callable $callable): void + { + $this->wg->add(); + + $callable = function () use ($callable) { + try { + $callable(); + } finally { + $this->wg->done(); + } + }; + + parent::create($callable); + } + + public function wait(float $timeout = -1): bool + { + return $this->wg->wait($timeout); + } +} From 223f0f9e17ee9dc8bf6e8da9e651296ba93e8d64 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Sat, 8 Feb 2025 11:41:26 +0800 Subject: [PATCH 18/22] Add `Hyperf\Coroutine\Coroutine::list()` (#7286) --- composer.json | 2 +- src/Coroutine.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1fb83bf..b004687 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "php": ">=8.1", "hyperf/context": "~3.1.0", "hyperf/contract": "~3.1.0", - "hyperf/engine": "^2.0" + "hyperf/engine": "^2.13.0" }, "autoload": { "psr-4": { diff --git a/src/Coroutine.php b/src/Coroutine.php index a65dfb2..bd4e731 100644 --- a/src/Coroutine.php +++ b/src/Coroutine.php @@ -119,6 +119,14 @@ public static function exists(int $id): bool return Co::exists($id); } + /** + * @return iterable + */ + public static function list(): iterable + { + return Co::list(); + } + private static function printLog(Throwable $throwable): void { if (ApplicationContext::hasContainer()) { From 5b474c4bb46be015f1340939d92931b96a0b0cad Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Mon, 14 Apr 2025 09:38:29 +0800 Subject: [PATCH 19/22] Added `Hyperf\Coroutine\Barrier` (#7343) --- composer.json | 2 +- src/Barrier.php | 17 +++++++++++++++++ tests/BarrierTest.php | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/Barrier.php create mode 100644 tests/BarrierTest.php diff --git a/composer.json b/composer.json index b004687..202a79e 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "php": ">=8.1", "hyperf/context": "~3.1.0", "hyperf/contract": "~3.1.0", - "hyperf/engine": "^2.13.0" + "hyperf/engine": "^2.14.0" }, "autoload": { "psr-4": { diff --git a/src/Barrier.php b/src/Barrier.php new file mode 100644 index 0000000..b01182a --- /dev/null +++ b/src/Barrier.php @@ -0,0 +1,17 @@ +assertSame($N, $count); + } +} From 6eef27161d44c7c2ac353591b7428a100aa87797 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Thu, 30 Oct 2025 09:44:44 +0800 Subject: [PATCH 20/22] Add `/types export-ignore` to all `.gitattributes` in `src/*` (#7581) --- .gitattributes | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitattributes b/.gitattributes index 27b765f..f11271b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ /tests export-ignore /.github export-ignore +/types export-ignore From 20c343c1641f19f0c59fde0eeb3c782b7d380ef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Mon, 1 Dec 2025 18:06:18 +0800 Subject: [PATCH 21/22] Optimized phpdoc for `Closure():TReturn`. (#7650) --- src/Functions.php | 2 +- src/Waiter.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Functions.php b/src/Functions.php index 35ded47..fe9424d 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -33,7 +33,7 @@ function parallel(array $callables, int $concurrent = 0): array /** * @template TReturn * - * @param Closure():TReturn $closure + * @param (Closure():TReturn) $closure * @return TReturn */ function wait(Closure $closure, ?float $timeout = null) diff --git a/src/Waiter.php b/src/Waiter.php index c0231d5..b760710 100644 --- a/src/Waiter.php +++ b/src/Waiter.php @@ -32,7 +32,7 @@ public function __construct(float $timeout = 10.0) /** * @template TReturn * - * @param Closure():TReturn $closure + * @param (Closure():TReturn) $closure * @param null|float $timeout seconds * @return TReturn */ From 060c276febdb9a079b54f5ca737b972ee6013d09 Mon Sep 17 00:00:00 2001 From: "Mr.tang" Date: Tue, 2 Dec 2025 22:30:56 +0800 Subject: [PATCH 22/22] phpstrom Code prompt optimization (#7579) --- src/Functions.php | 2 +- src/Waiter.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Functions.php b/src/Functions.php index fe9424d..35ded47 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -33,7 +33,7 @@ function parallel(array $callables, int $concurrent = 0): array /** * @template TReturn * - * @param (Closure():TReturn) $closure + * @param Closure():TReturn $closure * @return TReturn */ function wait(Closure $closure, ?float $timeout = null) diff --git a/src/Waiter.php b/src/Waiter.php index b760710..c0231d5 100644 --- a/src/Waiter.php +++ b/src/Waiter.php @@ -32,7 +32,7 @@ public function __construct(float $timeout = 10.0) /** * @template TReturn * - * @param (Closure():TReturn) $closure + * @param Closure():TReturn $closure * @param null|float $timeout seconds * @return TReturn */