google_cloud_gax/backoff_policy.rs
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Defines traits for backoff policies and a common implementations.
16//!
17//! The client libraries automatically retry RPCs based on the [RetryPolicy]
18//! configured for the request or client. Even when the policy determines that
19//! an operation is safe to retry, the client library does not retry failed
20//! requests immediately, as the service may need time to recover.
21//! [Exponential backoff] is a well known algorithm to find an acceptable delay
22//! between retries, but some application may need slight variations on this
23//! algorithm. The client libraries use a [BackoffPolicy] to configure the
24//! delays between retry attempts.
25//!
26//! While exponential backoff improves the system behavior when there are small
27//! faults, something like a [RetryThrottler] may be needed to improve recovery
28//! times in larger failures.
29//!
30//! To configure the default retry backoff policy for a client, use
31//! [ClientBuilder::with_backoff_policy]. To configure the retry backoff policy
32//! used for a specific request, use
33//! [RequestOptionsBuilder::with_backoff_policy].
34//!
35//! [ClientBuilder::with_backoff_policy]: crate::client_builder::ClientBuilder::with_backoff_policy
36//! [RequestOptionsBuilder::with_backoff_policy]: crate::options::RequestOptionsBuilder::with_backoff_policy
37//! [RetryPolicy]: crate::retry_policy::RetryPolicy
38//!
39//! # Example
40//! ```
41//! # use google_cloud_gax::exponential_backoff::Error;
42//! # use google_cloud_gax::exponential_backoff::ExponentialBackoffBuilder;
43//! use std::time::Duration;
44//!
45//! let policy = ExponentialBackoffBuilder::new()
46//! .with_initial_delay(Duration::from_millis(100))
47//! .with_maximum_delay(Duration::from_secs(5))
48//! .with_scaling(4.0)
49//! .build()?;
50//! # Ok::<(), Error>(())
51//! ```
52//!
53//! [RetryThrottler]: crate::retry_throttler::RetryThrottler
54//! [Exponential backoff]: https://en.wikipedia.org/wiki/Exponential_backoff
55//! [idempotent]: https://en.wikipedia.org/wiki/Idempotence
56
57use crate::retry_state::RetryState;
58use std::sync::Arc;
59
60/// Defines the trait implemented by all backoff strategies.
61pub trait BackoffPolicy: Send + Sync + std::fmt::Debug {
62 /// Returns the backoff delay on a failure.
63 ///
64 /// # Parameters
65 /// * `loop_start` - when the retry loop started.
66 /// * `attempt_count` - the number of attempts. This method is always called
67 /// after the first attempt.
68 #[cfg_attr(not(feature = "_internal-semver"), doc(hidden))]
69 fn on_failure(&self, state: &RetryState) -> std::time::Duration;
70}
71
72/// A helper type to use [BackoffPolicy] in client and request options.
73#[derive(Clone, Debug)]
74pub struct BackoffPolicyArg(Arc<dyn BackoffPolicy>);
75
76impl<T: BackoffPolicy + 'static> From<T> for BackoffPolicyArg {
77 fn from(value: T) -> Self {
78 Self(Arc::new(value))
79 }
80}
81
82impl From<Arc<dyn BackoffPolicy>> for BackoffPolicyArg {
83 fn from(value: Arc<dyn BackoffPolicy>) -> Self {
84 Self(value)
85 }
86}
87
88impl From<BackoffPolicyArg> for Arc<dyn BackoffPolicy> {
89 fn from(value: BackoffPolicyArg) -> Arc<dyn BackoffPolicy> {
90 value.0
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97 use crate::exponential_backoff::ExponentialBackoffBuilder;
98
99 // Verify `BackoffPolicyArg` can be converted from the desired types.
100 #[test]
101 fn backoff_policy_arg() {
102 let policy = ExponentialBackoffBuilder::default().clamp();
103 let _ = BackoffPolicyArg::from(policy);
104
105 let policy: Arc<dyn BackoffPolicy> = Arc::new(ExponentialBackoffBuilder::default().clamp());
106 let _ = BackoffPolicyArg::from(policy);
107 }
108}