Files
kiri-core/System/Abstracts/Pool.php
T

161 lines
2.7 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Abstracts;
use Exception;
2020-12-17 14:12:44 +08:00
2020-08-31 01:27:08 +08:00
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
/**
* Class Pool
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Pool
2020-08-31 01:27:08 +08:00
*/
abstract class Pool extends Component
{
/** @var Channel[] */
2020-10-29 18:17:25 +08:00
private array $_items = [];
2020-08-31 01:27:08 +08:00
2020-10-29 18:17:25 +08:00
protected int $max = 60;
2020-08-31 01:27:08 +08:00
/**
* @param $name
* @param false $isMaster
* @param int $max
*/
public function initConnections($name, $isMaster = false, $max = 60)
{
$name = $this->name($name, $isMaster);
2020-09-05 03:51:21 +08:00
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
2020-08-31 01:27:08 +08:00
return;
}
$this->_items[$name] = new Channel($max);
2020-09-04 19:10:08 +08:00
$this->max = $max;
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
2020-12-17 14:09:14 +08:00
* @return array
2020-08-31 12:38:32 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
protected function get($name): array
2020-08-31 01:27:08 +08:00
{
2020-09-05 02:31:08 +08:00
[$timeout, $connection] = $this->_items[$name]->pop();
2020-09-04 19:16:09 +08:00
if (!$this->checkCanUse($name, $timeout, $connection)) {
2020-08-31 01:27:08 +08:00
unset($client);
return [0, null];
} else {
2020-09-05 01:29:46 +08:00
return [$timeout, $connection];
2020-08-31 01:27:08 +08:00
}
}
/**
* @param $cds
* @param false $isMaster
* @return string
*/
2020-12-17 14:12:44 +08:00
public function name($cds, $isMaster = false): string
2020-08-31 01:27:08 +08:00
{
2020-09-17 19:23:02 +08:00
if ($isMaster === true) {
2020-12-17 14:09:14 +08:00
return hash('sha256', $cds . 'master');
2020-09-17 19:23:02 +08:00
} else {
2020-12-17 14:09:14 +08:00
return hash('sha256', $cds . 'slave');
2020-09-17 19:23:02 +08:00
}
2020-08-31 01:27:08 +08:00
}
/**
2020-09-04 19:10:08 +08:00
* @param $name
2020-08-31 01:27:08 +08:00
* @param $time
* @param $client
* @return mixed
* 检查连接可靠性
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function checkCanUse(string $name, int $time, mixed $client): mixed
2020-08-31 01:27:08 +08:00
{
throw new Exception('Undefined system processing function.');
}
/**
* @param $name
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function desc(string $name)
2020-08-31 01:27:08 +08:00
{
throw new Exception('Undefined system processing function.');
}
/**
* @param array $config
* @param $isMaster
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function getConnection(array $config, bool $isMaster)
2020-08-31 01:27:08 +08:00
{
throw new Exception('Undefined system processing function.');
}
/**
* @param $name
2020-12-17 14:09:14 +08:00
* @return bool
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function hasItem(string $name): bool
2020-08-31 01:27:08 +08:00
{
2020-09-04 19:00:27 +08:00
return $this->size($name) > 0;
2020-08-31 01:27:08 +08:00
}
/**
* @param $name
* @return mixed
*/
2020-12-17 14:09:14 +08:00
public function size(string $name): mixed
2020-08-31 01:27:08 +08:00
{
if (!isset($this->_items[$name])) {
return 0;
}
return $this->_items[$name]->length();
}
/**
* @param $name
* @param $client
*/
2020-12-17 14:09:14 +08:00
public function push(string $name, mixed $client)
2020-08-31 01:27:08 +08:00
{
2020-09-05 01:49:36 +08:00
$this->_items[$name]->push([time(), $client]);
2020-09-05 03:08:39 +08:00
unset($client);
2020-08-31 01:27:08 +08:00
}
/**
2020-12-17 14:09:14 +08:00
* @param string $name
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public function clean(string $name)
2020-08-31 01:27:08 +08:00
{
if (!isset($this->_items[$name])) {
return;
}
2020-09-06 05:37:00 +08:00
$channel = $this->_items[$name];
while ([$time, $client] = $channel->pop(0.001)) {
2020-08-31 01:27:08 +08:00
unset($client);
}
}
2020-09-06 05:37:00 +08:00
/**
* @return Channel[]
*/
2020-12-17 14:09:14 +08:00
protected function getChannels(): array
2020-09-06 05:37:00 +08:00
{
return $this->_items;
}
2020-08-31 01:27:08 +08:00
}