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}
39
40const MIB: usize = 1024 * 1024_usize;
41// There is some justification for these magic numbers at:
42//     https://github.com/googleapis/google-cloud-cpp/issues/2657
43const RESUMABLE_UPLOAD_THRESHOLD: usize = 16 * MIB;
44const RESUMABLE_UPLOAD_TARGET_CHUNK: usize = 8 * MIB;
45
46impl RequestOptions {
47    pub(crate) fn new() -> Self {
48        let retry_policy = Arc::new(crate::retry_policy::storage_default());
49        let backoff_policy = Arc::new(crate::backoff_policy::default());
50        let retry_throttler = Arc::new(Mutex::new(AdaptiveThrottler::default()));
51        let read_resume_policy = Arc::new(Recommended);
52        Self {
53            retry_policy,
54            backoff_policy,
55            retry_throttler,
56            read_resume_policy,
57            resumable_upload_threshold: RESUMABLE_UPLOAD_THRESHOLD,
58            resumable_upload_buffer_size: RESUMABLE_UPLOAD_TARGET_CHUNK,
59            idempotency: None,
60            checksum: Checksum {
61                crc32c: Some(Crc32c::default()),
62                md5_hash: None,
63            },
64        }
65    }
66}