-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimum throughput body timeouts Pt.1 (#3068)
## Motivation and Context <!--- Why is this change required? What problem does it solve? --> <!--- If it fixes an open issue, please link to the issue here --> #1562 ## Description <!--- Describe your changes in detail --> This change adds a new body wrapper: The minimum throughput limit wrapper. It tracks the rate that data is being streamed from itself. If that rate falls below some configurable limit, it emits an error instead of the next chunk. This protects users from requests that start quickly but then slow down considerably. I'd like to get this merged and then figure out the codegen/docs/examples/config part in a separate PR. ## Testing <!--- Please describe in detail how you tested your changes --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> Tests are included. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
- Loading branch information
Showing
21 changed files
with
736 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
[workspace] | ||
|
||
|
||
members = [ | ||
"inlineable", | ||
"aws-smithy-async", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
pub mod minimum_throughput; |
91 changes: 91 additions & 0 deletions
91
rust-runtime/aws-smithy-runtime/src/client/http/body/minimum_throughput.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
//! A body-wrapping type that ensures data is being streamed faster than some lower limit. | ||
//! | ||
//! If data is being streamed too slowly, this body type will emit an error next time it's polled. | ||
use aws_smithy_async::rt::sleep::Sleep; | ||
use aws_smithy_async::rt::sleep::{AsyncSleep, SharedAsyncSleep}; | ||
use aws_smithy_async::time::{SharedTimeSource, TimeSource}; | ||
use aws_smithy_runtime_api::box_error::BoxError; | ||
use aws_smithy_runtime_api::shared::IntoShared; | ||
use std::fmt; | ||
use std::time::Duration; | ||
use throughput::{Throughput, ThroughputLogs}; | ||
|
||
/// An implementation of v0.4 `http_body::Body` for `MinimumThroughputBody` and related code. | ||
pub mod http_body_0_4_x; | ||
|
||
mod throughput; | ||
|
||
pin_project_lite::pin_project! { | ||
/// A body-wrapping type that ensures data is being streamed faster than some lower limit. | ||
/// | ||
/// If data is being streamed too slowly, this body type will emit an error next time it's polled. | ||
pub struct MinimumThroughputBody<B> { | ||
async_sleep: SharedAsyncSleep, | ||
time_source: SharedTimeSource, | ||
minimum_throughput: Throughput, | ||
throughput_logs: ThroughputLogs, | ||
#[pin] | ||
sleep_fut: Option<Sleep>, | ||
#[pin] | ||
inner: B, | ||
} | ||
} | ||
|
||
const SIZE_OF_ONE_LOG: usize = std::mem::size_of::<(std::time::SystemTime, u64)>(); // 24 bytes per log | ||
const NUMBER_OF_LOGS_IN_ONE_KB: f64 = 1024.0 / SIZE_OF_ONE_LOG as f64; | ||
|
||
impl<B> MinimumThroughputBody<B> { | ||
/// Create a new minimum throughput body. | ||
pub fn new( | ||
time_source: impl TimeSource + 'static, | ||
async_sleep: impl AsyncSleep + 'static, | ||
body: B, | ||
(bytes_read, per_time_elapsed): (u64, Duration), | ||
) -> Self { | ||
let minimum_throughput = Throughput::new(bytes_read as f64, per_time_elapsed); | ||
Self { | ||
throughput_logs: ThroughputLogs::new( | ||
// Never keep more than 10KB of logs in memory. This currently | ||
// equates to 426 logs. | ||
(NUMBER_OF_LOGS_IN_ONE_KB * 10.0) as usize, | ||
minimum_throughput.per_time_elapsed(), | ||
), | ||
async_sleep: async_sleep.into_shared(), | ||
time_source: time_source.into_shared(), | ||
minimum_throughput, | ||
inner: body, | ||
sleep_fut: None, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
enum Error { | ||
ThroughputBelowMinimum { | ||
expected: Throughput, | ||
actual: Throughput, | ||
}, | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::ThroughputBelowMinimum { expected, actual } => { | ||
write!( | ||
f, | ||
"minimum throughput was specified at {expected}, but throughput of {actual} was observed", | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} | ||
|
||
// Tests are implemented per HTTP body type. |
Oops, something went wrong.