google_cloud_gax/
polling_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 polling policies.
16
17use std::time::Instant;
18
19/// The input into a polling policy query.
20///
21/// On an error, the client library queries the polling policy as to whether it
22/// should make a new attempt. The client library provides an instance of this
23/// type to this 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 PollingState {
29    /// The start time for this polling loop.
30    pub start: Instant,
31
32    /// The number of times the operation has been polled.
33    pub attempt_count: u32,
34}
35
36impl PollingState {
37    /// Update the start time, useful in mocks.
38    pub fn set_start<T: Into<Instant>>(mut self, v: T) -> Self {
39        self.start = v.into();
40        self
41    }
42
43    /// Update the attempt count, useful in mocks.
44    pub fn set_attempt_count<T: Into<u32>>(mut self, v: T) -> Self {
45        self.attempt_count = v.into();
46        self
47    }
48}
49
50impl std::default::Default for PollingState {
51    fn default() -> Self {
52        Self {
53            start: Instant::now(),
54            attempt_count: 0,
55        }
56    }
57}