modify
This commit is contained in:
+279
-275
@@ -19,314 +19,318 @@ use Swoole\Timer;
|
|||||||
abstract class Pool extends Component
|
abstract class Pool extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var Channel[] */
|
/** @var Channel[] */
|
||||||
private array $_items = [];
|
private 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 array $hasCreate = [];
|
protected array $hasCreate = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public function increment(string $name)
|
public function increment(string $name)
|
||||||
{
|
{
|
||||||
if (!isset($this->hasCreate[$name])) {
|
if (!isset($this->hasCreate[$name])) {
|
||||||
$this->hasCreate[$name] = 0;
|
$this->hasCreate[$name] = 0;
|
||||||
}
|
}
|
||||||
$this->hasCreate[$name] += 1;
|
$this->hasCreate[$name] += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public function decrement(string $name)
|
public function decrement(string $name)
|
||||||
{
|
{
|
||||||
if (!isset($this->hasCreate[$name])) {
|
if (!isset($this->hasCreate[$name])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($this->hasCreate[$name] <= 0) {
|
if ($this->hasCreate[$name] <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->hasCreate[$name] -= 1;
|
$this->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);
|
||||||
}
|
}
|
||||||
if ($retain_number == 0) {
|
$this->_items = [];
|
||||||
Timer::clear($this->creates);
|
if ($retain_number == 0) {
|
||||||
$this->creates = -1;
|
Timer::clear($this->creates);
|
||||||
}
|
$this->creates = -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
*/
|
*/
|
||||||
protected function clearCreateLog($name): void
|
protected function clearCreateLog($name): void
|
||||||
{
|
{
|
||||||
if (!isset($this->hasCreate[$name])) {
|
if (!isset($this->hasCreate[$name])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->hasCreate[$name] = 0;
|
$this->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
|
||||||
{
|
{
|
||||||
while ($channel->length() > $retain_number) {
|
|
||||||
$connection = $channel->pop();
|
|
||||||
if ($connection) {
|
|
||||||
unset($connection);
|
|
||||||
}
|
|
||||||
$this->decrement($name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $driver
|
|
||||||
* @param $name
|
|
||||||
* @param false $isMaster
|
|
||||||
* @param int $max
|
|
||||||
*/
|
|
||||||
public function initConnections($driver, $name, $isMaster = false, $max = 60)
|
|
||||||
{
|
|
||||||
$name = $this->name($driver, $name, $isMaster);
|
|
||||||
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->_items[$name] = new Channel((int)$max);
|
while ($channel->length() > $retain_number) {
|
||||||
$this->max = (int)$max;
|
$connection = $channel->pop();
|
||||||
}
|
if ($connection) {
|
||||||
|
unset($connection);
|
||||||
|
}
|
||||||
|
$this->decrement($name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $driver
|
||||||
* @param array $callback
|
* @param $name
|
||||||
* @return array
|
* @param false $isMaster
|
||||||
* @throws Exception
|
* @param int $max
|
||||||
*/
|
*/
|
||||||
protected function getFromChannel($name, mixed $callback): mixed
|
public function initConnections($driver, $name, $isMaster = false, $max = 60)
|
||||||
{
|
{
|
||||||
|
$name = $this->name($driver, $name, $isMaster);
|
||||||
|
if (isset($this->_items[$name]) && $this->_items[$name] instanceof Channel) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
return $this->createClient($name, $callback);
|
return;
|
||||||
}
|
}
|
||||||
if (!isset($this->_items[$name])) {
|
$this->_items[$name] = new Channel((int)$max);
|
||||||
$this->_items[$name] = new Channel($this->max);
|
$this->max = (int)$max;
|
||||||
}
|
}
|
||||||
if ($this->_items[$name]->isEmpty()) {
|
|
||||||
$this->createByCallback($name, $callback);
|
|
||||||
}
|
|
||||||
$connection = $this->_items[$name]->pop();
|
|
||||||
if (!$this->checkCanUse($name, $connection)) {
|
|
||||||
return $this->createClient($name, $callback);
|
|
||||||
} else {
|
|
||||||
return $connection;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param mixed $callback
|
* @param array $callback
|
||||||
* @throws Exception
|
* @return array
|
||||||
*/
|
* @throws Exception
|
||||||
private function createByCallback($name, mixed $callback): void
|
*/
|
||||||
{
|
protected function getFromChannel($name, mixed $callback): mixed
|
||||||
if ($this->creates === -1 && !is_callable($callback)) {
|
{
|
||||||
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
|
|
||||||
}
|
|
||||||
$this->_items[$name]->push($this->createClient($name, $callback));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
abstract public function createClient(string $name, mixed $config): mixed;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $driver
|
|
||||||
* @param $cds
|
|
||||||
* @param false $isMaster
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
#[Pure] public function name($driver, $cds, $isMaster = false): string
|
|
||||||
{
|
|
||||||
if ($isMaster === true) {
|
|
||||||
return $cds . '_master';
|
|
||||||
} else {
|
|
||||||
return $cds . '_slave';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @param $client
|
|
||||||
* @return bool
|
|
||||||
* 检查连接可靠性
|
|
||||||
*/
|
|
||||||
public function checkCanUse(string $name, mixed $client): bool
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $config
|
|
||||||
* @param $isMaster
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function get(mixed $config, bool $isMaster): mixed
|
|
||||||
{
|
|
||||||
throw new Exception('Undefined system processing function.');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $name
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function canCreate(string $name): bool
|
|
||||||
{
|
|
||||||
if (!isset($this->hasCreate[$name])) {
|
|
||||||
$this->hasCreate[$name] = 0;
|
|
||||||
}
|
|
||||||
return $this->hasCreate[$name] < $this->max;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasItem(string $name): bool
|
|
||||||
{
|
|
||||||
if (isset($this->_items[$name])) {
|
|
||||||
return !$this->_items[$name]->isEmpty();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function size(string $name): mixed
|
|
||||||
{
|
|
||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
return 0;
|
return $this->createClient($name, $callback);
|
||||||
}
|
}
|
||||||
if (!isset($this->_items[$name])) {
|
if (!isset($this->_items[$name])) {
|
||||||
return 0;
|
$this->_items[$name] = new Channel($this->max);
|
||||||
}
|
}
|
||||||
return $this->_items[$name]->length();
|
if ($this->_items[$name]->isEmpty()) {
|
||||||
}
|
$this->createByCallback($name, $callback);
|
||||||
|
}
|
||||||
|
$connection = $this->_items[$name]->pop();
|
||||||
|
if (!$this->checkCanUse($name, $connection)) {
|
||||||
|
return $this->createClient($name, $callback);
|
||||||
|
} else {
|
||||||
|
return $connection;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $client
|
* @param mixed $callback
|
||||||
*/
|
* @throws Exception
|
||||||
public function push(string $name, mixed $client)
|
*/
|
||||||
{
|
private function createByCallback($name, mixed $callback): void
|
||||||
|
{
|
||||||
|
if ($this->creates === -1 && !is_callable($callback)) {
|
||||||
|
$this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']);
|
||||||
|
}
|
||||||
|
$this->_items[$name]->push($this->createClient($name, $callback));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
abstract public function createClient(string $name, mixed $config): mixed;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $driver
|
||||||
|
* @param $cds
|
||||||
|
* @param false $isMaster
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
#[Pure] public function name($driver, $cds, $isMaster = false): string
|
||||||
|
{
|
||||||
|
if ($isMaster === true) {
|
||||||
|
return $cds . '_master';
|
||||||
|
} else {
|
||||||
|
return $cds . '_slave';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @param $client
|
||||||
|
* @return bool
|
||||||
|
* 检查连接可靠性
|
||||||
|
*/
|
||||||
|
public function checkCanUse(string $name, mixed $client): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $config
|
||||||
|
* @param $isMaster
|
||||||
|
* @return mixed
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function get(mixed $config, bool $isMaster): mixed
|
||||||
|
{
|
||||||
|
throw new Exception('Undefined system processing function.');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function canCreate(string $name): bool
|
||||||
|
{
|
||||||
|
if (!isset($this->hasCreate[$name])) {
|
||||||
|
$this->hasCreate[$name] = 0;
|
||||||
|
}
|
||||||
|
return $this->hasCreate[$name] < $this->max;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasItem(string $name): bool
|
||||||
|
{
|
||||||
|
if (isset($this->_items[$name])) {
|
||||||
|
return !$this->_items[$name]->isEmpty();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function size(string $name): mixed
|
||||||
|
{
|
||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
if (!isset($this->_items[$name])) {
|
if (!isset($this->_items[$name])) {
|
||||||
$this->_items[$name] = new Channel($this->max);
|
return 0;
|
||||||
}
|
}
|
||||||
if (!$this->_items[$name]->isFull()) {
|
return $this->_items[$name]->length();
|
||||||
$this->_items[$name]->push($client);
|
}
|
||||||
}
|
|
||||||
unset($client);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param $name
|
||||||
* @throws Exception
|
* @param $client
|
||||||
*/
|
*/
|
||||||
public function clean(string $name)
|
public function push(string $name, mixed $client)
|
||||||
{
|
{
|
||||||
if (Coroutine::getCid() === -1 || !isset($this->_items[$name])) {
|
if (Coroutine::getCid() === -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$channel = $this->_items[$name];
|
if (!isset($this->_items[$name])) {
|
||||||
$this->pop($channel, $name, 0);
|
$this->_items[$name] = new Channel($this->max);
|
||||||
if ($this->creates > -1) {
|
}
|
||||||
Timer::clear($this->creates);
|
if (!$this->_items[$name]->isFull()) {
|
||||||
}
|
$this->_items[$name]->push($client);
|
||||||
$this->_items[$name] = null;
|
}
|
||||||
}
|
unset($client);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Channel[]
|
* @param string $name
|
||||||
*/
|
* @throws Exception
|
||||||
protected function getChannels(): array
|
*/
|
||||||
{
|
public function clean(string $name)
|
||||||
return $this->_items;
|
{
|
||||||
}
|
if (Coroutine::getCid() === -1 || !isset($this->_items[$name])) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$channel = $this->_items[$name];
|
||||||
|
$this->pop($channel, $name, 0);
|
||||||
|
if ($this->creates > -1) {
|
||||||
|
Timer::clear($this->creates);
|
||||||
|
}
|
||||||
|
$this->_items[$name] = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Channel[]
|
||||||
|
*/
|
||||||
|
protected function getChannels(): array
|
||||||
|
{
|
||||||
|
return $this->_items;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user