This commit is contained in:
2020-09-06 05:37:00 +08:00
parent 4089f0f456
commit 238bcae813
2 changed files with 56 additions and 3 deletions
+12 -2
View File
@@ -126,7 +126,6 @@ abstract class Pool extends Component
{ {
$this->_items[$name]->push([time(), $client]); $this->_items[$name]->push([time(), $client]);
unset($client); unset($client);
$this->debug('release connect.' . $this->max . ':' . $this->size($name));
} }
@@ -138,9 +137,20 @@ abstract class Pool extends Component
if (!isset($this->_items[$name])) { if (!isset($this->_items[$name])) {
return; return;
} }
while ([$time, $client] = $this->_items[$name]->pop(0.001)) { $channel = $this->_items[$name];
while ([$time, $client] = $channel->pop(0.001)) {
unset($client); unset($client);
} }
} }
/**
* @return Channel[]
*/
protected function getChannels()
{
return $this->_items;
}
} }
+44 -1
View File
@@ -7,6 +7,7 @@ use PDO;
use Exception; use Exception;
use Swoole\Coroutine; use Swoole\Coroutine;
use Snowflake\Abstracts\Pool; use Snowflake\Abstracts\Pool;
use Swoole\Timer;
/** /**
* Class Connection * Class Connection
@@ -25,6 +26,48 @@ class Connection extends Pool
private $creates = 0; private $creates = 0;
public function init()
{
if ($this->creates) {
Timer::clear($this->creates);
}
Timer::tick(30000, [$this, 'Heartbeat_detection']);
}
public $lastTime = 0;
/**
* @param $timer
*/
public function Heartbeat_detection($timer)
{
$this->creates = $timer;
if ($this->lastTime == 0) {
return;
}
if ($this->lastTime + 600 < time()) {
$channels = $this->getChannels();
foreach ($channels as $name => $channel) {
while ($channel->length() > 0) {
$channel->pop();
$this->desc($name);
}
}
} else if ($this->lastTime + 500 < time()) {
$channels = $this->getChannels();
foreach ($channels as $name => $channel) {
while ($channel->length() > 5) {
$channel->pop();
$this->desc($name);
}
}
}
}
/** /**
* @param $timeout * @param $timeout
*/ */
@@ -158,7 +201,6 @@ class Connection extends Pool
if (Context::hasContext($coroutineName)) { if (Context::hasContext($coroutineName)) {
return Context::getContext($coroutineName); return Context::getContext($coroutineName);
} }
// if ($this->size($coroutineName) < 1) {
if ($this->size($coroutineName) < 1 && $this->hasCreate[$coroutineName] < $this->max) { if ($this->size($coroutineName) < 1 && $this->hasCreate[$coroutineName] < $this->max) {
return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config)); return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config));
} }
@@ -216,6 +258,7 @@ class Connection extends Pool
} }
$this->push($coroutineName, $client); $this->push($coroutineName, $client);
$this->remove($coroutineName); $this->remove($coroutineName);
$this->lastTime = time();
} }