ConstantFrequency.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\Pool;
  12. use Hyperf\Coordinator\Timer;
  13. class ConstantFrequency implements LowFrequencyInterface
  14. {
  15. protected Timer $timer;
  16. protected ?int $timerId = null;
  17. protected int $interval = 10000;
  18. public function __construct(protected ?Pool $pool = null)
  19. {
  20. $this->timer = new Timer();
  21. if ($pool) {
  22. $this->timerId = $this->timer->tick(
  23. $this->interval / 1000,
  24. fn () => $this->pool->flushOne()
  25. );
  26. }
  27. }
  28. public function __destruct()
  29. {
  30. $this->clear();
  31. }
  32. public function clear()
  33. {
  34. if ($this->timerId) {
  35. $this->timer->clear($this->timerId);
  36. }
  37. $this->timerId = null;
  38. }
  39. public function isLowFrequency(): bool
  40. {
  41. return false;
  42. }
  43. }