.gitattributes000064400000000054151360551660007446 0ustar00/tests export-ignore /.github export-ignore LICENSE000064400000002110151360551660005553 0ustar00The MIT License (MIT) Copyright (c) Taylor Otwell 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.json000064400000001624151360551660007301 0ustar00{ "name": "hyperf/coordinator", "description": "Hyperf Coordinator", "license": "MIT", "keywords": [ "php", "swoole", "hyperf", "coordinator" ], "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" }, "require": { "php": ">=8.0", "hyperf/engine": "^1.2|^2.0" }, "autoload": { "psr-4": { "Hyperf\\Coordinator\\": "src/" } }, "autoload-dev": { "psr-4": { "HyperfTest\\Coordinator\\": "tests/" } }, "config": { "sort-packages": true }, "extra": { "branch-alias": { "dev-master": "3.0-dev" } } } src/Constants.php000064400000000743151360551660010034 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.php000064400000002574151360551660011642 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/Listener/ResumeExitCoordinatorListener.php000064400000001355151360551660015651 0ustar00resume(); } } src/Timer.php000064400000006426151360551660007144 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, Closure $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(Closure $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, ]; } }