From 399d5ce2e79989b9bb657f94c0293813c7057e18 Mon Sep 17 00:00:00 2001 From: iorsa <32541698+iorsa@users.noreply.github.com> Date: Mon, 6 Dec 2021 15:50:52 +0500 Subject: [PATCH] Update ConnectionPool.php According to some issues when `ConnectionState` enum (of $connectionFactory Closure/Class, which creates connection adapter) isn't `Ready` and `retryWithDelay()` function is called repeatedly. Btw, here is even more readable-"friendly" `retryWithDelay()` function variation: ``` protected function retryWithDelay(Deferred $deferred = null): \React\Promise\Promise { if ($this->retryLimit !== null && $this->retryLimit < 1) { throw new ConnectionPoolException('No available connection to use'); } $deferred ??= new Deferred(); if (!$this->awaiting->contains($deferred)) { $this->awaiting->attach($deferred, 0); } if ($this->awaiting[$deferred] === $this->retryLimit) { $this->awaiting->detach($deferred); throw new ConnectionPoolException("No available connection to use; $this->retryLimit attempts were made"); } $selectConnectionDeferred = new Deferred(); $this->loop->addTimer($this->retryEverySec, function () use ($deferred, $selectConnectionDeferred) { $connectionAdapter = $this->selectConnection(); if ($connectionAdapter) { $this->awaiting->detach($deferred); $selectConnectionDeferred->resolve(true); $deferred->resolve($connectionAdapter); return; } $this->awaiting[$deferred] = $this->awaiting[$deferred] + 1; $selectConnectionDeferred->resolve(false); }); return $selectConnectionDeferred->promise()->then(function($onnectionSelected) use ($deferred) { return $onnectionSelected ? $deferred->promise() : $this->retryWithDelay($deferred); }); } ``` --- src/ConnectionPool.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ConnectionPool.php b/src/ConnectionPool.php index adee022..dfa23fc 100644 --- a/src/ConnectionPool.php +++ b/src/ConnectionPool.php @@ -55,7 +55,7 @@ public function get(): ConnectionAdapterInterface $this->connections->attach($this->makeNewConnection()); return $this->get(); } - return $this->retryWithDelay(); + return await($this->retryWithDelay()); } return $connection; @@ -76,7 +76,7 @@ protected function makeNewConnection(): ConnectionAdapterInterface return ($this->connectionFactory)(); } - protected function retryWithDelay(Deferred $deferred = null): ConnectionAdapterInterface + protected function retryWithDelay(Deferred $deferred = null): \React\Promise\Promise { if ($this->retryLimit !== null && $this->retryLimit < 1) { throw new ConnectionPoolException('No available connection to use'); @@ -106,6 +106,6 @@ protected function retryWithDelay(Deferred $deferred = null): ConnectionAdapterI $this->retryWithDelay($deferred); }); - return await($deferred->promise()); + return $deferred->promise(); } -} \ No newline at end of file +}