Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow blackhole to delay response #1012

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Linux observer is more resilient to scenarios where lading lacks ptrace permission.
### Removed
- lading_capture no longer exports a protobuf version of the capture.
### Added
- HTTP blackhole now has a `response_delay_millis` setting, allowing for
simulation of latent network connections.

## [0.23.2]
### Changed
Expand Down
15 changes: 15 additions & 0 deletions lading/src/blackhole/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ fn default_body_variant() -> BodyVariant {
BodyVariant::Nothing
}

fn default_response_delay_millis() -> u64 {
0
}

fn default_status_code() -> u16 {
StatusCode::OK.as_u16()
}
Expand Down Expand Up @@ -98,6 +102,9 @@ pub struct Config {
/// raw array of bytes if the `raw_bytes` body variant is selected
#[serde(default)]
pub raw_bytes: Vec<u8>,
/// delay to add before making a response
#[serde(default = "default_response_delay_millis")]
pub response_delay_millis: u64,
Comment on lines +105 to +107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you foresee possibly changing the default response delay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting question, it seems useful to make this a little closer to real-world conditions where you'll never have a 0ms rtt.
Asked some internal teams to see if we can get a better default

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original motivation for the question was a bit more mundane: if the default will always be zero, #[serde(default = "default_response_delay_millis"] could be replaced with #[serde(default)]. However, delegating the default value to a function makes changing that default value much easier.

}

#[derive(Serialize)]
Expand All @@ -124,6 +131,7 @@ async fn srv(
body_bytes: Vec<u8>,
req: Request<Body>,
headers: HeaderMap,
response_delay: Duration,
) -> Result<Response<Body>, hyper::Error> {
requests_received.increment(1);

Expand All @@ -136,6 +144,8 @@ async fn srv(
Ok(body) => {
bytes_received.increment(body.len() as u64);

tokio::time::sleep(response_delay).await;

let mut okay = Response::default();
*okay.status_mut() = status;
*okay.headers_mut() = headers;
Expand All @@ -155,6 +165,7 @@ pub struct Http {
headers: HeaderMap,
status: StatusCode,
metric_labels: Vec<(String, String)>,
response_delay: Duration,
}

impl Http {
Expand Down Expand Up @@ -208,6 +219,7 @@ impl Http {
status,
shutdown,
metric_labels,
response_delay: Duration::from_millis(config.response_delay_millis),
})
}

Expand Down Expand Up @@ -238,6 +250,7 @@ impl Http {
body_bytes.clone(),
request,
headers.clone(),
self.response_delay,
)
}))
}
Expand Down Expand Up @@ -287,6 +300,7 @@ body_variant: "nothing"
config,
Config {
concurrent_requests_max: default_concurrent_requests_max(),
response_delay_millis: default_response_delay_millis(),
binding_addr: SocketAddr::from_str("127.0.0.1:1000")
.expect("Not possible to parse into SocketAddr"),
body_variant: BodyVariant::Nothing,
Expand All @@ -310,6 +324,7 @@ raw_bytes: [0x01, 0x02, 0x10]
config,
Config {
concurrent_requests_max: default_concurrent_requests_max(),
response_delay_millis: default_response_delay_millis(),
binding_addr: SocketAddr::from_str("127.0.0.1:1000")
.expect("Not possible to parse into SocketAddr"),
body_variant: BodyVariant::RawBytes,
Expand Down
Loading