google_cloud_storage/
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//! Types and functions related to the default backoff policy.
16
17use gax::{backoff_policy::BackoffPolicy, exponential_backoff::ExponentialBackoffBuilder};
18use std::time::Duration;
19
20/// The default backoff policy for the Storage clients.
21///
22/// The service recommends exponential backoff with jitter, starting with a one
23/// second backoff and doubling on each attempt.
24pub fn default() -> impl BackoffPolicy {
25    ExponentialBackoffBuilder::new()
26        .with_initial_delay(Duration::from_secs(1))
27        .with_maximum_delay(Duration::from_secs(60))
28        .with_scaling(2.0)
29        .build()
30        .expect("statically configured policy should succeed")
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36    use gax::retry_state::RetryState;
37
38    #[test]
39    fn default() {
40        let policy = super::default();
41
42        let delay = policy.on_failure(&RetryState::new(true).set_attempt_count(1_u32));
43        assert!(
44            delay <= Duration::from_secs(1),
45            "{delay:?}, policy={policy:?}"
46        );
47
48        let delay = policy.on_failure(&RetryState::new(true).set_attempt_count(2_u32));
49        assert!(
50            delay <= Duration::from_secs(2),
51            "{delay:?}, policy={policy:?}"
52        );
53    }
54}