diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index ac36f827..1689a8cd 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -126,7 +126,6 @@ abstract class Pool extends Component { $this->_items[$name]->push([time(), $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])) { return; } - while ([$time, $client] = $this->_items[$name]->pop(0.001)) { + $channel = $this->_items[$name]; + while ([$time, $client] = $channel->pop(0.001)) { unset($client); } } + + /** + * @return Channel[] + */ + protected function getChannels() + { + return $this->_items; + } + + } diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index ddddf0a8..39419fd7 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -7,6 +7,7 @@ use PDO; use Exception; use Swoole\Coroutine; use Snowflake\Abstracts\Pool; +use Swoole\Timer; /** * Class Connection @@ -25,6 +26,48 @@ class Connection extends Pool 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 */ @@ -158,7 +201,6 @@ class Connection extends Pool if (Context::hasContext($coroutineName)) { return Context::getContext($coroutineName); } -// if ($this->size($coroutineName) < 1) { if ($this->size($coroutineName) < 1 && $this->hasCreate[$coroutineName] < $this->max) { return $this->saveClient($coroutineName, $this->nowClient($coroutineName, $config)); } @@ -216,6 +258,7 @@ class Connection extends Pool } $this->push($coroutineName, $client); $this->remove($coroutineName); + $this->lastTime = time(); }