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

287 lines
5.2 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-06-17 11:59:19 +08:00
use Kiri\Abstracts\CoordinatorManager;
2022-07-11 16:09:58 +08:00
use Kiri\Annotation\Inject;
2022-02-24 15:24:29 +08:00
use Kiri\Context;
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
use Swoole\Coroutine\Channel;
/**
* Class Pool
* @package Kiri\Pool
*/
class Pool extends Component
{
2022-06-17 11:59:19 +08:00
/** @var array<PoolQueue> */
2022-01-09 03:50:38 +08:00
private static array $_connections = [];
public int $max = 60;
2022-07-11 16:09:58 +08:00
/**
* @var WorkerStatus
*/
#[Inject(WorkerStatus::class)]
public WorkerStatus $status;
2022-01-09 03:50:38 +08:00
use Alias;
/**
* @param $channel
* @param $retain_number
* @throws Exception
*/
public function flush($channel, $retain_number)
{
$this->pop($channel, $retain_number);
}
/**
2022-06-17 11:59:19 +08:00
* @param PoolQueue $channel
2022-01-09 03:50:38 +08:00
* @param $retain_number
*/
2022-06-17 11:59:19 +08:00
protected function pop(PoolQueue $channel, $retain_number): void
2022-01-09 03:50:38 +08:00
{
while ($channel->length() > $retain_number) {
if (Context::inCoroutine()) {
$connection = $channel->pop();
if ($connection instanceof StopHeartbeatCheck) {
$connection->stopHeartbeatCheck();
}
}
}
}
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
* @throws ConfigException
*/
2022-06-17 14:07:16 +08:00
public function check($name): array
2022-06-17 11:59:19 +08:00
{
$channel = $this->channel($name);
2022-06-17 14:30:31 +08:00
if ($channel->length() < 1) {
return [0, 0];
}
2022-06-17 14:04:23 +08:00
2022-07-11 16:09:58 +08:00
if ($this->status->is(StatusEnum::EXIT)) {
$channel->close();
return [0, 0];
}
2022-07-11 14:50:33 +08:00
$success = 0;
2022-06-17 14:04:23 +08:00
$lists = [];
2022-07-11 14:50:33 +08:00
$count = $channel->length();
2022-07-11 16:21:51 +08:00
while ($this->status->is(StatusEnum::EXIT) === false) {
2022-07-11 17:05:32 +08:00
if (!(($pdo = $channel->pop()) instanceof PDO)) {
break;
}
2022-06-17 14:07:16 +08:00
if ($pdo->check()) {
$success += 1;
}
2022-06-17 14:04:23 +08:00
$lists[] = $pdo;
}
2022-07-11 16:21:51 +08:00
if ($this->status->is(StatusEnum::EXIT) === false) {
foreach ($lists as $list) {
$channel->push($list);
}
} else {
$channel->close();
2022-06-17 14:04:23 +08:00
}
2022-06-17 14:07:16 +08:00
return [$count, $success];
2022-06-17 11:59:19 +08:00
}
2022-01-09 03:50:38 +08:00
/**
* @param $name
* @param int $max
* @throws ConfigException
*/
2022-02-24 11:00:01 +08:00
public function initConnections($name, int $max = 60)
2022-01-09 03:50:38 +08:00
{
if (isset(static::$_connections[$name])) {
$value = static::$_connections[$name];
2022-06-17 11:59:19 +08:00
if ($value instanceof PoolQueue) {
2022-01-09 03:50:38 +08:00
return;
}
}
$this->newChannel($name, $max);
$this->max = $max;
}
/**
* @param $name
2022-06-17 11:59:19 +08:00
* @return PoolQueue
2022-01-09 03:50:38 +08:00
* @throws ConfigException
* @throws Exception
*/
2022-06-17 11:59:19 +08:00
public function channel($name): PoolQueue
2022-01-09 03:50:38 +08:00
{
if (!isset(static::$_connections[$name])) {
$this->newChannel($name);
}
2022-06-17 12:09:07 +08:00
if (static::$_connections[$name]->isClose()) {
2022-01-09 03:50:38 +08:00
throw new Exception('Channel is Close.');
}
return static::$_connections[$name];
}
/**
* @throws ConfigException
*/
private function newChannel($name, $max = null)
{
if ($max == null) {
$max = Config::get('databases.pool.max', 10);
}
2022-06-17 11:59:19 +08:00
static::$_connections[$name] = new PoolQueue($max);
2022-01-09 03:50:38 +08:00
}
/**
* @param $name
* @param $callback
* @param $minx
* @return array
* @throws ConfigException
* @throws Exception
*/
public function get($name, $callback, $minx): mixed
{
2022-06-17 11:59:19 +08:00
$channel = $this->channel($name);
2022-01-09 03:50:38 +08:00
if (!$channel->isEmpty()) {
return $this->maxIdleQuantity($channel, $minx);
}
return $callback();
}
/**
* @param $channel
* @param $minx
* @return mixed
* @throws Exception
*/
protected function maxIdleQuantity($channel, $minx): mixed
{
$connection = $channel->pop();
if ($channel->length() > $minx) {
$this->pop($channel, $minx);
}
return $connection;
}
/**
* @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
{
if (isset(static::$_connections[$name])) {
return !static::$_connections[$name]->isEmpty();
}
return false;
}
/**
* @param string $name
* @return mixed
*/
public function size(string $name): mixed
{
if (!isset(static::$_connections[$name])) {
return 0;
}
return static::$_connections[$name]->length();
}
/**
* @param string $name
* @param mixed $client
* @throws ConfigException
*/
public function push(string $name, mixed $client)
{
2022-06-17 11:59:19 +08:00
$channel = $this->channel($name);
2022-01-09 03:50:38 +08:00
if (!$channel->isFull()) {
$channel->push($client);
}
unset($client);
}
/**
* @param string $name
* @throws Exception
*/
public function clean(string $name)
{
if (!isset(static::$_connections[$name])) {
return;
}
while (static::$_connections[$name]->length() > 0) {
$client = static::$_connections[$name]->pop();
if ($client instanceof StopHeartbeatCheck) {
$client->stopHeartbeatCheck();
}
}
static::$_connections[$name] = null;
unset(static::$_connections[$name]);
}
/**
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
{
return static::$_connections;
}
}