Skip to content

Commit ee2e124

Browse files
committed
feat:object-pool:完善类型 DialerInterface 没有处理,因为会导致 php >= 7.4
1 parent 1c0ff19 commit ee2e124

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

src/object-pool/src/AbstractObjectPool.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function __construct(DialerInterface $dialer, int $maxOpen = -1, int $max
9090
* 创建连接
9191
* @return object
9292
*/
93-
protected function createConnection()
93+
protected function createConnection(): object
9494
{
9595
// 连接创建时会挂起当前协程,导致 actives 未增加,因此需先 actives++ 连接创建成功后 actives--
9696
$closure = function () {
@@ -115,10 +115,9 @@ protected function createConnection()
115115
* @return object
116116
* @throws WaitTimeoutException
117117
*/
118-
public function borrow()
118+
public function borrow(): object
119119
{
120120
/** @var ObjectTrait $connection */
121-
$connection = null;
122121
if ($this->getIdleNumber() > 0 || ($this->maxOpen && $this->getTotalNumber() >= $this->maxOpen)) {
123122
// 队列有连接,从队列取
124123
// 达到最大连接数,从队列取
@@ -143,10 +142,10 @@ public function borrow()
143142

144143
/**
145144
* 归还连接
146-
* @param $connection
145+
* @param object $connection
147146
* @return bool
148147
*/
149-
public function return(object $connection)
148+
public function return(object $connection): bool
150149
{
151150
$id = spl_object_hash($connection);
152151
// 判断是否已释放
@@ -161,10 +160,10 @@ public function return(object $connection)
161160

162161
/**
163162
* 丢弃连接
164-
* @param $connection
163+
* @param object $connection
165164
* @return bool
166165
*/
167-
public function discard(object $connection)
166+
public function discard(object $connection): bool
168167
{
169168
$id = spl_object_hash($connection);
170169
// 判断是否已丢弃
@@ -181,7 +180,7 @@ public function discard(object $connection)
181180
* 获取连接池的统计信息
182181
* @return array
183182
*/
184-
public function stats()
183+
public function stats(): array
185184
{
186185
return [
187186
'total' => $this->getTotalNumber(),
@@ -192,10 +191,10 @@ public function stats()
192191

193192
/**
194193
* 放入连接
195-
* @param $connection
194+
* @param object $connection
196195
* @return bool
197196
*/
198-
protected function push($connection)
197+
protected function push(object $connection): bool
199198
{
200199
// 解决对象在协程外部析构导致的: Swoole\Error: API must be called in the coroutine
201200
if (Coroutine::getCid() == -1) {
@@ -206,11 +205,11 @@ protected function push($connection)
206205

207206
/**
208207
* 弹出连接
209-
* @return mixed
208+
* @return object
210209
* @throws WaitTimeoutException
211210
* @throws Exception
212211
*/
213-
protected function pop()
212+
protected function pop(): object
214213
{
215214
$timeout = -1;
216215
if ($this->waitTimeout) {
@@ -230,7 +229,7 @@ protected function pop()
230229
* 获取队列中的连接数
231230
* @return int
232231
*/
233-
protected function getIdleNumber()
232+
protected function getIdleNumber(): int
234233
{
235234
$count = $this->queue->stats()['queue_num'];
236235
return $count < 0 ? 0 : $count;
@@ -240,7 +239,7 @@ protected function getIdleNumber()
240239
* 获取活跃的连接数
241240
* @return int
242241
*/
243-
protected function getActiveNumber()
242+
protected function getActiveNumber(): int
244243
{
245244
return count($this->actives);
246245
}
@@ -249,7 +248,7 @@ protected function getActiveNumber()
249248
* 获取当前总连接数
250249
* @return int
251250
*/
252-
protected function getTotalNumber()
251+
protected function getTotalNumber(): int
253252
{
254253
return $this->getIdleNumber() + $this->getActiveNumber();
255254
}

src/object-pool/src/ObjectTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ trait ObjectTrait
2323
* 丢弃连接
2424
* @return bool
2525
*/
26-
public function __discard()
26+
public function __discard(): bool
2727
{
2828
if (isset($this->pool)) {
2929
return $this->pool->discard($this);
@@ -35,7 +35,7 @@ public function __discard()
3535
* 归还连接
3636
* @return bool
3737
*/
38-
public function __return()
38+
public function __return(): bool
3939
{
4040
if (isset($this->pool)) {
4141
return $this->pool->return($this);

0 commit comments

Comments
 (0)