← All changes
|
includes/sdk/google/google/gax/src/Middleware/RetryMiddleware.php
+19
-13
1.0.2
→
1.4.2
View file →
| @@ -29,17 +29,19 @@ | ||
| 29 | 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | 32 | */ |
| 33 | -namespace Dudlewebs\WPMCS\Google\ApiCore\Middleware; | |
| 33 | +namespace Dudlewebs\WPMCS\GCP\Google\ApiCore\Middleware; | |
| 34 | 34 | |
| 35 | -use Dudlewebs\WPMCS\Google\ApiCore\ApiException; | |
| 36 | -use Dudlewebs\WPMCS\Google\ApiCore\ApiStatus; | |
| 37 | -use Dudlewebs\WPMCS\Google\ApiCore\Call; | |
| 38 | -use Dudlewebs\WPMCS\Google\ApiCore\RetrySettings; | |
| 39 | -use Dudlewebs\WPMCS\GuzzleHttp\Promise\PromiseInterface; | |
| 35 | +use Dudlewebs\WPMCS\GCP\Google\ApiCore\ApiException; | |
| 36 | +use Dudlewebs\WPMCS\GCP\Google\ApiCore\ApiStatus; | |
| 37 | +use Dudlewebs\WPMCS\GCP\Google\ApiCore\Call; | |
| 38 | +use Dudlewebs\WPMCS\GCP\Google\ApiCore\RetrySettings; | |
| 39 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Promise\PromiseInterface; | |
| 40 | 40 | /** |
| 41 | 41 | * Middleware that adds retry functionality. |
| 42 | + * | |
| 43 | + * @internal | |
| 42 | 44 | */ |
| 43 | 45 | class RetryMiddleware implements MiddlewareInterface |
| 44 | 46 | { |
| 45 | 47 | /** @var callable */ |
| @@ -74,13 +76,17 @@ | ||
| 74 | 76 | } elseif ($this->retrySettings->getInitialRpcTimeoutMillis() > 0) { |
| 75 | 77 | $options['timeoutMillis'] = $this->retrySettings->getInitialRpcTimeoutMillis(); |
| 76 | 78 | } |
| 77 | 79 | } |
| 80 | + // Setting the retry attempt for logging | |
| 81 | + if ($this->retryAttempts > 0) { | |
| 82 | + $options['retryAttempt'] = $this->retryAttempts; | |
| 83 | + } | |
| 78 | 84 | // Call the handler immediately if retry settings are disabled. |
| 79 | 85 | if (!$this->retrySettings->retriesEnabled()) { |
| 80 | 86 | return $nextHandler($call, $options); |
| 81 | 87 | } |
| 82 | - return $nextHandler($call, $options)->then(null, function ($e) use ($call, $options) { | |
| 88 | + return $nextHandler($call, $options)->then(null, function ($e) use($call, $options) { | |
| 83 | 89 | $retryFunction = $this->getRetryFunction(); |
| 84 | 90 | // If the number of retries has surpassed the max allowed retries |
| 85 | 91 | // then throw the exception as we normally would. |
| 86 | 92 | // If the maxRetries is set to 0, then we don't check this condition. |
| @@ -115,12 +121,12 @@ | ||
| 115 | 121 | $timeoutMs = $options['timeoutMillis']; |
| 116 | 122 | $currentTimeMs = $this->getCurrentTimeMs(); |
| 117 | 123 | $deadlineMs = $this->deadlineMs ?: $currentTimeMs + $totalTimeoutMs; |
| 118 | 124 | if ($currentTimeMs >= $deadlineMs) { |
| 119 | - throw new ApiException('Retry total timeout exceeded.', \Dudlewebs\WPMCS\Google\Rpc\Code::DEADLINE_EXCEEDED, ApiStatus::DEADLINE_EXCEEDED); | |
| 125 | + throw new ApiException('Retry total timeout exceeded.', \Dudlewebs\WPMCS\GCP\Google\Rpc\Code::DEADLINE_EXCEEDED, ApiStatus::DEADLINE_EXCEEDED); | |
| 120 | 126 | } |
| 121 | - $delayMs = min($delayMs * $delayMult, $maxDelayMs); | |
| 122 | - $timeoutMs = (int) min($timeoutMs * $timeoutMult, $maxTimeoutMs, $deadlineMs - $this->getCurrentTimeMs()); | |
| 127 | + $delayMs = \min($delayMs * $delayMult, $maxDelayMs); | |
| 128 | + $timeoutMs = (int) \min($timeoutMs * $timeoutMult, $maxTimeoutMs, $deadlineMs - $this->getCurrentTimeMs()); | |
| 123 | 129 | $nextHandler = new RetryMiddleware($this->nextHandler, $this->retrySettings->with(['initialRetryDelayMillis' => $delayMs]), $deadlineMs, $this->retryAttempts + 1); |
| 124 | 130 | // Set the timeout for the call |
| 125 | 131 | $options['timeoutMillis'] = $timeoutMs; |
| 126 | 132 | return $nextHandler($call, $options); |
| @@ -126,9 +132,9 @@ | ||
| 126 | 132 | return $nextHandler($call, $options); |
| 127 | 133 | } |
| 128 | 134 | protected function getCurrentTimeMs() |
| 129 | 135 | { |
| 130 | - return microtime(\true) * 1000.0; | |
| 136 | + return \microtime(\true) * 1000.0; | |
| 131 | 137 | } |
| 132 | 138 | /** |
| 133 | 139 | * This is the default retry behaviour. |
| 134 | 140 | */ |
| @@ -133,9 +139,9 @@ | ||
| 133 | 139 | * This is the default retry behaviour. |
| 134 | 140 | */ |
| 135 | 141 | private function getRetryFunction() |
| 136 | 142 | { |
| 137 | - return $this->retrySettings->getRetryFunction() ?? function (\Throwable $e, array $options): bool { | |
| 143 | + return $this->retrySettings->getRetryFunction() ?? function (\Throwable $e, array $options) : bool { | |
| 138 | 144 | // This is the default retry behaviour, i.e. we don't retry an ApiException |
| 139 | 145 | // and for other exception types, we only retry when the error code is in |
| 140 | 146 | // the list of retryable error codes. |
| 141 | 147 | if (!$e instanceof ApiException) { |
| @@ -140,9 +146,9 @@ | ||
| 140 | 146 | // the list of retryable error codes. |
| 141 | 147 | if (!$e instanceof ApiException) { |
| 142 | 148 | return \false; |
| 143 | 149 | } |
| 144 | - if (!in_array($e->getStatus(), $this->retrySettings->getRetryableCodes())) { | |
| 150 | + if (!\in_array($e->getStatus(), $this->retrySettings->getRetryableCodes())) { | |
| 145 | 151 | return \false; |
| 146 | 152 | } |
| 147 | 153 | return \true; |
| 148 | 154 | }; |