This commit is contained in:
2021-05-20 18:34:03 +08:00
parent 4dd3c3607c
commit 3abbfc8e22
+268 -268
View File
@@ -5,7 +5,6 @@ namespace Snowflake\Abstracts;
use Exception; use Exception;
use HttpServer\Http\Context;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Swoole\Coroutine; use Swoole\Coroutine;
@@ -19,318 +18,319 @@ use Swoole\Timer;
abstract class Pool extends Component abstract class Pool extends Component
{ {
/** @var Channel[] */ /** @var Channel[] */
private static array $_items = []; private static array $_items = [];
public int $max = 60; public int $max = 60;
public int $creates = -1; public int $creates = -1;
public int $lastTime = 0; public int $lastTime = 0;
protected static array $hasCreate = []; protected static array $hasCreate = [];
/** /**
* @param string $name * @param string $name
*/ */
public function increment(string $name) public function increment(string $name)
{ {
if (!isset(static::$hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
static::$hasCreate[$name] = 0; static::$hasCreate[$name] = 0;
} }
static::$hasCreate[$name] += 1; static::$hasCreate[$name] += 1;
} }
/** /**
* @param string $name * @param string $name
*/ */
public function decrement(string $name) public function decrement(string $name)
{ {
if (!isset(static::$hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
return; return;
} }
if (static::$hasCreate[$name] <= 0) { if (static::$hasCreate[$name] <= 0) {
return; return;
} }
static::$hasCreate[$name] -= 1; static::$hasCreate[$name] -= 1;
} }
/** /**
* @return array * @return array
* @throws ConfigException * @throws ConfigException
*/ */
private function getClearTime(): array private function getClearTime(): array
{ {
$firstClear = Config::get('pool.clear.start', 600); $firstClear = Config::get('pool.clear.start', 600);
$lastClear = Config::get('pool.clear.end', 300); $lastClear = Config::get('pool.clear.end', 300);
return [$firstClear, $lastClear]; return [$firstClear, $lastClear];
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function Heartbeat_detection() public function Heartbeat_detection()
{ {
if (env('state') == 'exit') { if (env('state') == 'exit') {
Timer::clear($this->creates); Timer::clear($this->creates);
$this->creates = -1; $this->creates = -1;
} else if ($this->lastTime != 0) { } else if ($this->lastTime != 0) {
[$firstClear, $lastClear] = $this->getClearTime(); [$firstClear, $lastClear] = $this->getClearTime();
if ($this->lastTime + $firstClear < time()) { if ($this->lastTime + $firstClear < time()) {
$this->flush(0); $this->flush(0);
} else if ($this->lastTime + $lastClear < time()) { } else if ($this->lastTime + $lastClear < time()) {
$this->flush(2); $this->flush(2);
} }
} }
} }
/** /**
* @param $retain_number * @param $retain_number
* @throws Exception * @throws Exception
*/ */
protected function flush($retain_number) protected function flush($retain_number)
{ {
$channels = $this->getChannels(); $channels = $this->getChannels();
foreach ($channels as $name => $channel) { foreach ($channels as $name => $channel) {
$names[] = $name; $names[] = $name;
$this->pop($channel, $name, $retain_number); $this->pop($channel, $name, $retain_number);
} }
static::$_items = []; static::$_items = [];
if ($retain_number == 0) { if ($retain_number == 0) {
Timer::clear($this->creates); Timer::clear($this->creates);
$this->creates = -1; $this->creates = -1;
} }
} }
/** /**
* @param $name * @param $name
*/ */
protected function clearCreateLog($name): void protected function clearCreateLog($name): void
{ {
if (!isset(static::$hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
return; return;
} }
static::$hasCreate[$name] = 0; static::$hasCreate[$name] = 0;
} }
/** /**
* @param $channel * @param $channel
* @param $name * @param $name
* @param $retain_number * @param $retain_number
* @throws Exception * @throws Exception
*/ */
protected function pop($channel, $name, $retain_number): void protected function pop($channel, $name, $retain_number): void
{ {
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return; return;
} }
while ($channel->length() > $retain_number) { while ($channel->length() > $retain_number) {
$connection = $channel->pop(); $connection = $channel->pop();
if ($connection) { if ($connection) {
unset($connection); unset($connection);
} }
$this->decrement($name); $this->decrement($name);
} }
} }
/** /**
* @param $driver * @param $driver
* @param $name * @param $name
* @param false $isMaster * @param false $isMaster
* @param int $max * @param int $max
*/ */
public function initConnections($driver, $name, $isMaster = false, $max = 60) public function initConnections($driver, $name, $isMaster = false, $max = 60)
{ {
$name = $this->name($driver, $name, $isMaster); $name = $this->name($driver, $name, $isMaster);
if (isset(static::$_items[$name]) && static::$_items[$name] instanceof Channel) { if (isset(static::$_items[$name]) && static::$_items[$name] instanceof Channel) {
return; return;
} }
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return; return;
} }
static::$_items[$name] = new Channel((int)$max); static::$_items[$name] = new Channel((int)$max);
$this->max = (int)$max; $this->max = (int)$max;
} }
/** /**
* @param $name * @param $name
* @param array $callback * @param array $callback
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
protected function getFromChannel($name, mixed $callback): mixed protected function getFromChannel($name, mixed $callback): mixed
{ {
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return $this->createClient($name, $callback); return $this->createClient($name, $callback);
} }
if (!isset(static::$_items[$name])) { $channel = static::$_items[$name] ?? new Channel($this->max);
static::$_items[$name] = new Channel($this->max); if (!isset(static::$_items[$name])) {
} static::$_items[$name] = $channel;
if (static::$_items[$name]->isEmpty()) { }
$this->createByCallback($name, $callback); if ($channel->isEmpty()) {
} $this->createByCallback($channel, $name, $callback);
$connection = static::$_items[$name]->pop(); }
if (!$this->checkCanUse($name, $connection)) { $connection = $channel->pop();
return $this->createClient($name, $callback); if (!$this->checkCanUse($name, $connection)) {
} else { return $this->createClient($name, $callback);
return $connection; } else {
} return $connection;
} }
}
/** /**
* @param $name * @param $channel
* @param mixed $callback * @param $name
* @throws Exception * @param mixed $callback
*/ */
private function createByCallback($name, mixed $callback): void private function createByCallback(Channel $channel, $name, mixed $callback): void
{ {
if ($this->creates === -1 && !is_callable($callback)) { if ($this->creates === -1 && !is_callable($callback)) {
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
} }
static::$_items[$name]->push($this->createClient($name, $callback)); $channel->push($this->createClient($name, $callback));
} }
abstract public function createClient(string $name, mixed $config): mixed; abstract public function createClient(string $name, mixed $config): mixed;
/** /**
* @param $driver * @param $driver
* @param $cds * @param $cds
* @param false $isMaster * @param false $isMaster
* @return string * @return string
*/ */
#[Pure] public function name($driver, $cds, $isMaster = false): string #[Pure] public function name($driver, $cds, $isMaster = false): string
{ {
if ($isMaster === true) { if ($isMaster === true) {
return $cds . '_master'; return $cds . '_master';
} else { } else {
return $cds . '_slave'; return $cds . '_slave';
} }
} }
/** /**
* @param string $name * @param string $name
* @param $client * @param $client
* @return bool * @return bool
* 检查连接可靠性 * 检查连接可靠性
*/ */
public function checkCanUse(string $name, mixed $client): bool public function checkCanUse(string $name, mixed $client): bool
{ {
return true; return true;
} }
/** /**
* @param array $config * @param array $config
* @param $isMaster * @param $isMaster
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function get(mixed $config, bool $isMaster): mixed public function get(mixed $config, bool $isMaster): mixed
{ {
throw new Exception('Undefined system processing function.'); throw new Exception('Undefined system processing function.');
} }
/** /**
* @param string $name * @param string $name
* @return bool * @return bool
*/ */
public function canCreate(string $name): bool public function canCreate(string $name): bool
{ {
if (!isset(static::$hasCreate[$name])) { if (!isset(static::$hasCreate[$name])) {
static::$hasCreate[$name] = 0; static::$hasCreate[$name] = 0;
} }
return static::$hasCreate[$name] < $this->max; return static::$hasCreate[$name] < $this->max;
} }
/** /**
* @param $name * @param $name
* @return bool * @return bool
*/ */
public function hasItem(string $name): bool public function hasItem(string $name): bool
{ {
if (isset(static::$_items[$name])) { if (isset(static::$_items[$name])) {
return !static::$_items[$name]->isEmpty(); return !static::$_items[$name]->isEmpty();
} }
return false; return false;
} }
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
*/ */
public function size(string $name): mixed public function size(string $name): mixed
{ {
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return 0; return 0;
} }
if (!isset(static::$_items[$name])) { if (!isset(static::$_items[$name])) {
return 0; return 0;
} }
return static::$_items[$name]->length(); return static::$_items[$name]->length();
} }
/** /**
* @param $name * @param $name
* @param $client * @param $client
*/ */
public function push(string $name, mixed $client) public function push(string $name, mixed $client)
{ {
if (Coroutine::getCid() === -1) { if (Coroutine::getCid() === -1) {
return; return;
} }
if (!isset(static::$_items[$name])) { if (!isset(static::$_items[$name])) {
static::$_items[$name] = new Channel($this->max); static::$_items[$name] = new Channel($this->max);
} }
if (!static::$_items[$name]->isFull()) { if (!static::$_items[$name]->isFull()) {
static::$_items[$name]->push($client); static::$_items[$name]->push($client);
} }
unset($client); unset($client);
} }
/** /**
* @param string $name * @param string $name
* @throws Exception * @throws Exception
*/ */
public function clean(string $name) public function clean(string $name)
{ {
if (Coroutine::getCid() === -1 || !isset(static::$_items[$name])) { if (Coroutine::getCid() === -1 || !isset(static::$_items[$name])) {
return; return;
} }
$channel = static::$_items[$name]; $channel = static::$_items[$name];
$this->pop($channel, $name, 0); $this->pop($channel, $name, 0);
if ($this->creates > -1) { if ($this->creates > -1) {
Timer::clear($this->creates); Timer::clear($this->creates);
} }
static::$_items[$name] = null; static::$_items[$name] = null;
} }
/** /**
* @return Channel[] * @return Channel[]
*/ */
protected function getChannels(): array protected function getChannels(): array
{ {
return static::$_items; return static::$_items;
} }
} }