.gitattributes000064400000000054151360553200007436 0ustar00/tests export-ignore /.github export-ignore LICENSE000064400000002110151360553200005543 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.json000064400000001624151360553200007271 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.php000064400000000743151360553200010024 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(); } /** * Wakeup all coroutines yielding for this coordinator. */ public function resume(): void { $this->channel->close(); } } src/CoordinatorManager.php000064400000002574151360553200011632 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/Timer.php000064400000005343151360553200007131 0ustar00id; $this->closures[$id] = true; go(function () use ($timeout, $closure, $identifier, $id) { try { ++Timer::$count; $isClosing = CoordinatorManager::until($identifier)->yield($timeout); 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($timeout); if (! isset($this->closures[$id])) { break; } try { $result = $closure($isClosing); if ($result === self::STOP || $isClosing) { break; } } catch (Throwable $exception) { $this->logger?->error((string) $exception); } ++$round; ++Timer::$round; } } finally { unset($this->closures[$id]); Timer::$round -= $round; --Timer::$count; } }); return $id; } 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, ]; } }