google_cloud_gax/retry_state.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 types to query retry policies.
16
17use std::time::Instant;
18
19/// The input into a retry policy query.
20///
21/// On an error, the client library queries the retry policy as to whether it
22/// should make a new attempt. The client library provides an instance of this
23/// type to the retry policy.
24///
25/// This struct may gain new fields in future versions of the client libraries.
26#[derive(Clone, Debug)]
27#[non_exhaustive]
28pub struct RetryState {
29 /// If true, the request is idempotent and it is safe to retry.
30 ///
31 /// Some policies retry non-idempotent operations because they are safe for
32 /// a given configuration of the service or client.
33 pub idempotent: bool,
34
35 /// The start time for this retry loop.
36 pub start: Instant,
37
38 /// The number of times the request has been attempted.
39 pub attempt_count: u32,
40}
41
42impl RetryState {
43 /// Create a new instance.
44 pub fn new(idempotent: bool) -> Self {
45 Self::default().set_idempotent(idempotent)
46 }
47
48 /// Update the idempotency.
49 pub fn set_idempotent(mut self, v: bool) -> Self {
50 self.idempotent = v;
51 self
52 }
53
54 /// Update the start time, useful in mocks.
55 pub fn set_start<T: Into<Instant>>(mut self, v: T) -> Self {
56 self.start = v.into();
57 self
58 }
59
60 /// Update the attempt count, useful in mocks.
61 pub fn set_attempt_count<T: Into<u32>>(mut self, v: T) -> Self {
62 self.attempt_count = v.into();
63 self
64 }
65}
66
67impl std::default::Default for RetryState {
68 fn default() -> Self {
69 Self {
70 start: Instant::now(),
71 idempotent: false,
72 attempt_count: 0,
73 }
74 }
75}