From cfda39ebdde994269d337ade512d3ef8735620cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 24 Feb 2021 14:53:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Abstracts/Pool.php | 96 ++++++++++++++++++++++++++++++++++---- System/Pool/ObjectPool.php | 5 +- System/Pool/Timeout.php | 70 --------------------------- 3 files changed, 92 insertions(+), 79 deletions(-) delete mode 100644 System/Pool/Timeout.php diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index ccbb4b93..1aeb996c 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -11,6 +11,7 @@ use JetBrains\PhpStorm\Pure; use PDO; use Redis; use Snowflake\Exception\ComponentException; +use Snowflake\Exception\ConfigException; use Snowflake\Pool\Timeout; use Swoole\Coroutine; use Swoole\Coroutine\Channel; @@ -28,7 +29,76 @@ abstract class Pool extends Component public int $max = 60; - use Timeout; + public int $creates = -1; + + public int $lastTime = 0; + + + /** + * @return array + * @throws ConfigException + */ + private function getClearTime(): array + { + $firstClear = Config::get('pool.clear.start', false, 600); + $lastClear = Config::get('pool.clear.end', false, 300); + return [$firstClear, $lastClear]; + } + + + /** + * @throws Exception + */ + public function Heartbeat_detection() + { + if ($this->lastTime == 0) { + return; + } + [$firstClear, $lastClear] = $this->getClearTime(); + if ($this->lastTime + $firstClear < time()) { + $this->flush(0); + } else if ($this->lastTime + $lastClear < time()) { + $this->flush(2); + } + } + + + /** + * @param $retain_number + * @throws Exception + */ + protected function flush($retain_number) + { + $channels = $this->getChannels(); + foreach ($channels as $name => $channel) { + $names[] = $name; + $this->pop($channel, $name, $retain_number); + } + if ($retain_number == 0) { + $this->debug('release Timer::tick'); + Timer::clear($this->creates); + $this->creates = -1; + } + } + + + /** + * @param $channel + * @param $name + * @param $retain_number + * @throws Exception + */ + protected function pop($channel, $name, $retain_number) + { + while ($channel->length() > $retain_number) { + $connection = $channel->pop(); + if ($connection) { + unset($connection); + } + $this->desc($name); + } + } + /** * @param $driver @@ -62,13 +132,7 @@ abstract class Pool extends Component return $this->createClient($name, $callback); } if (!$this->hasItem($name)) { - if ($this->creates === -1 && !is_callable($callback)) { - $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); - } - if (!Context::hasContext('create::client::ing::' . $name)) { - $this->push($name, $this->createClient($name, $callback)); - Context::deleteContext('create::client::ing::' . $name); - } + $this->createByCallback($name, $callback); } $connection = $this->_items[$name]->pop(-1); if (!$this->checkCanUse($name, $connection)) { @@ -79,6 +143,22 @@ abstract class Pool extends Component } + /** + * @param $name + * @param mixed $callback + */ + private function createByCallback($name, mixed $callback) + { + if ($this->creates === -1 && !is_callable($callback)) { + $this->creates = Timer::tick(1000, [$this, 'Heartbeat_detection']); + } + if (!Context::hasContext('create::client::ing::' . $name)) { + $this->push($name, $this->createClient($name, $callback)); + Context::deleteContext('create::client::ing::' . $name); + } + } + + /** * @param $cds * @param $coroutineName diff --git a/System/Pool/ObjectPool.php b/System/Pool/ObjectPool.php index bcd8d42b..4c089d5d 100644 --- a/System/Pool/ObjectPool.php +++ b/System/Pool/ObjectPool.php @@ -33,7 +33,10 @@ class ObjectPool extends \Snowflake\Abstracts\Pool if (is_object($config)) { return $config; } - $object = $this->getFromChannel($name = md5($config), [$config, $construct]); + + $className = hash('sha384', $config); + + $object = $this->getFromChannel($className, [$config, $construct]); if (method_exists($object, 'clean')) { $object->clean(); } diff --git a/System/Pool/Timeout.php b/System/Pool/Timeout.php deleted file mode 100644 index 2234a1e0..00000000 --- a/System/Pool/Timeout.php +++ /dev/null @@ -1,70 +0,0 @@ -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) { - $names[] = $name; - $this->pop($channel, $name, $retain_number); - } - if ($retain_number == 0) { - $this->debug('release Timer::tick'); - Timer::clear($this->creates); - $this->creates = -1; - } - } - - - /** - * @param $channel - * @param $name - * @param $retain_number - * @throws Exception - */ - protected function pop($channel, $name, $retain_number) - { - while ($channel->length() > $retain_number) { - $connection = $channel->pop(); - if ($connection) { - unset($connection); - } - $this->desc($name); - } - } -}