Files
kiri-pool/Pool.php
T

218 lines
4.3 KiB
PHP
Raw Normal View History

2023-08-16 00:28:54 +08:00
<?php
namespace Kiri\Pool;
use Exception;
/**
* 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-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-16 00:39:55 +08:00
if ($channel->isClose()) {
$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
* @throws Exception
*/
public function abandon(string $name): void
{
$this->channel($name)->abandon();
}
2023-08-16 00:28:54 +08:00
/**
* @param string $name
* @return array
* @throws Exception
*/
2023-08-16 16:33:57 +08:00
public function get(string $name): mixed
{
return $this->channel($name)->pop();
}
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
{
$channel = $this->_connections[$name] ?? null;
if ($channel === null) {
return;
}
$channel->tailor(0);
$channel->close();
}
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);
}
}