From 0b1be47fb6a6c15eb4060fc1f4fea8933038c10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Thu, 20 Jul 2023 15:01:24 +0800 Subject: [PATCH] qqq --- kiri-engine/Pool/PoolItem.php | 163 +++++++++++++++++----------------- 1 file changed, 83 insertions(+), 80 deletions(-) diff --git a/kiri-engine/Pool/PoolItem.php b/kiri-engine/Pool/PoolItem.php index 62ddb55e..a5fdc832 100644 --- a/kiri-engine/Pool/PoolItem.php +++ b/kiri-engine/Pool/PoolItem.php @@ -11,105 +11,108 @@ class PoolItem { - /** - * @var Channel|SplQueue - */ - private Channel|SplQueue $_items; + /** + * @var Channel|SplQueue + */ + private Channel|SplQueue $_items; - /** - * @var int - */ - private int $created = 0; + /** + * @var int + */ + private int $created = 0; - /** - * @param int $maxCreated - * @param Closure $callback - */ - public function __construct(readonly public int $maxCreated, readonly public Closure $callback) - { - if (Context::inCoroutine()) { - $this->_items = new Channel($this->maxCreated); - } else { - $this->_items = new SplQueue($this->maxCreated); - } - } + /** + * @param int $maxCreated + * @param Closure $callback + */ + public function __construct(readonly public int $maxCreated, readonly public Closure $callback) + { + if (Context::inCoroutine()) { + $this->_items = new Channel($this->maxCreated); + } else { + $this->_items = new SplQueue($this->maxCreated); + } + } - /** - * @param Channel|SplQueue $items - */ - public function setItems(Channel|SplQueue $items): void - { - $this->_items = $items; - } + /** + * @param Channel|SplQueue $items + */ + public function setItems(Channel|SplQueue $items): void + { + $this->_items = $items; + } - /** - * @param mixed $item - * @return void - */ - public function push(mixed $item): void - { - $this->_items->push($item); - } + /** + * @param mixed $item + * @return void + */ + public function push(mixed $item): void + { + if (is_null($item)) { + $item = call_user_func($this->callback); + } + $this->_items->push($item); + } - /** - * @return bool - */ - public function isEmpty(): bool - { - return $this->_items->isEmpty(); - } + /** + * @return bool + */ + public function isEmpty(): bool + { + return $this->_items->isEmpty(); + } /** * @return int */ - public function size(): int - { - return $this->_items->length(); - } + public function size(): int + { + return $this->_items->length(); + } - /** - * @return bool - */ - public function close(): bool - { - return $this->_items->close(); - } + /** + * @return bool + */ + public function close(): bool + { + return $this->_items->close(); + } - /** - * @param int $min - * @return void - */ - public function tailor(int $min = 0): void - { - while ($this->_items->length() > $min) { - $connection = $this->_items->pop(0.000001); - if ($connection instanceof StopHeartbeatCheck) { - $connection->stopHeartbeatCheck(); - } - $connection = null; - $this->created -= 1; - } - } + /** + * @param int $min + * @return void + */ + public function tailor(int $min = 0): void + { + while ($this->_items->length() > $min) { + $connection = $this->_items->pop(0.000001); + if ($connection instanceof StopHeartbeatCheck) { + $connection->stopHeartbeatCheck(); + } + $connection = null; + $this->created -= 1; + } + } - /** - * @param int $waite - * @return mixed - */ - public function pop(int $waite = 10): mixed - { - if ($this->_items->isEmpty()) { - return call_user_func($this->callback); - } else { - return $this->_items->pop(); - } - } + /** + * @param int $waite + * @return mixed + */ + public function pop(int $waite = 10): mixed + { + if ($this->_items->isEmpty()) { + return call_user_func($this->callback); + } else { + return $this->_items->pop($waite); + } + } }