aws_smithy_runtime/client/http/body/minimum_throughput/
options.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use super::Throughput;
use aws_smithy_runtime_api::client::stalled_stream_protection::StalledStreamProtectionConfig;
use std::time::Duration;

/// A collection of options for configuring a [`MinimumThroughputBody`](super::MinimumThroughputBody).
#[derive(Debug, Clone)]
pub struct MinimumThroughputBodyOptions {
    /// The minimum throughput that is acceptable.
    minimum_throughput: Throughput,

    /// The 'grace period' after which the minimum throughput will be enforced.
    ///
    /// If this is set to 0, the minimum throughput will be enforced immediately.
    ///
    /// If this is set to a positive value, whenever throughput is below the minimum throughput,
    /// a timer is started. If the timer expires before throughput rises above the minimum,
    /// an error is emitted.
    ///
    /// It is recommended to set this to a small value (e.g. 200ms) to avoid issues during
    /// stream-startup.
    grace_period: Duration,

    /// The period of time to consider when computing the throughput
    ///
    /// This SHOULD be longer than the check interval, or stuck-streams may evade detection.
    check_window: Duration,
}

impl MinimumThroughputBodyOptions {
    /// Create a new builder.
    pub fn builder() -> MinimumThroughputBodyOptionsBuilder {
        Default::default()
    }

    /// Convert this struct into a builder.
    pub fn to_builder(self) -> MinimumThroughputBodyOptionsBuilder {
        MinimumThroughputBodyOptionsBuilder::new()
            .minimum_throughput(self.minimum_throughput)
            .grace_period(self.grace_period)
    }

    /// The throughput check grace period.
    ///
    /// If throughput is below the minimum for longer than this period, an error is emitted.
    ///
    /// If this is set to 0, the minimum throughput will be enforced immediately.
    pub fn grace_period(&self) -> Duration {
        self.grace_period
    }

    /// The minimum acceptable throughput
    pub fn minimum_throughput(&self) -> Throughput {
        self.minimum_throughput
    }

    pub(crate) fn check_window(&self) -> Duration {
        self.check_window
    }

    /// Not used. Always returns `Duration::from_millis(500)`.
    #[deprecated(note = "No longer used. Always returns Duration::from_millis(500)")]
    pub fn check_interval(&self) -> Duration {
        Duration::from_millis(500)
    }
}

impl Default for MinimumThroughputBodyOptions {
    fn default() -> Self {
        Self {
            minimum_throughput: DEFAULT_MINIMUM_THROUGHPUT,
            grace_period: DEFAULT_GRACE_PERIOD,
            check_window: DEFAULT_CHECK_WINDOW,
        }
    }
}

/// A builder for [`MinimumThroughputBodyOptions`]
#[derive(Debug, Default, Clone)]
pub struct MinimumThroughputBodyOptionsBuilder {
    minimum_throughput: Option<Throughput>,
    check_window: Option<Duration>,
    grace_period: Option<Duration>,
}

const DEFAULT_GRACE_PERIOD: Duration = Duration::from_secs(0);
const DEFAULT_MINIMUM_THROUGHPUT: Throughput = Throughput {
    bytes_read: 1,
    per_time_elapsed: Duration::from_secs(1),
};

const DEFAULT_CHECK_WINDOW: Duration = Duration::from_secs(1);

impl MinimumThroughputBodyOptionsBuilder {
    /// Create a new `MinimumThroughputBodyOptionsBuilder`.
    pub fn new() -> Self {
        Default::default()
    }

    /// Set the amount of time that throughput my fall below minimum before an error is emitted.
    ///
    /// If throughput rises above the minimum, the timer is reset.
    pub fn grace_period(mut self, grace_period: Duration) -> Self {
        self.set_grace_period(Some(grace_period));
        self
    }

    /// Set the amount of time that throughput my fall below minimum before an error is emitted.
    ///
    /// If throughput rises above the minimum, the timer is reset.
    pub fn set_grace_period(&mut self, grace_period: Option<Duration>) -> &mut Self {
        self.grace_period = grace_period;
        self
    }

    /// Set the minimum allowable throughput.
    pub fn minimum_throughput(mut self, minimum_throughput: Throughput) -> Self {
        self.set_minimum_throughput(Some(minimum_throughput));
        self
    }

    /// Set the minimum allowable throughput.
    pub fn set_minimum_throughput(&mut self, minimum_throughput: Option<Throughput>) -> &mut Self {
        self.minimum_throughput = minimum_throughput;
        self
    }

    /// No longer used. The check interval is now based on the check window (not currently configurable).
    #[deprecated(
        note = "No longer used. The check interval is now based on the check window (not currently configurable). Open an issue if you need to configure the check window."
    )]
    pub fn check_interval(self, _check_interval: Duration) -> Self {
        self
    }

    /// No longer used. The check interval is now based on the check window (not currently configurable).
    #[deprecated(
        note = "No longer used. The check interval is now based on the check window (not currently configurable). Open an issue if you need to configure the check window."
    )]
    pub fn set_check_interval(&mut self, _check_interval: Option<Duration>) -> &mut Self {
        self
    }

    #[allow(unused)]
    pub(crate) fn check_window(mut self, check_window: Duration) -> Self {
        self.set_check_window(Some(check_window));
        self
    }
    #[allow(unused)]
    pub(crate) fn set_check_window(&mut self, check_window: Option<Duration>) -> &mut Self {
        self.check_window = check_window;
        self
    }

    /// Build this builder, producing a [`MinimumThroughputBodyOptions`].
    ///
    /// Unset fields will be set with defaults.
    pub fn build(self) -> MinimumThroughputBodyOptions {
        MinimumThroughputBodyOptions {
            grace_period: self.grace_period.unwrap_or(DEFAULT_GRACE_PERIOD),
            minimum_throughput: self
                .minimum_throughput
                .unwrap_or(DEFAULT_MINIMUM_THROUGHPUT),
            check_window: self.check_window.unwrap_or(DEFAULT_CHECK_WINDOW),
        }
    }
}

impl From<StalledStreamProtectionConfig> for MinimumThroughputBodyOptions {
    fn from(value: StalledStreamProtectionConfig) -> Self {
        MinimumThroughputBodyOptions {
            grace_period: value.grace_period(),
            minimum_throughput: DEFAULT_MINIMUM_THROUGHPUT,
            check_window: DEFAULT_CHECK_WINDOW,
        }
    }
}