2023-08-16 00:28:54 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Pool;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2023-08-25 09:39:52 +08:00
|
|
|
use Kiri\Di\Context;
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Pool
|
|
|
|
|
* @package Kiri\Pool
|
|
|
|
|
*/
|
|
|
|
|
class Pool implements PoolInterface
|
|
|
|
|
{
|
|
|
|
|
|
2023-08-16 16:33:57 +08:00
|
|
|
/** @var array<PoolItem> */
|
|
|
|
|
protected array $_connections = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $retain_number
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function flush($name, $retain_number): void
|
|
|
|
|
{
|
|
|
|
|
if ($this->hasChannel($name)) {
|
2023-08-25 09:37:59 +08:00
|
|
|
if ($retain_number == 0) {
|
|
|
|
|
$this->close($name);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
$this->channel($name)->tailor($retain_number);
|
2023-08-16 16:33:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
2023-08-16 16:33:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param PoolItem $channel
|
|
|
|
|
* @param $retain_number
|
|
|
|
|
*/
|
|
|
|
|
protected function pop(PoolItem $channel, $retain_number): void
|
|
|
|
|
{
|
|
|
|
|
$channel->tailor($retain_number);
|
|
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param int $max
|
|
|
|
|
* @param callable $closure
|
|
|
|
|
*/
|
2023-08-16 16:33:57 +08:00
|
|
|
public function created($name, int $max, callable $closure): void
|
|
|
|
|
{
|
|
|
|
|
if (!isset($this->_connections[$name])) {
|
|
|
|
|
$this->_connections[$name] = new PoolItem($max, $closure);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return PoolItem
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function channel($name): PoolItem
|
|
|
|
|
{
|
|
|
|
|
if (!isset($this->_connections[$name])) {
|
|
|
|
|
throw new Exception('Channel is not exists.');
|
|
|
|
|
}
|
|
|
|
|
$channel = $this->_connections[$name];
|
2023-08-25 09:39:52 +08:00
|
|
|
if ($channel->isClose()) {
|
2023-08-16 00:39:55 +08:00
|
|
|
$channel->reconnect();
|
|
|
|
|
}
|
|
|
|
|
return $channel;
|
2023-08-16 16:33:57 +08:00
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2023-08-16 16:33:57 +08:00
|
|
|
public function hasChannel($name): bool
|
|
|
|
|
{
|
|
|
|
|
return isset($this->_connections[$name]) && $this->_connections[$name] instanceof PoolItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return void
|
2023-08-29 21:35:19 +08:00
|
|
|
* @throws
|
2023-08-16 16:33:57 +08:00
|
|
|
*/
|
|
|
|
|
public function abandon(string $name): void
|
|
|
|
|
{
|
|
|
|
|
$this->channel($name)->abandon();
|
|
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
2023-08-16 16:35:55 +08:00
|
|
|
* @param int $waite_time
|
2023-08-16 00:28:54 +08:00
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-08-16 16:35:55 +08:00
|
|
|
public function get(string $name, int $waite_time = 3): mixed
|
2023-08-16 16:33:57 +08:00
|
|
|
{
|
2023-08-16 16:35:55 +08:00
|
|
|
return $this->channel($name)->pop($waite_time);
|
2023-08-16 16:33:57 +08:00
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-08-16 16:33:57 +08:00
|
|
|
public function isNull($name): bool
|
|
|
|
|
{
|
|
|
|
|
return $this->channel($name)->isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $client
|
|
|
|
|
* @return bool
|
|
|
|
|
* 检查连接可靠性
|
|
|
|
|
*/
|
|
|
|
|
public function checkCanUse(string $name, mixed $client): bool
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function hasItem(string $name): bool
|
|
|
|
|
{
|
|
|
|
|
$channel = $this->_connections[$name] ?? null;
|
|
|
|
|
if ($channel === null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return !$channel->isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function size(string $name): int
|
|
|
|
|
{
|
|
|
|
|
$channel = $this->_connections[$name] ?? null;
|
|
|
|
|
if ($channel === null) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return $channel->size();
|
|
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $data
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-08-16 16:33:57 +08:00
|
|
|
public function push(string $name, mixed $data): void
|
|
|
|
|
{
|
|
|
|
|
$this->channel($name)->push($data);
|
|
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param int $time
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-08-16 16:33:57 +08:00
|
|
|
public function waite($name, int $time = 30): mixed
|
|
|
|
|
{
|
|
|
|
|
return $this->channel($name)->pop($time);
|
|
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
2023-08-16 16:33:57 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function close(string $name): void
|
|
|
|
|
{
|
2023-08-25 09:39:52 +08:00
|
|
|
if (!isset($this->_connections[$name])) {
|
2023-08-16 16:33:57 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2023-08-25 09:40:11 +08:00
|
|
|
$this->_connections[$name]->close();
|
2023-08-16 16:33:57 +08:00
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
2023-08-16 16:33:57 +08:00
|
|
|
/**
|
2023-08-16 00:28:54 +08:00
|
|
|
* return pool queue lists
|
2023-08-16 16:33:57 +08:00
|
|
|
* @return PoolItem[]
|
|
|
|
|
*/
|
|
|
|
|
protected function channels(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_connections;
|
|
|
|
|
}
|
2023-08-16 00:28:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param mixed $client
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function release(string $name, mixed $client): void
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement release() method.
|
|
|
|
|
$this->channel($name)->push($client);
|
|
|
|
|
}
|
|
|
|
|
}
|