Channel.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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\Engine;
  12. use Hyperf\Engine\Contract\ChannelInterface;
  13. use Hyperf\Engine\Exception\RuntimeException;
  14. /**
  15. * @template TValue of mixed
  16. * @implements ChannelInterface
  17. */
  18. class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface
  19. {
  20. protected bool $closed = false;
  21. /**
  22. * @param TValue $data
  23. * @param float|int $timeout seconds [optional] = -1
  24. */
  25. public function push(mixed $data, float $timeout = -1): bool
  26. {
  27. return parent::push($data, $timeout);
  28. }
  29. /**
  30. * @param float $timeout seconds [optional] = -1
  31. * @return false|TValue when pop failed, return false
  32. */
  33. public function pop(float $timeout = -1): mixed
  34. {
  35. return parent::pop($timeout);
  36. }
  37. public function getCapacity(): int
  38. {
  39. return $this->capacity;
  40. }
  41. public function getLength(): int
  42. {
  43. return $this->length();
  44. }
  45. public function isAvailable(): bool
  46. {
  47. return ! $this->isClosing();
  48. }
  49. public function close(): bool
  50. {
  51. $this->closed = true;
  52. return parent::close();
  53. }
  54. public function hasProducers(): bool
  55. {
  56. throw new RuntimeException('Not supported.');
  57. }
  58. public function hasConsumers(): bool
  59. {
  60. throw new RuntimeException('Not supported.');
  61. }
  62. public function isReadable(): bool
  63. {
  64. throw new RuntimeException('Not supported.');
  65. }
  66. public function isWritable(): bool
  67. {
  68. throw new RuntimeException('Not supported.');
  69. }
  70. public function isClosing(): bool
  71. {
  72. return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED;
  73. }
  74. public function isTimeout(): bool
  75. {
  76. return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT;
  77. }
  78. }