ChannelPool.php 768 B

1234567891011121314151617181920212223242526272829303132333435363738
  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\Utils;
  12. use Swoole\Coroutine\Channel;
  13. class ChannelPool extends \SplQueue
  14. {
  15. /**
  16. * @var ChannelPool
  17. */
  18. private static $instance;
  19. public static function getInstance(): self
  20. {
  21. return static::$instance ?? (static::$instance = new self());
  22. }
  23. public function get(): Channel
  24. {
  25. return $this->isEmpty() ? new Channel(1) : $this->pop();
  26. }
  27. public function release(Channel $channel)
  28. {
  29. $channel->errCode = 0;
  30. $this->push($channel);
  31. }
  32. }