Skip to content

Commit

Permalink
feat:object-pool:完善类型 DialerInterface 没有处理,因为会导致 php >= 7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
onanying committed Jul 22, 2021
1 parent 1c0ff19 commit ee2e124
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
29 changes: 14 additions & 15 deletions src/object-pool/src/AbstractObjectPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __construct(DialerInterface $dialer, int $maxOpen = -1, int $max
* 创建连接
* @return object
*/
protected function createConnection()
protected function createConnection(): object
{
// 连接创建时会挂起当前协程,导致 actives 未增加,因此需先 actives++ 连接创建成功后 actives--
$closure = function () {
Expand All @@ -115,10 +115,9 @@ protected function createConnection()
* @return object
* @throws WaitTimeoutException
*/
public function borrow()
public function borrow(): object
{
/** @var ObjectTrait $connection */
$connection = null;
if ($this->getIdleNumber() > 0 || ($this->maxOpen && $this->getTotalNumber() >= $this->maxOpen)) {
// 队列有连接,从队列取
// 达到最大连接数,从队列取
Expand All @@ -143,10 +142,10 @@ public function borrow()

/**
* 归还连接
* @param $connection
* @param object $connection
* @return bool
*/
public function return(object $connection)
public function return(object $connection): bool
{
$id = spl_object_hash($connection);
// 判断是否已释放
Expand All @@ -161,10 +160,10 @@ public function return(object $connection)

/**
* 丢弃连接
* @param $connection
* @param object $connection
* @return bool
*/
public function discard(object $connection)
public function discard(object $connection): bool
{
$id = spl_object_hash($connection);
// 判断是否已丢弃
Expand All @@ -181,7 +180,7 @@ public function discard(object $connection)
* 获取连接池的统计信息
* @return array
*/
public function stats()
public function stats(): array
{
return [
'total' => $this->getTotalNumber(),
Expand All @@ -192,10 +191,10 @@ public function stats()

/**
* 放入连接
* @param $connection
* @param object $connection
* @return bool
*/
protected function push($connection)
protected function push(object $connection): bool
{
// 解决对象在协程外部析构导致的: Swoole\Error: API must be called in the coroutine
if (Coroutine::getCid() == -1) {
Expand All @@ -206,11 +205,11 @@ protected function push($connection)

/**
* 弹出连接
* @return mixed
* @return object
* @throws WaitTimeoutException
* @throws Exception
*/
protected function pop()
protected function pop(): object
{
$timeout = -1;
if ($this->waitTimeout) {
Expand All @@ -230,7 +229,7 @@ protected function pop()
* 获取队列中的连接数
* @return int
*/
protected function getIdleNumber()
protected function getIdleNumber(): int
{
$count = $this->queue->stats()['queue_num'];
return $count < 0 ? 0 : $count;
Expand All @@ -240,7 +239,7 @@ protected function getIdleNumber()
* 获取活跃的连接数
* @return int
*/
protected function getActiveNumber()
protected function getActiveNumber(): int
{
return count($this->actives);
}
Expand All @@ -249,7 +248,7 @@ protected function getActiveNumber()
* 获取当前总连接数
* @return int
*/
protected function getTotalNumber()
protected function getTotalNumber(): int
{
return $this->getIdleNumber() + $this->getActiveNumber();
}
Expand Down
4 changes: 2 additions & 2 deletions src/object-pool/src/ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ trait ObjectTrait
* 丢弃连接
* @return bool
*/
public function __discard()
public function __discard(): bool
{
if (isset($this->pool)) {
return $this->pool->discard($this);
Expand All @@ -35,7 +35,7 @@ public function __discard()
* 归还连接
* @return bool
*/
public function __return()
public function __return(): bool
{
if (isset($this->pool)) {
return $this->pool->return($this);
Expand Down

0 comments on commit ee2e124

Please sign in to comment.