This commit is contained in:
2023-07-20 15:01:24 +08:00
parent 221183e196
commit 0b1be47fb6
+83 -80
View File
@@ -11,105 +11,108 @@ class PoolItem
{ {
/** /**
* @var Channel|SplQueue * @var Channel|SplQueue
*/ */
private Channel|SplQueue $_items; private Channel|SplQueue $_items;
/** /**
* @var int * @var int
*/ */
private int $created = 0; private int $created = 0;
/** /**
* @param int $maxCreated * @param int $maxCreated
* @param Closure $callback * @param Closure $callback
*/ */
public function __construct(readonly public int $maxCreated, readonly public Closure $callback) public function __construct(readonly public int $maxCreated, readonly public Closure $callback)
{ {
if (Context::inCoroutine()) { if (Context::inCoroutine()) {
$this->_items = new Channel($this->maxCreated); $this->_items = new Channel($this->maxCreated);
} else { } else {
$this->_items = new SplQueue($this->maxCreated); $this->_items = new SplQueue($this->maxCreated);
} }
} }
/** /**
* @param Channel|SplQueue $items * @param Channel|SplQueue $items
*/ */
public function setItems(Channel|SplQueue $items): void public function setItems(Channel|SplQueue $items): void
{ {
$this->_items = $items; $this->_items = $items;
} }
/** /**
* @param mixed $item * @param mixed $item
* @return void * @return void
*/ */
public function push(mixed $item): void public function push(mixed $item): void
{ {
$this->_items->push($item); if (is_null($item)) {
} $item = call_user_func($this->callback);
}
$this->_items->push($item);
}
/** /**
* @return bool * @return bool
*/ */
public function isEmpty(): bool public function isEmpty(): bool
{ {
return $this->_items->isEmpty(); return $this->_items->isEmpty();
} }
/** /**
* @return int * @return int
*/ */
public function size(): int public function size(): int
{ {
return $this->_items->length(); return $this->_items->length();
} }
/** /**
* @return bool * @return bool
*/ */
public function close(): bool public function close(): bool
{ {
return $this->_items->close(); return $this->_items->close();
} }
/** /**
* @param int $min * @param int $min
* @return void * @return void
*/ */
public function tailor(int $min = 0): void public function tailor(int $min = 0): void
{ {
while ($this->_items->length() > $min) { while ($this->_items->length() > $min) {
$connection = $this->_items->pop(0.000001); $connection = $this->_items->pop(0.000001);
if ($connection instanceof StopHeartbeatCheck) { if ($connection instanceof StopHeartbeatCheck) {
$connection->stopHeartbeatCheck(); $connection->stopHeartbeatCheck();
} }
$connection = null; $connection = null;
$this->created -= 1; $this->created -= 1;
} }
} }
/** /**
* @param int $waite * @param int $waite
* @return mixed * @return mixed
*/ */
public function pop(int $waite = 10): mixed public function pop(int $waite = 10): mixed
{ {
if ($this->_items->isEmpty()) { if ($this->_items->isEmpty()) {
return call_user_func($this->callback); return call_user_func($this->callback);
} else { } else {
return $this->_items->pop(); return $this->_items->pop($waite);
} }
} }
} }