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
|
|
|
{
|
2021-02-17 00:06:03 +08:00
|
|
|
if ($client = Context::getContext($coroutineName)) {
|
2021-02-16 23:47:41 +08:00
|
|
|
return $client;
|
2021-01-04 18:40:11 +08:00
|
|
|
}
|
2021-02-17 00:06:03 +08:00
|
|
|
if (Context::hasContext('create:connect:' . $coroutineName)) {
|
2021-02-20 12:21:44 +08:00
|
|
|
while ($client = Context::getContext($coroutineName)) {
|
|
|
|
|
Coroutine::sleep(0.001);
|
|
|
|
|
}
|
|
|
|
|
return $client;
|
2021-02-17 00:06:03 +08:00
|
|
|
}
|
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-16 23:47:41 +08:00
|
|
|
return Context::setContext($coroutineName, $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:01 +08:00
|
|
|
|
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
|
|
|
}
|