Skip to content

Commit 2caf9a8

Browse files
committed
feat Utils/Pool
1 parent bdac16f commit 2caf9a8

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/Utils/Pool/Pool.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function Workbunny\WebmanCoroutine\wait_for;
1515

1616
use Workerman\Worker;
17+
use function Workbunny\WebmanCoroutine\wakeup;
1718

1819
class Pool
1920
{
@@ -203,11 +204,13 @@ public static function idle(string $name): Pool|null
203204
public static function getIdle(string $name, int $timeout = -1): Pool
204205
{
205206
$pool = null;
207+
// 使用pool.idle.$name事件等待空闲对象
206208
wait_for(function () use (&$pool, $name) {
207209
$pool = self::idle($name);
208210

209211
return $pool !== null;
210-
}, $timeout);
212+
}, timeout: $timeout, event: "pool.idle.$name");
213+
// 获取到的对象加锁
211214
$pool->setIdle(false);
212215

213216
return $pool;
@@ -366,6 +369,14 @@ public function isIdle(): bool
366369
public function setIdle(bool $idle): void
367370
{
368371
$this->_idle = $idle;
372+
if ($idle) {
373+
$id = spl_object_hash($this);
374+
$name = $this->getName();
375+
// 唤醒pool.wait.$name.$id
376+
wakeup("pool.wait.$name.$id");
377+
// 唤醒pool.idle.$name
378+
wakeup("pool.idle.$name");
379+
}
369380
}
370381

371382
/**
@@ -397,9 +408,12 @@ public function setForce(bool $force): void
397408
*/
398409
public function wait(?\Closure $closure = null): void
399410
{
411+
$id = spl_object_hash($this);
412+
$name = $this->getName();
413+
// 永久等待pool.wait.$name.$id唤醒事件
400414
wait_for(function () {
401415
return $this->isIdle();
402-
});
416+
}, timeout: -1, event: "pool.wait.$name.$id");
403417
if ($closure) {
404418
$this->setIdle(false);
405419
try {

0 commit comments

Comments
 (0)