|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2025 Google Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * For instructions on how to run the full sample: |
| 21 | + * |
| 22 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Google\Cloud\Samples\Storage; |
| 26 | + |
| 27 | +# [START storage_configure_retries] |
| 28 | +use Google\Cloud\Storage\StorageClient; |
| 29 | + |
| 30 | +/** |
| 31 | + * Configures retries with customizations. |
| 32 | + * |
| 33 | + * @param string $bucketName The name of your Cloud Storage bucket. |
| 34 | + * (e.g. 'my-bucket') |
| 35 | + */ |
| 36 | +function configure_retries(string $bucketName): void |
| 37 | +{ |
| 38 | + $storage = new StorageClient([ |
| 39 | + // The maximum number of automatic retries attempted before returning |
| 40 | + // the error. |
| 41 | + // Default: 3 |
| 42 | + 'retries' => 10, |
| 43 | + |
| 44 | + // Exponential backoff settings |
| 45 | + // Retry strategy to signify that we never want to retry an operation |
| 46 | + // even if the error is retryable. |
| 47 | + // Default: StorageClient::RETRY_IDEMPOTENT |
| 48 | + 'retryStrategy' => StorageClient::RETRY_ALWAYS, |
| 49 | + |
| 50 | + // Executes a delay |
| 51 | + // Defaults to utilizing `usleep`. |
| 52 | + // Function signature should match: `function (int $delay) : void`. |
| 53 | + // This function is mostly used internally, so the tests don't wait |
| 54 | + // the time of the delay to run. |
| 55 | + 'restDelayFunction' => function ($delay) { |
| 56 | + usleep($delay); |
| 57 | + }, |
| 58 | + |
| 59 | + // Sets the conditions for determining how long to wait between attempts to retry. |
| 60 | + // Function signature should match: `function (int $attempt) : int`. |
| 61 | + // Allows to change the initial retry delay, retry delay multiplier and maximum retry delay. |
| 62 | + 'restCalcDelayFunction' => fn ($attempt) => ($attempt + 1) * 100, |
| 63 | + |
| 64 | + // Sets the conditions for whether or not a request should attempt to retry. |
| 65 | + // Function signature should match: `function (\Exception $ex) : bool`. |
| 66 | + 'restRetryFunction' => function (\Exception $e) { |
| 67 | + // Custom logic: ex. only retry if the error code is 404. |
| 68 | + return $e->getCode() === 404; |
| 69 | + }, |
| 70 | + |
| 71 | + // Runs after the restRetryFunction. This might be used to simply consume the |
| 72 | + // exception and $arguments b/w retries. This returns the new $arguments thus allowing |
| 73 | + // modification on demand for $arguments. For ex: changing the headers in b/w retries. |
| 74 | + 'restRetryListener' => function (\Exception $e, $retryAttempt, &$arguments) { |
| 75 | + // logic |
| 76 | + }, |
| 77 | + ]); |
| 78 | + $bucket = $storage->bucket($bucketName); |
| 79 | + $operationRetriesOverrides = [ |
| 80 | + // The maximum number of automatic retries attempted before returning |
| 81 | + // the error. |
| 82 | + // Default: 3 |
| 83 | + 'retries' => 10, |
| 84 | + |
| 85 | + // Exponential backoff settings |
| 86 | + // Retry strategy to signify that we never want to retry an operation |
| 87 | + // even if the error is retryable. |
| 88 | + // Default: StorageClient::RETRY_IDEMPOTENT |
| 89 | + 'retryStrategy' => StorageClient::RETRY_ALWAYS, |
| 90 | + |
| 91 | + // Executes a delay |
| 92 | + // Defaults to utilizing `usleep`. |
| 93 | + // Function signature should match: `function (int $delay) : void`. |
| 94 | + // This function is mostly used internally, so the tests don't wait |
| 95 | + // the time of the delay to run. |
| 96 | + 'restDelayFunction' => function ($delay) { |
| 97 | + usleep($delay); |
| 98 | + }, |
| 99 | + |
| 100 | + // Sets the conditions for determining how long to wait between attempts to retry. |
| 101 | + // Function signature should match: `function (int $attempt) : int`. |
| 102 | + // Allows to change the initial retry delay, retry delay multiplier and maximum retry delay. |
| 103 | + 'restCalcDelayFunction' => fn ($attempt) => ($attempt + 1) * 100, |
| 104 | + |
| 105 | + // Sets the conditions for whether or not a request should attempt to retry. |
| 106 | + // Function signature should match: `function (\Exception $ex) : bool`. |
| 107 | + 'restRetryFunction' => function (\Exception $e) { |
| 108 | + // Custom logic: ex. only retry if the error code is 404. |
| 109 | + return $e->getCode() === 404; |
| 110 | + }, |
| 111 | + |
| 112 | + // Runs after the restRetryFunction. This might be used to simply consume the |
| 113 | + // exception and $arguments b/w retries. This returns the new $arguments thus allowing |
| 114 | + // modification on demand for $arguments. For ex: changing the headers in b/w retries. |
| 115 | + 'restRetryListener' => function (\Exception $e, $retryAttempt, &$arguments) { |
| 116 | + // logic |
| 117 | + }, |
| 118 | + ]; |
| 119 | + foreach ($bucket->objects($operationRetriesOverrides) as $object) { |
| 120 | + printf('Object: %s' . PHP_EOL, $object->name()); |
| 121 | + } |
| 122 | +} |
| 123 | +# [END storage_configure_retries] |
| 124 | + |
| 125 | +// The following 2 lines are only needed to run the samples |
| 126 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 127 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments