4 Commits

Author SHA1 Message Date
as2252258 2445380bd9 更新 PoolItem.php 2026-06-28 06:34:18 +00:00
as2252258 577bd478c3 eee 2026-06-24 20:11:12 +08:00
as2252258 1ec2bbec2a eee 2026-06-12 23:57:23 +08:00
as2252258 d40e7db483 eee 2025-07-14 15:36:04 +08:00
2 changed files with 164 additions and 148 deletions
+30 -14
View File
@@ -18,14 +18,14 @@ class PoolItem
/** /**
* @var int * @var int 当前已创建的连接数
*/ */
private int $created = 0; private int $created = 0;
/** /**
* @param int $maxCreated * @param int $maxCreated 最大允许创建的连接数
* @param Closure|array $callback * @param Closure|array $callback 创建新连接的回调函数
*/ */
public function __construct(readonly public int $maxCreated, readonly public Closure|array $callback) public function __construct(readonly public int $maxCreated, readonly public Closure|array $callback)
{ {
@@ -34,7 +34,7 @@ class PoolItem
/** /**
* @return bool * @return bool 判断 Channel 是否已关闭
*/ */
public function isClose(): bool public function isClose(): bool
{ {
@@ -46,6 +46,7 @@ class PoolItem
/** /**
* 重新创建内部存储队列 Channel 或 SplQueue
* @return void * @return void
*/ */
public function reconnect(): void public function reconnect(): void
@@ -55,6 +56,7 @@ class PoolItem
} else { } else {
$this->_items = new SplQueue($this->maxCreated); $this->_items = new SplQueue($this->maxCreated);
} }
$this->created = 0;
} }
@@ -68,7 +70,8 @@ class PoolItem
/** /**
* @param mixed $item * 将连接推回池中
* @param mixed $item 连接对象
* @return void * @return void
*/ */
public function push(mixed $item): void public function push(mixed $item): void
@@ -81,7 +84,7 @@ class PoolItem
/** /**
* @return bool * @return bool 判断池是否为空
*/ */
public function isEmpty(): bool public function isEmpty(): bool
{ {
@@ -90,7 +93,7 @@ class PoolItem
/** /**
* @return int * @return int 当前池中可用连接数
*/ */
public function size(): int public function size(): int
{ {
@@ -99,7 +102,7 @@ class PoolItem
/** /**
* @return bool * @return bool 关闭连接池
*/ */
public function close(): bool public function close(): bool
{ {
@@ -108,7 +111,8 @@ class PoolItem
/** /**
* @param int $min * 缩容连接池,将连接数缩减至 min
* @param int $min 保留的最小连接数
* @return void * @return void
*/ */
public function tailor(int $min = 0): void public function tailor(int $min = 0): void
@@ -118,30 +122,42 @@ class PoolItem
if ($connection instanceof StopHeartbeatCheck) { if ($connection instanceof StopHeartbeatCheck) {
$connection->stopHeartbeatCheck(); $connection->stopHeartbeatCheck();
} }
// 关闭连接释放底层资源
if (is_object($connection) && method_exists($connection, 'close')) {
$connection->close();
}
$this->created--;
$connection = null; $connection = null;
} }
} }
/** /**
* 创建连接失败时回退计数器
* @return void * @return void
*/ */
public function abandon(): void public function abandon(): void
{ {
if ($this->created > 0) {
$this->created -= 1; $this->created -= 1;
} }
}
/** /**
* @param int $waite * 从连接池中获取一个连接
* @return mixed * 当未达到最大创建数时直接创建新连接,达到上限后阻塞等待归还
* @param int $waite 等待超时时间(秒)
* @return mixed 连接对象,超时返回 false
*/ */
public function pop(int $waite = 10): mixed public function pop(int $waite = 10): mixed
{ {
if ($this->_items->isEmpty()) { // 未达到最大创建数,直接创建新连接
if ($this->created < $this->maxCreated) {
$this->created++;
return call_user_func($this->callback); return call_user_func($this->callback);
} else { }
// 已达到最大创建数,阻塞等待连接归还
return $this->_items->pop($waite); return $this->_items->pop($waite);
} }
}
} }
+1 -1
View File
@@ -9,7 +9,7 @@
} }
], ],
"require": { "require": {
"php": ">=8.0", "php": ">=8.5",
"composer-runtime-api": "^2.0" "composer-runtime-api": "^2.0"
}, },
"autoload": { "autoload": {