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

71 lines
1.1 KiB
PHP
Raw Normal View History

2021-02-17 00:51:21 +08:00
<?php
namespace Snowflake\Pool;
use Exception;
use Swoole\Timer;
trait Timeout
{
2021-02-20 13:21:23 +08:00
public int $creates = -1;
2021-02-17 00:51:21 +08:00
2021-02-20 13:21:23 +08:00
public int $lastTime = 0;
2021-02-17 00:51:21 +08:00
/**
* @throws Exception
*/
2021-02-20 13:25:09 +08:00
public function Heartbeat_detection()
2021-02-17 00:51:21 +08:00
{
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) {
2021-02-20 13:43:12 +08:00
$names[] = $name;
2021-02-17 00:51:21 +08:00
$this->pop($channel, $name, $retain_number);
}
if ($retain_number == 0) {
$this->debug('release Timer::tick');
Timer::clear($this->creates);
2021-02-20 13:29:56 +08:00
$this->creates = -1;
2021-02-17 00:51:21 +08:00
}
}
/**
* @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);
}
}
}