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
|
|
|
|
2021-01-04 18:40:11 +08:00
|
|
|
use HttpServer\Http\Context;
|
2021-02-15 16:09:36 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2021-01-05 10:23:20 +08:00
|
|
|
use PDO;
|
|
|
|
|
use Redis;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Swoole\Coroutine;
|
|
|
|
|
use Swoole\Coroutine\Channel;
|
2021-02-15 20:31:01 +08:00
|
|
|
use Swoole\Timer;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
2021-01-19 18:44:19 +08:00
|
|
|
if (!Context::inCoroutine()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-19 18:01:33 +08:00
|
|
|
$this->_items[$name] = new Channel((int)$max);
|
|
|
|
|
$this->max = (int)$max;
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-15 10:59:13 +08:00
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-01-19 18:44:19 +08:00
|
|
|
if (!Context::inCoroutine()) {
|
|
|
|
|
return [0, null];
|
|
|
|
|
}
|
2021-02-15 10:59:13 +08:00
|
|
|
[$timeout, $connection] = $this->_items[$name]->pop(-1);
|
2021-02-03 18:53:14 +08:00
|
|
|
if (empty($timeout) || empty($connection)) {
|
|
|
|
|
return [0, null];
|
|
|
|
|
}
|
2020-09-04 19:16:09 +08:00
|
|
|
if (!$this->checkCanUse($name, $timeout, $connection)) {
|
2020-08-31 01:27:08 +08:00
|
|
|
return [0, null];
|
|
|
|
|
} else {
|
2020-09-05 01:29:46 +08:00
|
|
|
return [$timeout, $connection];
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 10:59:13 +08:00
|
|
|
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
/**
|
|
|
|
|
* @param $cds
|
|
|
|
|
* @param false $isMaster
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-02-15 16:09:36 +08:00
|
|
|
#[Pure] 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.');
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-04 18:40:11 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $config
|
|
|
|
|
* @param string $coroutineName
|
|
|
|
|
* @param callable $createHandler
|
2021-01-05 10:23:20 +08:00
|
|
|
* @return PDO|Redis|null
|
|
|
|
|
* @throws Exception
|
2021-01-04 18:40:11 +08:00
|
|
|
*/
|
2021-02-15 20:45:35 +08:00
|
|
|
public function createConnect(array $config, string $coroutineName, callable $createHandler): PDO|Redis|false
|
2021-01-04 18:40:11 +08:00
|
|
|
{
|
|
|
|
|
if (Context::hasContext('create:connect:' . $coroutineName)) {
|
2021-02-16 23:44:37 +08:00
|
|
|
while (!($client = Context::hasContext($coroutineName))){
|
|
|
|
|
var_dump($client);
|
2021-02-16 23:42:13 +08:00
|
|
|
}
|
|
|
|
|
return true;
|
2021-01-04 18:40:11 +08:00
|
|
|
}
|
|
|
|
|
Context::setContext('create:connect:' . $coroutineName, 1);
|
|
|
|
|
|
2021-01-19 18:47:38 +08:00
|
|
|
$client = call_user_func($createHandler, ...$config);
|
|
|
|
|
|
2021-01-04 18:40:11 +08:00
|
|
|
Context::deleteId('create:connect:' . $coroutineName);
|
2021-01-05 10:23:20 +08:00
|
|
|
|
2021-02-15 10:46:06 +08:00
|
|
|
return $client;
|
2021-01-04 18:40:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
{
|
2021-02-03 18:35:52 +08:00
|
|
|
if (isset($this->_items[$name])) {
|
2021-02-03 18:52:25 +08:00
|
|
|
return !$this->_items[$name]->isEmpty();
|
2021-02-03 18:35:52 +08:00
|
|
|
}
|
|
|
|
|
return false;
|
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
|
|
|
{
|
2021-01-19 18:44:19 +08:00
|
|
|
if (!Context::inCoroutine()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
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
|
|
|
{
|
2021-01-19 18:44:19 +08:00
|
|
|
if (!Context::inCoroutine()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-04 18:54:27 +08:00
|
|
|
if (!$this->_items[$name]->isFull()) {
|
|
|
|
|
$this->_items[$name]->push([time(), $client]);
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-17 14:09:14 +08:00
|
|
|
* @param string $name
|
2021-01-05 16:32:48 +08:00
|
|
|
* @throws Exception
|
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
|
|
|
{
|
2021-01-19 18:44:19 +08:00
|
|
|
if (!Context::inCoroutine()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
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);
|
2021-01-05 16:32:48 +08:00
|
|
|
|
|
|
|
|
$this->desc($name);
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-15 20:31:53 +08:00
|
|
|
protected int $creates = 0;
|
2021-02-15 20:31:01 +08:00
|
|
|
|
|
|
|
|
|
2021-02-15 20:31:53 +08:00
|
|
|
protected int $lastTime = 0;
|
2021-02-15 20:31:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $timer
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function Heartbeat_detection($timer)
|
|
|
|
|
{
|
|
|
|
|
$this->creates = $timer;
|
|
|
|
|
if ($this->lastTime == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($this->lastTime + 60 < time()) {
|
|
|
|
|
$this->flush(0);
|
|
|
|
|
} else if ($this->lastTime + 30 < time()) {
|
|
|
|
|
$this->flush(2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $retain_number
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function flush($retain_number)
|
|
|
|
|
{
|
|
|
|
|
$channels = $this->getChannels();
|
|
|
|
|
foreach ($channels as $name => $channel) {
|
|
|
|
|
$this->pop($channel, $name, $retain_number);
|
|
|
|
|
}
|
|
|
|
|
if ($retain_number == 0) {
|
|
|
|
|
$this->debug('release Timer::tick');
|
|
|
|
|
Timer::clear($this->creates);
|
|
|
|
|
$this->creates = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $channel
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param $retain_number
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function pop($channel, $name, $retain_number)
|
|
|
|
|
{
|
|
|
|
|
while ($channel->length() > $retain_number) {
|
|
|
|
|
[$timer, $connection] = $channel->pop();
|
|
|
|
|
if ($connection) {
|
|
|
|
|
unset($connection);
|
|
|
|
|
}
|
|
|
|
|
$this->desc($name);
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
}
|