.gitattributes000064400000000054151360553210007437 0ustar00/tests export-ignore /.github export-ignore LICENSE000064400000002054151360553210005553 0ustar00The MIT License (MIT) Copyright (c) Hyperf Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. composer.json000064400000001715151360553210007273 0ustar00{ "name": "hyperf/coordinator", "description": "Hyperf Coordinator", "license": "MIT", "keywords": [ "php", "swoole", "hyperf", "coordinator" ], "homepage": "https://hyperf.io", "support": { "issues": "https://github.com/hyperf/hyperf/issues", "source": "https://github.com/hyperf/hyperf", "docs": "https://hyperf.wiki", "pull-request": "https://github.com/hyperf/hyperf/pulls" }, "require": { "php": ">=8.1", "hyperf/engine": "^2.0" }, "autoload": { "psr-4": { "Hyperf\\Coordinator\\": "src/" }, "files": [ "src/Functions.php" ] }, "autoload-dev": { "psr-4": { "HyperfTest\\Coordinator\\": "tests/" } }, "config": { "sort-packages": true }, "extra": { "branch-alias": { "dev-master": "3.1-dev" } } } src/Constants.php000064400000000744151360553210010026 0ustar00channel = new Channel(1); } /** * Yield the current coroutine for a given timeout, * unless the coordinator is woke up from outside. * * @param float|int $timeout * @return bool returns true if the coordinator has been woken up */ public function yield($timeout = -1): bool { $this->channel->pop((float) $timeout); return $this->channel->isClosing(); } public function isClosing(): bool { return $this->channel->isClosing(); } /** * Wakeup all coroutines yielding for this coordinator. */ public function resume(): void { $this->channel->close(); } } src/CoordinatorManager.php000064400000002575151360553210011634 0ustar00 */ private static array $container = []; /** * You should initialize a Coordinator with the identifier before use it. */ public static function initialize(string $identifier): void { static::$container[$identifier] = new Coordinator(); } /** * Get a Coordinator from container by the identifier. * * @throws RuntimeException when the Coordinator with the identifier has not initialization */ public static function until(string $identifier): Coordinator { if (! isset(static::$container[$identifier])) { static::$container[$identifier] = new Coordinator(); } return static::$container[$identifier]; } /** * Remove the Coordinator by the identifier from container after used, * otherwise memory leaks will occur. */ public static function clear(string $identifier): void { unset(static::$container[$identifier]); } } src/Functions.php000064400000002106151360553210010014 0ustar00yield($timeout)`. */ function block(float $timeout = -1, string $identifier = Constants::WORKER_EXIT): bool { return CoordinatorManager::until($identifier)->yield($timeout); } /** * Resume the coroutine that is blocked by the specified identifier. * Alias of `CoordinatorManager::until($identifier)->resume()`. */ function resume(string $identifier = Constants::WORKER_EXIT): void { CoordinatorManager::until($identifier)->resume(); } /** * Clear the coroutine that is blocked by the specified identifier. * Alias of `CoordinatorManager::clear($identifier)`. */ function clear(string $identifier = Constants::WORKER_EXIT): void { CoordinatorManager::clear($identifier); } src/Listener/ResumeExitCoordinatorListener.php000064400000001356151360553210015643 0ustar00resume(); } } src/Timer.php000064400000006371151360553210007134 0ustar00id; $this->closures[$id] = true; go(function () use ($timeout, $closure, $identifier, $id) { try { ++Timer::$count; $isClosing = match (true) { $timeout > 0 => CoordinatorManager::until($identifier)->yield($timeout), // Run after $timeout seconds. $timeout == 0 => CoordinatorManager::until($identifier)->isClosing(), // Run immediately. default => CoordinatorManager::until($identifier)->yield(), // Run until $identifier resume. }; if (isset($this->closures[$id])) { $closure($isClosing); } } finally { unset($this->closures[$id]); --Timer::$count; } }); return $id; } public function tick(float $timeout, callable $closure, string $identifier = Constants::WORKER_EXIT): int { $id = ++$this->id; $this->closures[$id] = true; go(function () use ($timeout, $closure, $identifier, $id) { try { $round = 0; ++Timer::$count; while (true) { $isClosing = CoordinatorManager::until($identifier)->yield(max($timeout, 0.000001)); if (! isset($this->closures[$id])) { break; } $result = null; try { $result = $closure($isClosing); } catch (Throwable $exception) { $this->logger?->error((string) $exception); } if ($result === self::STOP || $isClosing) { break; } ++$round; ++Timer::$round; } } finally { unset($this->closures[$id]); Timer::$round -= $round; --Timer::$count; } }); return $id; } public function until(callable $closure, string $identifier = Constants::WORKER_EXIT): int { return $this->after(-1, $closure, $identifier); } public function clear(int $id): void { unset($this->closures[$id]); } public function clearAll(): void { $this->closures = []; } public static function stats(): array { return [ 'num' => Timer::$count, 'round' => Timer::$round, ]; } }