← All changes
|
includes/sdk/google/google/cloud-storage/src/Connection/RetryTrait.php
+19
-14
1.2.3
→
1.4.1
View file →
| @@ -14,11 +14,11 @@ | ||
| 14 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | 15 | * See the License for the specific language governing permissions and |
| 16 | 16 | * limitations under the License. |
| 17 | 17 | */ |
| 18 | -namespace Dudlewebs\WPMCS\Google\Cloud\Storage\Connection; | |
| 18 | +namespace Dudlewebs\WPMCS\GCP\Google\Cloud\Storage\Connection; | |
| 19 | 19 | |
| 20 | -use Dudlewebs\WPMCS\Google\Cloud\Storage\StorageClient; | |
| 20 | +use Dudlewebs\WPMCS\GCP\Google\Cloud\Storage\StorageClient; | |
| 21 | 21 | /** |
| 22 | 22 | * Trait which provides helper methods for retry logic. |
| 23 | 23 | * |
| 24 | 24 | * @internal |
| @@ -30,9 +30,9 @@ | ||
| 30 | 30 | * @var array |
| 31 | 31 | */ |
| 32 | 32 | private static $httpRetryCodes = [ |
| 33 | 33 | 0, |
| 34 | - // connetion-refused OR connection-reset gives status code of 0 | |
| 34 | + // connection-refused OR connection-reset gives status code of 0 | |
| 35 | 35 | 200, |
| 36 | 36 | // partial download cases |
| 37 | 37 | 408, |
| 38 | 38 | 429, |
| @@ -45,9 +45,9 @@ | ||
| 45 | 45 | * The operations which can be retried without any conditions |
| 46 | 46 | * (Idempotent) |
| 47 | 47 | * @var array |
| 48 | 48 | */ |
| 49 | - private static $idempotentOps = ['bucket_acl.get', 'bucket_acl.list', 'buckets.delete', 'buckets.get', 'buckets.getIamPolicy', 'buckets.insert', 'buckets.list', 'buckets.lockRetentionPolicy', 'buckets.testIamPermissions', 'default_object_acl.get', 'default_object_acl.list', 'hmacKey.delete', 'hmacKey.get', 'hmacKey.list', 'notifications.delete', 'notifications.get', 'notifications.list', 'object_acl.get', 'object_acl.list', 'objects.get', 'objects.list', 'serviceaccount.get']; | |
| 49 | + private static $idempotentOps = ['bucket_acl.get', 'bucket_acl.list', 'buckets.delete', 'buckets.get', 'buckets.getIamPolicy', 'buckets.insert', 'buckets.list', 'buckets.lockRetentionPolicy', 'buckets.testIamPermissions', 'default_object_acl.get', 'default_object_acl.list', 'hmacKey.delete', 'hmacKey.get', 'hmacKey.list', 'notifications.delete', 'notifications.get', 'notifications.list', 'object_acl.get', 'object_acl.list', 'objects.get', 'objects.list', 'serviceaccount.get', 'signBlob.execute']; | |
| 50 | 50 | /** |
| 51 | 51 | * The operations which can be retried with specific conditions |
| 52 | 52 | * (Conditionally idempotent) |
| 53 | 53 | * @var array |
| @@ -96,15 +96,15 @@ | ||
| 96 | 96 | { |
| 97 | 97 | if (isset($args['restRetryFunction'])) { |
| 98 | 98 | return $args['restRetryFunction']; |
| 99 | 99 | } |
| 100 | - $methodName = sprintf('%s.%s', $resource, $method); | |
| 101 | - $isOpIdempotent = in_array($methodName, self::$idempotentOps); | |
| 102 | - $preconditionNeeded = array_key_exists($methodName, self::$condIdempotentOps); | |
| 100 | + $methodName = \sprintf('%s.%s', $resource, $method); | |
| 101 | + $isOpIdempotent = \in_array($methodName, self::$idempotentOps); | |
| 102 | + $preconditionNeeded = \array_key_exists($methodName, self::$condIdempotentOps); | |
| 103 | 103 | $preconditionSupplied = $this->isPreConditionSupplied($methodName, $args); |
| 104 | 104 | $retryStrategy = isset($args['retryStrategy']) ? $args['retryStrategy'] : StorageClient::RETRY_IDEMPOTENT; |
| 105 | - return function (\Exception $exception) use ($isOpIdempotent, $preconditionNeeded, $preconditionSupplied, $retryStrategy) { | |
| 106 | - return $this->retryDeciderFunction($exception, $isOpIdempotent, $preconditionNeeded, $preconditionSupplied, $retryStrategy); | |
| 105 | + return function (\Exception $exception, $currentAttempt = 0, $maxRetries = null) use($isOpIdempotent, $preconditionNeeded, $preconditionSupplied, $retryStrategy) { | |
| 106 | + return $this->retryDeciderFunction($exception, $isOpIdempotent, $preconditionNeeded, $preconditionSupplied, $retryStrategy, $currentAttempt, $maxRetries); | |
| 107 | 107 | }; |
| 108 | 108 | } |
| 109 | 109 | /** |
| 110 | 110 | * This function returns true when the user given |
| @@ -124,9 +124,9 @@ | ||
| 124 | 124 | private function isPreConditionSupplied($methodName, array $args) |
| 125 | 125 | { |
| 126 | 126 | if (isset(self::$condIdempotentOps[$methodName])) { |
| 127 | 127 | // return true if required precondition are given. |
| 128 | - return !empty(array_intersect(self::$condIdempotentOps[$methodName], array_keys($args))); | |
| 128 | + return !empty(\array_intersect(self::$condIdempotentOps[$methodName], \array_keys($args))); | |
| 129 | 129 | } |
| 130 | 130 | return \false; |
| 131 | 131 | } |
| 132 | 132 | /** |
| @@ -137,13 +137,18 @@ | ||
| 137 | 137 | * @param int $currentAttempt Current retry attempt. |
| 138 | 138 | * @param bool $isIdempotent |
| 139 | 139 | * @param bool $preconditionNeeded |
| 140 | 140 | * @param bool $preconditionSupplied |
| 141 | - * @param int $maxRetries | |
| 141 | + * @param int|null $maxRetries The maximum number of retries allowed. | |
| 142 | + * Null for no limit. | |
| 142 | 143 | * @return bool |
| 143 | 144 | */ |
| 144 | - private function retryDeciderFunction(\Exception $exception, $isIdempotent, $preconditionNeeded, $preconditionSupplied, $retryStrategy) | |
| 145 | + private function retryDeciderFunction(\Exception $exception, $isIdempotent, $preconditionNeeded, $preconditionSupplied, $retryStrategy, $currentAttempt = 0, $maxRetries = null) | |
| 145 | 146 | { |
| 147 | + // If maxRetries is specified, ensure we don't exceed it | |
| 148 | + if ($maxRetries !== null && $currentAttempt >= $maxRetries) { | |
| 149 | + return \false; | |
| 150 | + } | |
| 146 | 151 | if ($retryStrategy == StorageClient::RETRY_NEVER) { |
| 147 | 152 | return \false; |
| 148 | 153 | } |
| 149 | 154 | $statusCode = $exception->getCode(); |
| @@ -150,9 +155,9 @@ | ||
| 150 | 155 | // Retry if the exception status code matches |
| 151 | 156 | // with one of the retriable status code and |
| 152 | 157 | // the operation is either idempotent or conditionally |
| 153 | 158 | // idempotent with preconditions supplied. |
| 154 | - if (in_array($statusCode, self::$httpRetryCodes)) { | |
| 159 | + if (\in_array($statusCode, self::$httpRetryCodes)) { | |
| 155 | 160 | if ($retryStrategy == StorageClient::RETRY_ALWAYS) { |
| 156 | 161 | return \true; |
| 157 | 162 | } elseif ($isIdempotent) { |
| 158 | 163 | return \true; |
| @@ -167,7 +172,7 @@ | ||
| 167 | 172 | * attached to every request and its retries. |
| 168 | 173 | */ |
| 169 | 174 | private static function getRetryHeaders($invocationId, $attemptCount) |
| 170 | 175 | { |
| 171 | - return [sprintf('%s/%s', self::$INVOCATION_ID_HEADER, $invocationId), sprintf('%s/%d', self::$ATTEMPT_COUNT_HEADER, $attemptCount)]; | |
| 176 | + return [\sprintf('%s/%s', self::$INVOCATION_ID_HEADER, $invocationId), \sprintf('%s/%d', self::$ATTEMPT_COUNT_HEADER, $attemptCount)]; | |
| 172 | 177 | } |
| 173 | 178 | } |