googleanalytics
/
lib
/
analytics-admin
/
vendor
/
google
/
gax
/
src
/
Middleware
/
RetryMiddleware.php
CredentialsWrapperMiddleware.php
3 years ago
FixedHeaderMiddleware.php
3 years ago
OperationsMiddleware.php
3 years ago
OptionsFilterMiddleware.php
3 years ago
PagedMiddleware.php
3 years ago
ResponseMetadataMiddleware.php
3 years ago
RetryMiddleware.php
3 years ago
RetryMiddleware.php
159 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright 2018 Google LLC |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | namespace Google\ApiCore\Middleware; |
| 33 | |
| 34 | use Google\ApiCore\ApiException; |
| 35 | use Google\ApiCore\ApiStatus; |
| 36 | use Google\ApiCore\Call; |
| 37 | use Google\ApiCore\RetrySettings; |
| 38 | use GuzzleHttp\Promise\PromiseInterface; |
| 39 | |
| 40 | /** |
| 41 | * Middleware that adds retry functionality. |
| 42 | */ |
| 43 | class RetryMiddleware |
| 44 | { |
| 45 | /** @var callable */ |
| 46 | private $nextHandler; |
| 47 | |
| 48 | /** @var RetrySettings */ |
| 49 | private $retrySettings; |
| 50 | |
| 51 | /** @var float|null */ |
| 52 | private $deadlineMs; |
| 53 | |
| 54 | public function __construct( |
| 55 | callable $nextHandler, |
| 56 | RetrySettings $retrySettings, |
| 57 | $deadlineMs = null |
| 58 | ) { |
| 59 | $this->nextHandler = $nextHandler; |
| 60 | $this->retrySettings = $retrySettings; |
| 61 | $this->deadlineMs = $deadlineMs; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param Call $call |
| 66 | * @param array $options |
| 67 | * |
| 68 | * @return PromiseInterface |
| 69 | */ |
| 70 | public function __invoke(Call $call, array $options) |
| 71 | { |
| 72 | $nextHandler = $this->nextHandler; |
| 73 | |
| 74 | if (!isset($options['timeoutMillis'])) { |
| 75 | // default to "noRetriesRpcTimeoutMillis" when retries are disabled, otherwise use "initialRpcTimeoutMillis" |
| 76 | if (!$this->retrySettings->retriesEnabled() && $this->retrySettings->getNoRetriesRpcTimeoutMillis() > 0) { |
| 77 | $options['timeoutMillis'] = $this->retrySettings->getNoRetriesRpcTimeoutMillis(); |
| 78 | } elseif ($this->retrySettings->getInitialRpcTimeoutMillis() > 0) { |
| 79 | $options['timeoutMillis'] = $this->retrySettings->getInitialRpcTimeoutMillis(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Call the handler immediately if retry settings are disabled. |
| 84 | if (!$this->retrySettings->retriesEnabled()) { |
| 85 | return $nextHandler($call, $options); |
| 86 | } |
| 87 | |
| 88 | return $nextHandler($call, $options)->then(null, function ($e) use ($call, $options) { |
| 89 | if (!$e instanceof ApiException) { |
| 90 | throw $e; |
| 91 | } |
| 92 | |
| 93 | if (!in_array($e->getStatus(), $this->retrySettings->getRetryableCodes())) { |
| 94 | throw $e; |
| 95 | } |
| 96 | |
| 97 | return $this->retry($call, $options, $e->getStatus()); |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @param Call $call |
| 103 | * @param array $options |
| 104 | * @param string $status |
| 105 | * |
| 106 | * @return PromiseInterface |
| 107 | * @throws ApiException |
| 108 | */ |
| 109 | private function retry(Call $call, array $options, string $status) |
| 110 | { |
| 111 | $delayMult = $this->retrySettings->getRetryDelayMultiplier(); |
| 112 | $maxDelayMs = $this->retrySettings->getMaxRetryDelayMillis(); |
| 113 | $timeoutMult = $this->retrySettings->getRpcTimeoutMultiplier(); |
| 114 | $maxTimeoutMs = $this->retrySettings->getMaxRpcTimeoutMillis(); |
| 115 | $totalTimeoutMs = $this->retrySettings->getTotalTimeoutMillis(); |
| 116 | |
| 117 | $delayMs = $this->retrySettings->getInitialRetryDelayMillis(); |
| 118 | $timeoutMs = $options['timeoutMillis']; |
| 119 | $currentTimeMs = $this->getCurrentTimeMs(); |
| 120 | $deadlineMs = $this->deadlineMs ?: $currentTimeMs + $totalTimeoutMs; |
| 121 | |
| 122 | if ($currentTimeMs >= $deadlineMs) { |
| 123 | throw new ApiException( |
| 124 | 'Retry total timeout exceeded.', |
| 125 | \Google\Rpc\Code::DEADLINE_EXCEEDED, |
| 126 | ApiStatus::DEADLINE_EXCEEDED |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | $delayMs = min($delayMs * $delayMult, $maxDelayMs); |
| 131 | $timeoutMs = min( |
| 132 | $timeoutMs * $timeoutMult, |
| 133 | $maxTimeoutMs, |
| 134 | $deadlineMs - $this->getCurrentTimeMs() |
| 135 | ); |
| 136 | |
| 137 | $nextHandler = new RetryMiddleware( |
| 138 | $this->nextHandler, |
| 139 | $this->retrySettings->with([ |
| 140 | 'initialRetryDelayMillis' => $delayMs, |
| 141 | ]), |
| 142 | $deadlineMs |
| 143 | ); |
| 144 | |
| 145 | // Set the timeout for the call |
| 146 | $options['timeoutMillis'] = $timeoutMs; |
| 147 | |
| 148 | return $nextHandler( |
| 149 | $call, |
| 150 | $options |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | protected function getCurrentTimeMs() |
| 155 | { |
| 156 | return microtime(true) * 1000.0; |
| 157 | } |
| 158 | } |
| 159 |