Channel.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. declare(strict_types=1);
  15. namespace Workerman\Coroutine;
  16. use InvalidArgumentException;
  17. use Workerman\Coroutine\Channel\ChannelInterface;
  18. use Workerman\Coroutine\Channel\Memory as ChannelMemory;
  19. use Workerman\Coroutine\Channel\Swoole as ChannelSwoole;
  20. use Workerman\Coroutine\Channel\Swow as ChannelSwow;
  21. use Workerman\Coroutine\Channel\Fiber as ChannelFiber;
  22. use Workerman\Events\Fiber;
  23. use Workerman\Events\Swoole;
  24. use Workerman\Events\Swow;
  25. use Workerman\Worker;
  26. /**
  27. * Class Channel
  28. */
  29. class Channel implements ChannelInterface
  30. {
  31. /**
  32. * @var ChannelInterface
  33. */
  34. protected ChannelInterface $driver;
  35. /**
  36. * Channel constructor.
  37. *
  38. * @param int $capacity
  39. */
  40. public function __construct(int $capacity = 1)
  41. {
  42. if ($capacity < 1) {
  43. throw new InvalidArgumentException("The capacity must be greater than 0");
  44. }
  45. $this->driver = match (Worker::$eventLoopClass) {
  46. Swoole::class => new ChannelSwoole($capacity),
  47. Swow::class => new ChannelSwow($capacity),
  48. Fiber::class => new ChannelFiber($capacity),
  49. default => new ChannelMemory($capacity),
  50. };
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. public function push(mixed $data, float $timeout = -1): bool
  56. {
  57. return $this->driver->push($data, $timeout);
  58. }
  59. /**
  60. * @inheritDoc
  61. */
  62. public function pop(float $timeout = -1): mixed
  63. {
  64. return $this->driver->pop($timeout);
  65. }
  66. /**
  67. * @inheritDoc
  68. */
  69. public function length(): int
  70. {
  71. return $this->driver->length();
  72. }
  73. /**
  74. * @inheritDoc
  75. */
  76. public function getCapacity(): int
  77. {
  78. return $this->driver->getCapacity();
  79. }
  80. /**
  81. * @inheritDoc
  82. */
  83. public function hasConsumers(): bool
  84. {
  85. return $this->driver->hasConsumers();
  86. }
  87. /**
  88. * @inheritDoc
  89. */
  90. public function hasProducers(): bool
  91. {
  92. return $this->driver->hasProducers();
  93. }
  94. /**
  95. * @inheritDoc
  96. */
  97. public function close(): void
  98. {
  99. $this->driver->close();
  100. }
  101. }