123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace Hyperf\Pool;
- use Hyperf\Contract\ConnectionInterface;
- use Hyperf\Engine\Channel as CoChannel;
- use Hyperf\Utils\Coroutine;
- class Channel
- {
- protected $size;
- /**
- * @var CoChannel
- */
- protected $channel;
- /**
- * @var \SplQueue
- */
- protected $queue;
- public function __construct(int $size)
- {
- $this->size = $size;
- $this->channel = new CoChannel($size);
- $this->queue = new \SplQueue();
- }
- /**
- * @return ConnectionInterface|false
- */
- public function pop(float $timeout)
- {
- if ($this->isCoroutine()) {
- return $this->channel->pop($timeout);
- }
- return $this->queue->shift();
- }
- /**
- * @param ConnectionInterface $data
- * @return bool
- */
- public function push($data)
- {
- if ($this->isCoroutine()) {
- return $this->channel->push($data);
- }
- $this->queue->push($data);
- return true;
- }
- public function length(): int
- {
- if ($this->isCoroutine()) {
- return $this->channel->getLength();
- }
- return $this->queue->count();
- }
- protected function isCoroutine(): bool
- {
- return Coroutine::id() > 0;
- }
- }
|