google_cloud_storage/storage/
request_options.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
15use super::common_options::CommonOptions;
16use crate::{
17    read_resume_policy::ReadResumePolicy,
18    storage::checksum::details::{Checksum, Crc32c},
19};
20use gax::{
21    backoff_policy::BackoffPolicy,
22    retry_policy::RetryPolicy,
23    retry_throttler::{AdaptiveThrottler, SharedRetryThrottler},
24};
25use gaxi::options::ClientConfig;
26use std::sync::{Arc, Mutex};
27
28/// The per-request options for a client call.
29///
30/// This is currently an opaque type, used only in mocking the `Storage` client.
31/// It is opaque to avoid breaking changes until its interface stabilizes.
32#[derive(Clone, Debug)]
33pub struct RequestOptions {
34    pub(crate) retry_policy: Arc<dyn RetryPolicy>,
35    pub(crate) backoff_policy: Arc<dyn BackoffPolicy>,
36    pub(crate) retry_throttler: SharedRetryThrottler,
37    pub(crate) idempotency: Option<bool>,
38    pub(crate) checksum: Checksum,
39    pub(crate) automatic_decompression: bool,
40    pub(crate) common_options: CommonOptions,
41}
42
43impl RequestOptions {
44    pub(crate) fn new() -> Self {
45        let retry_policy = Arc::new(crate::retry_policy::storage_default());
46        let backoff_policy = Arc::new(crate::backoff_policy::default());
47        let retry_throttler = Arc::new(Mutex::new(AdaptiveThrottler::default()));
48        Self::new_with_policies(
49            retry_policy,
50            backoff_policy,
51            retry_throttler,
52            CommonOptions::new(),
53        )
54    }
55
56    pub(crate) fn new_with_client_config(
57        config: &ClientConfig,
58        common_options: CommonOptions,
59    ) -> Self {
60        let retry_policy = config
61            .retry_policy
62            .clone()
63            .unwrap_or_else(|| Arc::new(crate::retry_policy::storage_default()));
64        let backoff_policy = config
65            .backoff_policy
66            .clone()
67            .unwrap_or_else(|| Arc::new(crate::backoff_policy::default()));
68        let retry_throttler = config.retry_throttler.clone();
69        Self::new_with_policies(
70            retry_policy,
71            backoff_policy,
72            retry_throttler,
73            common_options,
74        )
75    }
76
77    pub fn set_read_resume_policy(&mut self, v: Arc<dyn ReadResumePolicy>) {
78        self.common_options.read_resume_policy = v;
79    }
80
81    pub fn read_resume_policy(&self) -> Arc<dyn ReadResumePolicy> {
82        self.common_options.read_resume_policy.clone()
83    }
84
85    pub fn set_resumable_upload_threshold(&mut self, v: usize) {
86        self.common_options.resumable_upload_threshold = v;
87    }
88
89    pub fn resumable_upload_threshold(&self) -> usize {
90        self.common_options.resumable_upload_threshold
91    }
92
93    pub fn set_resumable_upload_buffer_size(&mut self, v: usize) {
94        self.common_options.resumable_upload_buffer_size = v;
95    }
96
97    pub fn resumable_upload_buffer_size(&self) -> usize {
98        self.common_options.resumable_upload_buffer_size
99    }
100
101    fn new_with_policies(
102        retry_policy: Arc<dyn RetryPolicy>,
103        backoff_policy: Arc<dyn BackoffPolicy>,
104        retry_throttler: SharedRetryThrottler,
105        common_options: CommonOptions,
106    ) -> Self {
107        Self {
108            retry_policy,
109            backoff_policy,
110            retry_throttler,
111            common_options,
112            idempotency: None,
113            checksum: Checksum {
114                crc32c: Some(Crc32c::default()),
115                md5_hash: None,
116            },
117            automatic_decompression: false,
118        }
119    }
120
121    #[cfg(google_cloud_unstable_storage_bidi)]
122    #[allow(dead_code)]
123    pub(crate) fn gax(&self) -> gax::options::RequestOptions {
124        let mut options = gax::options::RequestOptions::default();
125        options.set_backoff_policy(self.backoff_policy.clone());
126        options.set_retry_policy(self.retry_policy.clone());
127        options.set_retry_throttler(self.retry_throttler.clone());
128        if let Some(ref i) = self.idempotency {
129            options.set_idempotency(*i);
130        }
131        options
132    }
133}
134
135#[cfg(all(test, google_cloud_unstable_storage_bidi))]
136mod tests {
137    use super::*;
138    use crate::storage::client::tests::{MockBackoffPolicy, MockRetryPolicy, MockRetryThrottler};
139
140    #[test]
141    fn gax_policies() {
142        let mut options = RequestOptions::new();
143        options.retry_policy = Arc::new(MockRetryPolicy::new());
144        options.retry_throttler = Arc::new(Mutex::new(MockRetryThrottler::new()));
145        options.backoff_policy = Arc::new(MockBackoffPolicy::new());
146
147        let got = options.gax();
148        assert!(got.backoff_policy().is_some(), "{got:?}");
149        assert!(got.retry_policy().is_some(), "{got:?}");
150        assert!(got.retry_throttler().is_some(), "{got:?}");
151        assert!(got.idempotent().is_none(), "{got:?}");
152
153        let fmt = format!("{got:?}");
154        assert!(fmt.contains("MockBackoffPolicy"), "{fmt}");
155        assert!(fmt.contains("MockRetryPolicy"), "{fmt}");
156        assert!(fmt.contains("MockRetryThrottler"), "{fmt}");
157    }
158
159    #[test]
160    fn gax_idempotency() {
161        let mut options = RequestOptions::new();
162        options.idempotency = Some(true);
163        let got = options.gax();
164        assert_eq!(got.idempotent(), Some(true));
165    }
166}