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 crate::read_resume_policy::{ReadResumePolicy, Recommended};
16use crate::storage::checksum::details::{Checksum, Crc32c};
17use gax::{
18    backoff_policy::BackoffPolicy,
19    retry_policy::RetryPolicy,
20    retry_throttler::{AdaptiveThrottler, SharedRetryThrottler},
21};
22use std::sync::{Arc, Mutex};
23
24/// The per-request options for a client call.
25///
26/// This is currently an opaque type, used only in mocking the `Storage` client.
27/// It is opaque to avoid breaking changes until its interface stabilizes.
28#[derive(Clone, Debug)]
29pub struct RequestOptions {
30    pub(crate) retry_policy: Arc<dyn RetryPolicy>,
31    pub(crate) backoff_policy: Arc<dyn BackoffPolicy>,
32    pub(crate) retry_throttler: SharedRetryThrottler,
33    pub(crate) read_resume_policy: Arc<dyn ReadResumePolicy>,
34    pub(crate) resumable_upload_threshold: usize,
35    pub(crate) resumable_upload_buffer_size: usize,
36    pub(crate) idempotency: Option<bool>,
37    pub(crate) checksum: Checksum,
38    pub(crate) automatic_decompression: bool,
39}
40
41const MIB: usize = 1024 * 1024_usize;
42// There is some justification for these magic numbers at:
43//     https://github.com/googleapis/google-cloud-cpp/issues/2657
44const RESUMABLE_UPLOAD_THRESHOLD: usize = 16 * MIB;
45const RESUMABLE_UPLOAD_TARGET_CHUNK: usize = 8 * MIB;
46
47impl RequestOptions {
48    pub(crate) fn new() -> Self {
49        let retry_policy = Arc::new(crate::retry_policy::storage_default());
50        let backoff_policy = Arc::new(crate::backoff_policy::default());
51        let retry_throttler = Arc::new(Mutex::new(AdaptiveThrottler::default()));
52        let read_resume_policy = Arc::new(Recommended);
53        Self {
54            retry_policy,
55            backoff_policy,
56            retry_throttler,
57            read_resume_policy,
58            resumable_upload_threshold: RESUMABLE_UPLOAD_THRESHOLD,
59            resumable_upload_buffer_size: RESUMABLE_UPLOAD_TARGET_CHUNK,
60            idempotency: None,
61            checksum: Checksum {
62                crc32c: Some(Crc32c::default()),
63                md5_hash: None,
64            },
65            automatic_decompression: false,
66        }
67    }
68}