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

246 lines
4.3 KiB
PHP
Raw Normal View History

2022-01-09 03:50:38 +08:00
<?php
namespace Kiri\Pool;
2022-06-17 11:59:19 +08:00
use Database\Mysql\PDO;
2022-01-09 03:50:38 +08:00
use Exception;
use Kiri\Abstracts\Component;
use Kiri\Abstracts\Config;
2022-07-11 16:09:58 +08:00
use Kiri\Annotation\Inject;
2023-04-03 13:45:59 +08:00
use Kiri\Di\ContainerInterface;
2022-01-09 03:50:38 +08:00
use Kiri\Exception\ConfigException;
2022-07-11 16:09:58 +08:00
use Kiri\Server\Abstracts\StatusEnum;
use Kiri\Server\WorkerStatus;
2022-01-09 03:50:38 +08:00
/**
* Class Pool
* @package Kiri\Pool
*/
class Pool extends Component
{
2023-04-03 13:45:59 +08:00
/** @var array<PoolItem> */
private array $_connections = [];
2022-01-09 03:50:38 +08:00
2022-07-11 16:09:58 +08:00
/**
* @var WorkerStatus
*/
#[Inject(WorkerStatus::class)]
public WorkerStatus $status;
2022-01-09 03:50:38 +08:00
/**
2022-09-19 18:39:28 +08:00
* @param $name
2022-01-09 03:50:38 +08:00
* @param $retain_number
* @throws Exception
*/
2022-09-19 18:39:28 +08:00
public function flush($name, $retain_number)
2022-01-09 03:50:38 +08:00
{
2023-02-07 17:46:41 +08:00
if ($this->hasChannel($name)) {
2023-04-03 13:45:59 +08:00
$channel = $this->channel($name);
$channel->tailor($retain_number);
2023-02-07 17:46:41 +08:00
}
2022-01-09 03:50:38 +08:00
}
/**
2023-04-03 13:45:59 +08:00
* @param PoolItem $channel
2022-01-09 03:50:38 +08:00
* @param $retain_number
*/
2023-04-03 13:45:59 +08:00
protected function pop(PoolItem $channel, $retain_number): void
2022-01-09 03:50:38 +08:00
{
2023-04-03 13:45:59 +08:00
$channel->tailor($retain_number);
2022-01-09 03:50:38 +08:00
}
2022-06-17 11:59:19 +08:00
/**
* @param $name
2022-06-17 14:07:16 +08:00
* @return array
2022-06-17 11:59:19 +08:00
*/
2022-06-17 14:07:16 +08:00
public function check($name): array
2022-06-17 11:59:19 +08:00
{
2023-04-03 13:45:59 +08:00
// $channel = $this->channel($name);
// if ($channel->size() < 1) {
// return [0, 0];
// }
//
// if ($this->status->is(StatusEnum::EXIT)) {
// $channel->close();
// return [0, 0];
// }
//
// $success = 0;
// $lists = [];
// $count = $channel->size();
// while ($this->status->is(StatusEnum::EXIT) === false) {
// if (!(($pdo = $channel->pop(0.001)) instanceof PDO)) {
// break;
// }
// if ($pdo->check()) {
// $success += 1;
// }
// $lists[] = $pdo;
// }
// if ($this->status->is(StatusEnum::EXIT) === false) {
// foreach ($lists as $list) {
// $channel->push($list);
// }
// } else {
// $channel->close();
// }
// return [$count, $success];
return [0, 0];
2022-06-17 11:59:19 +08:00
}
2022-01-09 03:50:38 +08:00
/**
* @param $name
* @param int $max
2023-04-03 13:45:59 +08:00
* @param \Closure $closure
2022-01-09 03:50:38 +08:00
*/
2023-04-03 13:45:59 +08:00
public function initConnections($name, int $max, \Closure $closure)
2022-01-09 03:50:38 +08:00
{
2023-04-03 13:45:59 +08:00
if (!isset($this->_connections[$name])) {
$this->_connections[$name] = new PoolItem($max, $closure);
2022-01-09 03:50:38 +08:00
}
}
/**
* @param $name
2023-04-03 13:45:59 +08:00
* @return PoolItem
2022-01-09 03:50:38 +08:00
* @throws ConfigException
* @throws Exception
*/
2023-04-03 13:45:59 +08:00
public function channel($name): PoolItem
2022-01-09 03:50:38 +08:00
{
2023-04-03 13:45:59 +08:00
if (!isset($this->_connections[$name])) {
2023-02-07 16:46:13 +08:00
throw new Exception('Channel is not exists.');
2022-01-09 03:50:38 +08:00
}
2023-04-03 13:45:59 +08:00
return $this->_connections[$name];
2022-01-09 03:50:38 +08:00
}
2023-02-07 17:19:31 +08:00
public function hasChannel($name): bool
2023-02-07 17:16:10 +08:00
{
2023-04-03 13:45:59 +08:00
if (!isset($this->_connections[$name])) {
2023-02-07 17:16:10 +08:00
return false;
}
return true;
}
2022-01-09 03:50:38 +08:00
/**
2023-04-03 13:45:59 +08:00
* @param string $name
2022-01-09 03:50:38 +08:00
* @return array
* @throws ConfigException
*/
2023-04-03 13:45:59 +08:00
public function get(string $name): mixed
2022-01-09 03:50:38 +08:00
{
2023-04-03 13:45:59 +08:00
return $this->channel($name)->pop();
2022-01-09 03:50:38 +08:00
}
/**
* @param $name
* @return bool
* @throws ConfigException
*/
public function isNull($name): bool
{
2022-06-17 11:59:19 +08:00
return $this->channel($name)->isEmpty();
2022-01-09 03:50:38 +08:00
}
/**
* @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
{
2023-04-03 13:45:59 +08:00
$channel = $this->_connections[$name] ?? null;
if ($channel === null) {
2023-02-07 16:46:13 +08:00
return false;
2022-01-09 03:50:38 +08:00
}
2023-02-07 16:46:13 +08:00
return !$channel->isEmpty();
2022-01-09 03:50:38 +08:00
}
/**
* @param string $name
2023-01-30 11:03:58 +08:00
* @return int
2022-01-09 03:50:38 +08:00
*/
2023-01-30 11:03:58 +08:00
public function size(string $name): int
2022-01-09 03:50:38 +08:00
{
2023-04-03 13:45:59 +08:00
$channel = $this->_connections[$name] ?? null;
if ($channel === null) {
2022-01-09 03:50:38 +08:00
return 0;
}
2023-04-03 13:45:59 +08:00
return $channel->size();
2022-01-09 03:50:38 +08:00
}
/**
* @param string $name
* @param mixed $client
* @throws ConfigException
*/
public function push(string $name, mixed $client)
{
2023-04-03 13:45:59 +08:00
$this->channel($name)->push($client);
2022-01-09 03:50:38 +08:00
}
2023-04-03 00:14:29 +08:00
/**
* @param $name
* @param int $time
* @return array
* @throws ConfigException
*/
public function waite($name, int $time = 30): mixed
{
return $this->channel($name)->pop($time);
}
2022-01-09 03:50:38 +08:00
/**
* @param string $name
* @throws Exception
*/
public function clean(string $name)
{
2023-04-03 13:45:59 +08:00
$channel = $this->_connections[$name] ?? null;
if ($channel === null) {
2022-01-09 03:50:38 +08:00
return;
}
2023-04-03 13:45:59 +08:00
$channel->tailor(0);
2023-02-07 16:46:13 +08:00
$channel->close();
2022-01-09 03:50:38 +08:00
}
/**
2022-06-17 11:59:19 +08:00
* @return PoolQueue[]
2022-01-09 03:50:38 +08:00
*/
2022-06-17 11:59:19 +08:00
protected function channels(): array
2022-01-09 03:50:38 +08:00
{
2023-04-03 13:45:59 +08:00
return $this->_connections;
2022-01-09 03:50:38 +08:00
}
}