aws_smithy_runtime_api/client/
stalled_stream_protection.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#![allow(missing_docs)]
7
8//! Stalled stream protection.
9//!
10//! When enabled, upload and download streams that stall (stream no data) for
11//! longer than a configured grace period will return an error.
12
13use aws_smithy_types::config_bag::{Storable, StoreReplace};
14use std::time::Duration;
15
16/// The default grace period for stalled stream protection.
17///
18/// When a stream stalls for longer than this grace period, the stream will
19/// return an error.
20pub const DEFAULT_GRACE_PERIOD: Duration = Duration::from_secs(20);
21
22/// Configuration for stalled stream protection.
23///
24/// When enabled, download streams that stall out will be cancelled.
25#[derive(Clone, Debug)]
26pub struct StalledStreamProtectionConfig {
27    upload_enabled: bool,
28    download_enabled: bool,
29    grace_period: Duration,
30}
31
32impl StalledStreamProtectionConfig {
33    /// Create a new config that enables stalled stream protection for both uploads and downloads.
34    pub fn enabled() -> Builder {
35        Builder {
36            upload_enabled: Some(true),
37            download_enabled: Some(true),
38            grace_period: None,
39        }
40    }
41
42    /// Create a new config that disables stalled stream protection.
43    pub fn disabled() -> Self {
44        Self {
45            upload_enabled: false,
46            download_enabled: false,
47            grace_period: DEFAULT_GRACE_PERIOD,
48        }
49    }
50
51    /// Return whether stalled stream protection is enabled for either uploads or downloads.
52    pub fn is_enabled(&self) -> bool {
53        self.upload_enabled || self.download_enabled
54    }
55
56    /// True if stalled stream protection is enabled for upload streams.
57    pub fn upload_enabled(&self) -> bool {
58        self.upload_enabled
59    }
60
61    /// True if stalled stream protection is enabled for download streams.
62    pub fn download_enabled(&self) -> bool {
63        self.download_enabled
64    }
65
66    /// Return the grace period for stalled stream protection.
67    ///
68    /// When a stream stalls for longer than this grace period, the stream will
69    /// return an error.
70    pub fn grace_period(&self) -> Duration {
71        self.grace_period
72    }
73}
74
75#[derive(Clone, Debug)]
76pub struct Builder {
77    upload_enabled: Option<bool>,
78    download_enabled: Option<bool>,
79    grace_period: Option<Duration>,
80}
81
82impl Builder {
83    /// Set the grace period for stalled stream protection.
84    pub fn grace_period(mut self, grace_period: Duration) -> Self {
85        self.grace_period = Some(grace_period);
86        self
87    }
88
89    /// Set the grace period for stalled stream protection.
90    pub fn set_grace_period(&mut self, grace_period: Option<Duration>) -> &mut Self {
91        self.grace_period = grace_period;
92        self
93    }
94
95    /// Set whether stalled stream protection is enabled for both uploads and downloads.
96    pub fn is_enabled(mut self, enabled: bool) -> Self {
97        self.set_is_enabled(Some(enabled));
98        self
99    }
100
101    /// Set whether stalled stream protection is enabled for both uploads and downloads.
102    pub fn set_is_enabled(&mut self, enabled: Option<bool>) -> &mut Self {
103        self.set_upload_enabled(enabled);
104        self.set_download_enabled(enabled);
105        self
106    }
107
108    /// Set whether stalled stream protection is enabled for upload streams.
109    pub fn upload_enabled(mut self, enabled: bool) -> Self {
110        self.set_upload_enabled(Some(enabled));
111        self
112    }
113
114    /// Set whether stalled stream protection is enabled for upload streams.
115    pub fn set_upload_enabled(&mut self, enabled: Option<bool>) -> &mut Self {
116        self.upload_enabled = enabled;
117        self
118    }
119
120    /// Set whether stalled stream protection is enabled for download streams.
121    pub fn download_enabled(mut self, enabled: bool) -> Self {
122        self.set_download_enabled(Some(enabled));
123        self
124    }
125
126    /// Set whether stalled stream protection is enabled for download streams.
127    pub fn set_download_enabled(&mut self, enabled: Option<bool>) -> &mut Self {
128        self.download_enabled = enabled;
129        self
130    }
131
132    /// Build the config.
133    pub fn build(self) -> StalledStreamProtectionConfig {
134        StalledStreamProtectionConfig {
135            upload_enabled: self.upload_enabled.unwrap_or_default(),
136            download_enabled: self.download_enabled.unwrap_or_default(),
137            grace_period: self.grace_period.unwrap_or(DEFAULT_GRACE_PERIOD),
138        }
139    }
140}
141
142impl From<StalledStreamProtectionConfig> for Builder {
143    fn from(config: StalledStreamProtectionConfig) -> Self {
144        Builder {
145            upload_enabled: Some(config.upload_enabled),
146            download_enabled: Some(config.download_enabled),
147            grace_period: Some(config.grace_period),
148        }
149    }
150}
151
152impl Storable for StalledStreamProtectionConfig {
153    type Storer = StoreReplace<Self>;
154}