From c0474055de5f027309462b44643b3562210db282 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Wed, 9 Oct 2024 18:12:38 +0800 Subject: [PATCH 1/4] feat: pool --- src/Exceptions/PoolException.php | 12 ++ src/Utils/Pool/Pool.php | 203 +++++++++++++++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 src/Exceptions/PoolException.php create mode 100644 src/Utils/Pool/Pool.php diff --git a/src/Exceptions/PoolException.php b/src/Exceptions/PoolException.php new file mode 100644 index 0000000..3bc93a0 --- /dev/null +++ b/src/Exceptions/PoolException.php @@ -0,0 +1,12 @@ + $pool) { + // Pool + if ($pool instanceof Pool) { + if (!$force) { + wait_for(function () use ($pool) { + return $pool->isIdle(); + }); + } + unset(self::$pools[$name][$in]); + continue; + } + // 非Pool则为数组 + foreach ($pool as $i => $p) { + if (!$force) { + wait_for(function () use ($p) { + return $p->isIdle(); + }); + } + unset(self::$pools[$name][$i]); + } + } + } + + } + + /** + * 获取 + * + * @param string $name + * @param int|null $index + * @return Pool|Pool[]|null + */ + public static function get(string $name, ?int $index): Pool|array|null + { + $pools = self::$pools[$name] ?? []; + return $index !== null ? ($pools[$index] ?? null) : $pools; + } + + + + /** + * 构造 + * + * @param string $name + * @param int $index 索引 + * @param object|array|resource|mixed $element + */ + public function __construct(string $name, int $index, mixed $element) + { + if (static::get($name, $index)) { + throw new PoolException("Pool $name#$index already exists. ", -2); + } + $this->_name = $name; + $this->_index = $index; + $this->setIdle(true); + $this->_element = match (true) { + is_object($element), is_array($element) => clone $element, + is_callable($element) => call_user_func($element), + default => $element, + }; + } + + /** + * 析构等待销毁 + */ + public function __destruct() + { + wait_for(function () { + return $this->isIdle(); + }); + } + + /** + * 获取所在区域名称 + * + * @return string + */ + public function getName(): string + { + return $this->_name; + } + + /** + * 所在区域的索引 + * + * @return int + */ + public function getIndex(): int + { + return $this->_index; + } + + /** + * 获取元素 + * + * @return resource|object|array|mixed + */ + public function getElement(): mixed + { + return $this->_element; + } + + /** + * 是否闲置 + * + * @return bool + */ + public function isIdle(): bool + { + return $this->_idle; + } + + /** + * 设置状态 + * + * @param bool $idle + * @return void + */ + public function setIdle(bool $idle): void + { + $this->_idle = $idle; + } + +} \ No newline at end of file From 8fc732f6664149266df9f8ce4b8edb5ec2a6d6ac Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Wed, 9 Oct 2024 18:18:11 +0800 Subject: [PATCH 2/4] feat: pool --- src/Utils/Pool/Pool.php | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Utils/Pool/Pool.php b/src/Utils/Pool/Pool.php index 8ccc1c7..7397764 100644 --- a/src/Utils/Pool/Pool.php +++ b/src/Utils/Pool/Pool.php @@ -88,12 +88,14 @@ public static function destroy(string $name, ?int $index, bool $force = false): unset(self::$pools[$name][$in]); continue; } - // 非Pool则为数组 + /** + * 非Pool则为数组 + * @var int $i + * @var Pool $p + */ foreach ($pool as $i => $p) { if (!$force) { - wait_for(function () use ($p) { - return $p->isIdle(); - }); + $p->waitForIdle(); } unset(self::$pools[$name][$i]); } @@ -144,9 +146,7 @@ public function __construct(string $name, int $index, mixed $element) */ public function __destruct() { - wait_for(function () { - return $this->isIdle(); - }); + $this->waitForIdle(); } /** @@ -200,4 +200,19 @@ public function setIdle(bool $idle): void $this->_idle = $idle; } + /** + * 等待至闲置 + * + * @param \Closure|null $closure + * @return void + */ + public function waitForIdle(?\Closure $closure = null): void + { + wait_for(function () { + return $this->isIdle(); + }); + if ($closure) { + call_user_func($closure, $this); + } + } } \ No newline at end of file From edce12a3c6b43d8fb5fd61f26177a909e24f39a8 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Thu, 10 Oct 2024 14:01:24 +0800 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20=E7=94=B1=E4=BA=8Eripple=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=86workerman5.x=E7=9A=84=E6=94=AF=E6=92=91?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=94=AF=E6=8C=81ripple=20workerman?= =?UTF-8?q?=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 80f30a3..410342b 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,7 @@ "friendsofphp/php-cs-fixer": "^3.0", "symfony/var-dumper": "^6.0", "swoole/ide-helper": "^5.1", - "swow/swow": "^1.5", - "revolt/event-loop": "^1.0" + "swow/swow": "^1.5" }, "autoload": { "psr-4": { From 185551a2d5998e92e7d8b5487ca4cfdf3de2c5b1 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Thu, 10 Oct 2024 14:38:40 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20=E5=AF=B9=E8=B1=A1=E6=B1=A0?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Utils/Pool/Pool.php | 81 +++++++++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/src/Utils/Pool/Pool.php b/src/Utils/Pool/Pool.php index 7397764..2161d96 100644 --- a/src/Utils/Pool/Pool.php +++ b/src/Utils/Pool/Pool.php @@ -46,6 +46,13 @@ class Pool */ protected bool $_idle; + /** + * 是否强制回收 + * + * @var bool + */ + protected bool $_force; + /** * 创建 * @@ -80,11 +87,7 @@ public static function destroy(string $name, ?int $index, bool $force = false): foreach ($pools as $in => $pool) { // Pool if ($pool instanceof Pool) { - if (!$force) { - wait_for(function () use ($pool) { - return $pool->isIdle(); - }); - } + $pool->setForce($force); unset(self::$pools[$name][$in]); continue; } @@ -94,9 +97,7 @@ public static function destroy(string $name, ?int $index, bool $force = false): * @var Pool $p */ foreach ($pool as $i => $p) { - if (!$force) { - $p->waitForIdle(); - } + $p->setForce($force); unset(self::$pools[$name][$i]); } } @@ -117,7 +118,41 @@ public static function get(string $name, ?int $index): Pool|array|null return $index !== null ? ($pools[$index] ?? null) : $pools; } + /** + * 获取空闲池 + * + * @param string $name + * @return Pool|null + */ + public static function idle(string $name): Pool|null + { + $pools = self::get($name, null); + // 总是按顺序优先获取空闲 + foreach ($pools as $pool) { + if ($pool->isIdle()) { + return $pool; + } + } + return null; + } + /** + * 等待空闲并执行 + * + * @param string $name 池区域 + * @param \Closure $closure 被执行函数 = function (Pool $pool) {} + * @param int $timeout 超时时间 + * @return mixed + */ + public static function waitForIdle(string $name, \Closure $closure, int $timeout = -1): mixed + { + $pool = null; + wait_for(function () use (&$pool, $name) { + $pool = self::idle($name); + return $pool !== null; + }, $timeout); + return call_user_func($closure, $pool); + } /** * 构造 @@ -131,8 +166,9 @@ public function __construct(string $name, int $index, mixed $element) if (static::get($name, $index)) { throw new PoolException("Pool $name#$index already exists. ", -2); } - $this->_name = $name; + $this->_name = $name; $this->_index = $index; + $this->setForce(false); $this->setIdle(true); $this->_element = match (true) { is_object($element), is_array($element) => clone $element, @@ -146,7 +182,9 @@ public function __construct(string $name, int $index, mixed $element) */ public function __destruct() { - $this->waitForIdle(); + if (!$this->isForce()) { + $this->wait(); + } } /** @@ -200,13 +238,34 @@ public function setIdle(bool $idle): void $this->_idle = $idle; } + /** + * 是否强制回收 + * + * @return bool + */ + public function isForce(): bool + { + return $this->_force; + } + + /** + * 设置强制回收 + * + * @param bool $force + * @return void + */ + public function setForce(bool $force): void + { + $this->_force = $force; + } + /** * 等待至闲置 * * @param \Closure|null $closure * @return void */ - public function waitForIdle(?\Closure $closure = null): void + public function wait(?\Closure $closure = null): void { wait_for(function () { return $this->isIdle();