-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(Storage): adding retry configuration example. #2149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
piotrgradzinski
wants to merge
2
commits into
GoogleCloudPlatform:main
Choose a base branch
from
piotrgradzinski:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2025 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* For instructions on how to run the full sample: | ||
* | ||
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/storage/README.md | ||
*/ | ||
|
||
namespace Google\Cloud\Samples\Storage; | ||
|
||
# [START configure_retries] | ||
use Google\Cloud\Storage\StorageClient; | ||
|
||
/** | ||
* Configures retries with customizations. | ||
* | ||
* @param string $bucketName The name of your Cloud Storage bucket. | ||
* (e.g. 'my-bucket') | ||
*/ | ||
function configure_retries(string $bucketName): void | ||
{ | ||
$storage = new StorageClient([ | ||
// The maximum number of automatic retries attempted before returning | ||
// the error. | ||
// Default: 3 | ||
'retries' => 10, | ||
|
||
// Exponential backoff settings | ||
// Retry strategy to signify that we never want to retry an operation | ||
// even if the error is retryable. | ||
// Default: StorageClient::RETRY_IDEMPOTENT | ||
'retryStrategy' => StorageClient::RETRY_ALWAYS, | ||
|
||
// Executes a delay | ||
// Defaults to utilizing `usleep`. | ||
// Function signature should match: `function (int $delay) : void`. | ||
'restDelayFunction' => function ($delay) { | ||
usleep($delay); | ||
piotrgradzinski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
|
||
// Sets the conditions for determining how long to wait between attempts to retry. | ||
// Function signature should match: `function (int $attempt) : int`. | ||
// Allows to change the initial retry delay, retry delay multiplier and maximum retry delay. | ||
'restCalcDelayFunction' => fn ($attempt) => ($attempt + 1) * 100, | ||
|
||
// Sets the conditions for whether or not a request should attempt to retry. | ||
// Function signature should match: `function (\Exception $ex) : bool`. | ||
'restRetryFunction' => function (\Exception $e) { | ||
// Custom logic: ex. only retry if the error code is 404. | ||
return $e->getCode() === 404; | ||
}, | ||
|
||
// Runs after the restRetryFunction. This might be used to simply consume the | ||
// exception and $arguments b/w retries. This returns the new $arguments thus allowing | ||
// modification on demand for $arguments. For ex: changing the headers in b/w retries. | ||
'restRetryListener' => function (\Exception $e, $retryAttempt, &$arguments) { | ||
// logic | ||
}, | ||
]); | ||
$bucket = $storage->bucket($bucketName); | ||
$operationRetriesOverrides = [ | ||
// The maximum number of automatic retries attempted before returning | ||
// the error. | ||
// Default: 3 | ||
'retries' => 10, | ||
|
||
// Exponential backoff settings | ||
// Retry strategy to signify that we never want to retry an operation | ||
// even if the error is retryable. | ||
// Default: StorageClient::RETRY_IDEMPOTENT | ||
'retryStrategy' => StorageClient::RETRY_ALWAYS, | ||
|
||
// Executes a delay | ||
// Defaults to utilizing `usleep`. | ||
// Function signature should match: `function (int $delay) : void`. | ||
'restDelayFunction' => function ($delay) { | ||
usleep($delay); | ||
}, | ||
|
||
// Sets the conditions for determining how long to wait between attempts to retry. | ||
// Function signature should match: `function (int $attempt) : int`. | ||
// Allows to change the initial retry delay, retry delay multiplier and maximum retry delay. | ||
'restCalcDelayFunction' => fn ($attempt) => ($attempt + 1) * 100, | ||
|
||
// Sets the conditions for whether or not a request should attempt to retry. | ||
// Function signature should match: `function (\Exception $ex) : bool`. | ||
'restRetryFunction' => function (\Exception $e) { | ||
// Custom logic: ex. only retry if the error code is 404. | ||
return $e->getCode() === 404; | ||
}, | ||
|
||
// Runs after the restRetryFunction. This might be used to simply consume the | ||
// exception and $arguments b/w retries. This returns the new $arguments thus allowing | ||
// modification on demand for $arguments. For ex: changing the headers in b/w retries. | ||
'restRetryListener' => function (\Exception $e, $retryAttempt, &$arguments) use (&$listenerInvocations6b) { | ||
// logic | ||
}, | ||
]; | ||
foreach ($bucket->objects($operationRetriesOverrides) as $object) { | ||
printf('Object: %s' . PHP_EOL, $object->name()); | ||
} | ||
} | ||
# [END configure_retries] | ||
|
||
// The following 2 lines are only needed to run the samples | ||
require_once __DIR__ . '/../../testing/sample_helpers.php'; | ||
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.