Timer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Coordinator;
  12. use Psr\Log\LoggerInterface;
  13. use Throwable;
  14. use function Hyperf\Coroutine\go;
  15. class Timer
  16. {
  17. public const STOP = 'stop';
  18. private array $closures = [];
  19. private int $id = 0;
  20. private static int $count = 0;
  21. private static int $round = 0;
  22. public function __construct(private ?LoggerInterface $logger = null)
  23. {
  24. }
  25. public function after(float $timeout, callable $closure, string $identifier = Constants::WORKER_EXIT): int
  26. {
  27. $id = ++$this->id;
  28. $this->closures[$id] = true;
  29. go(function () use ($timeout, $closure, $identifier, $id) {
  30. try {
  31. ++Timer::$count;
  32. $isClosing = match (true) {
  33. $timeout > 0 => CoordinatorManager::until($identifier)->yield($timeout), // Run after $timeout seconds.
  34. $timeout == 0 => CoordinatorManager::until($identifier)->isClosing(), // Run immediately.
  35. default => CoordinatorManager::until($identifier)->yield(), // Run until $identifier resume.
  36. };
  37. if (isset($this->closures[$id])) {
  38. $closure($isClosing);
  39. }
  40. } finally {
  41. unset($this->closures[$id]);
  42. --Timer::$count;
  43. }
  44. });
  45. return $id;
  46. }
  47. public function tick(float $timeout, callable $closure, string $identifier = Constants::WORKER_EXIT): int
  48. {
  49. $id = ++$this->id;
  50. $this->closures[$id] = true;
  51. go(function () use ($timeout, $closure, $identifier, $id) {
  52. try {
  53. $round = 0;
  54. ++Timer::$count;
  55. while (true) {
  56. $isClosing = CoordinatorManager::until($identifier)->yield(max($timeout, 0.000001));
  57. if (! isset($this->closures[$id])) {
  58. break;
  59. }
  60. $result = null;
  61. try {
  62. $result = $closure($isClosing);
  63. } catch (Throwable $exception) {
  64. $this->logger?->error((string) $exception);
  65. }
  66. if ($result === self::STOP || $isClosing) {
  67. break;
  68. }
  69. ++$round;
  70. ++Timer::$round;
  71. }
  72. } finally {
  73. unset($this->closures[$id]);
  74. Timer::$round -= $round;
  75. --Timer::$count;
  76. }
  77. });
  78. return $id;
  79. }
  80. public function until(callable $closure, string $identifier = Constants::WORKER_EXIT): int
  81. {
  82. return $this->after(-1, $closure, $identifier);
  83. }
  84. public function clear(int $id): void
  85. {
  86. unset($this->closures[$id]);
  87. }
  88. public function clearAll(): void
  89. {
  90. $this->closures = [];
  91. }
  92. public static function stats(): array
  93. {
  94. return [
  95. 'num' => Timer::$count,
  96. 'round' => Timer::$round,
  97. ];
  98. }
  99. }