The Cloud Client Libraries for Rust can automatically retry operations that fail due to transient errors. This document shows you how to configure a default retry policy on a client, set attempt and time limits, and override the retry policy for individual requests.
Before you begin
The examples in this document use Secret Manager, though the same concepts apply to other Google Cloud services.
Before running the code in this document, follow Create and access a secret using Secret Manager to enable the Secret Manager API and configure authentication.
Dependencies
Add the required crates to your Cargo.toml file:
cargo add google-cloud-secretmanager-v1 google-cloud-gax anyhow
Configure a default retry policy
This example uses the Aip194Strict policy, which implements the guidelines in
AIP-194 for retrying Google API requests.
To configure Aip194Strict as the default retry policy for a client, complete
the following steps:
Set the retry policy during client initialization:
Send requests using the configured client:
Complete code for a default retry policy
The following example applies Aip194Strict to a Secret Manager
client:
Configure a default retry policy with limits
By default, Aip194Strict doesn't bound the number of attempts or total time
spent retrying. To configure attempt and time limits on a client, bring the
RetryPolicyExt trait into scope and complete the following steps:
Configure the retry policy with
with_attempt_limitandwith_time_limiton the client builder:Send requests using the configured client:
Complete code for a retry policy with limits
The following example applies Aip194Strict with custom attempt and time
limits:
Override the retry policy for a single request
You can override the retry policy on an individual request when you know that retrying a specific operation is safe for your application. For example, deleting a secret can succeed at most once, even though the client library treats delete operations as non-idempotent by default.
Bring RequestOptionsBuilder, RetryPolicyExt, and AlwaysRetry into scope,
and then call with_retry_policy on the request builder:
Complete code for a per-request retry policy
The following example overrides the retry policy, attempt limit, and time limit
for a single delete_secret request: