Files
kiri-core/kiri-engine/Pool/PoolQueue.php
T

109 lines
1.5 KiB
PHP
Raw Normal View History

2022-06-17 11:59:19 +08:00
<?php
namespace Kiri\Pool;
2022-12-12 17:31:12 +08:00
use Kiri\Di\Context;
2022-06-17 11:59:19 +08:00
use Swoole\Coroutine\Channel;
class PoolQueue implements QueueInterface
{
private Channel|SplQueue $queue;
2022-07-11 16:54:24 +08:00
/**
* @param int $max
*/
2022-06-17 11:59:19 +08:00
public function __construct(public int $max)
{
2023-04-03 00:56:22 +08:00
if (Context::inCoroutine()) {
2022-07-11 17:05:32 +08:00
$this->queue = new Channel($this->max);
2023-04-03 00:56:22 +08:00
} else {
$this->queue = new SplQueue($this->max);
}
2022-06-17 11:59:19 +08:00
}
/**
* @return bool
*/
public function isEmpty(): bool
{
return $this->queue->isEmpty();
}
/**
* @param mixed $data
* @param float $timeout
* @return bool
*/
public function push(mixed $data, float $timeout = -1): bool
{
2022-07-11 18:53:19 +08:00
if ($this->isFull()) {
return false;
}
2022-07-11 16:09:58 +08:00
if (!$this->isClose()) {
return $this->queue->push($data, $timeout);
}
return false;
2022-06-17 11:59:19 +08:00
}
/**
* @param float $timeout
* @return mixed
*/
2022-07-11 17:35:45 +08:00
public function pop(float $timeout = 0): mixed
2022-06-17 11:59:19 +08:00
{
return $this->queue->pop($timeout);
}
/**
* @return array
*/
public function stats(): array
{
return $this->queue->stats();
}
/**
* @return bool
*/
public function close(): bool
{
return $this->queue->close();
}
/**
* @return int
*/
public function length(): int
{
return $this->queue->length();
}
/**
* @return bool
*/
public function isFull(): bool
{
return $this->queue->isFull();
}
/**
* @return bool
*/
public function isClose(): bool
{
if ($this->queue instanceof Channel) {
return $this->queue->errCode == SWOOLE_CHANNEL_CLOSED;
}
return false;
}
}