Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2445380bd9 | |||
| 577bd478c3 |
+30
-14
@@ -18,14 +18,14 @@ class PoolItem
|
||||
|
||||
|
||||
/**
|
||||
* @var int
|
||||
* @var int 当前已创建的连接数
|
||||
*/
|
||||
private int $created = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $maxCreated
|
||||
* @param Closure|array $callback
|
||||
* @param int $maxCreated 最大允许创建的连接数
|
||||
* @param 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
|
||||
{
|
||||
@@ -46,6 +46,7 @@ class PoolItem
|
||||
|
||||
|
||||
/**
|
||||
* 重新创建内部存储队列 Channel 或 SplQueue
|
||||
* @return void
|
||||
*/
|
||||
public function reconnect(): void
|
||||
@@ -55,6 +56,7 @@ class PoolItem
|
||||
} else {
|
||||
$this->_items = new SplQueue($this->maxCreated);
|
||||
}
|
||||
$this->created = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +70,8 @@ class PoolItem
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $item
|
||||
* 将连接推回池中
|
||||
* @param mixed $item 连接对象
|
||||
* @return void
|
||||
*/
|
||||
public function push(mixed $item): void
|
||||
@@ -81,7 +84,7 @@ class PoolItem
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @return bool 判断池是否为空
|
||||
*/
|
||||
public function isEmpty(): bool
|
||||
{
|
||||
@@ -90,7 +93,7 @@ class PoolItem
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
* @return int 当前池中可用连接数
|
||||
*/
|
||||
public function size(): int
|
||||
{
|
||||
@@ -99,7 +102,7 @@ class PoolItem
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @return bool 关闭连接池
|
||||
*/
|
||||
public function close(): bool
|
||||
{
|
||||
@@ -108,7 +111,8 @@ class PoolItem
|
||||
|
||||
|
||||
/**
|
||||
* @param int $min
|
||||
* 缩容连接池,将连接数缩减至 min 个
|
||||
* @param int $min 保留的最小连接数
|
||||
* @return void
|
||||
*/
|
||||
public function tailor(int $min = 0): void
|
||||
@@ -118,30 +122,42 @@ class PoolItem
|
||||
if ($connection instanceof StopHeartbeatCheck) {
|
||||
$connection->stopHeartbeatCheck();
|
||||
}
|
||||
// 关闭连接释放底层资源
|
||||
if (is_object($connection) && method_exists($connection, 'close')) {
|
||||
$connection->close();
|
||||
}
|
||||
$this->created--;
|
||||
$connection = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建连接失败时回退计数器
|
||||
* @return void
|
||||
*/
|
||||
public function abandon(): void
|
||||
{
|
||||
if ($this->created > 0) {
|
||||
$this->created -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $waite
|
||||
* @return mixed
|
||||
* 从连接池中获取一个连接
|
||||
* 当未达到最大创建数时直接创建新连接,达到上限后阻塞等待归还
|
||||
* @param int $waite 等待超时时间(秒)
|
||||
* @return mixed 连接对象,超时返回 false
|
||||
*/
|
||||
public function pop(int $waite = 10): mixed
|
||||
{
|
||||
if ($this->_items->isEmpty()) {
|
||||
// 未达到最大创建数,直接创建新连接
|
||||
if ($this->created < $this->maxCreated) {
|
||||
$this->created++;
|
||||
return call_user_func($this->callback);
|
||||
} else {
|
||||
}
|
||||
// 已达到最大创建数,阻塞等待连接归还
|
||||
return $this->_items->pop($waite);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user