Skip to content

Commit

Permalink
integ: use retries when applying toxics
Browse files Browse the repository at this point in the history
The forked noxious-server takes a little longer to start on MacOS. Using
retries ensures we keep trying until the server is ready.
  • Loading branch information
cbgbt committed Aug 24, 2023
1 parent c4bfd0d commit a18164b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions integ/failure-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rand = "0.8"
serde_json = "1.0"
tempfile = "3.8"
tokio = "1.0"
tokio-retry = "0.3"
tower = { version = "0.4", features = ["util"] }
tower-fault = "0.0.5"
tower-http = { version = "0.4", features = ["fs"] }
27 changes: 19 additions & 8 deletions integ/failure-server/src/toxic/tcp_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::net::ToSocketAddrs;
use std::process::Command;
use std::{fmt::Debug, net::SocketAddr};
use tempfile::NamedTempFile;
use tokio_retry::{strategy::ExponentialBackoff, Retry};

/// A TCP proxy server that introduces artificial faults at the TCP layer.
#[derive(Debug)]
Expand All @@ -26,6 +27,10 @@ pub(crate) struct ToxicTcpProxy {
toxics: Vec<Toxic>,
}

fn retry_strategy() -> impl Iterator<Item = std::time::Duration> {
ExponentialBackoff::from_millis(500).take(10)
}

impl ToxicTcpProxy {
pub(crate) fn new<T1, T2, T3>(
name: String,
Expand Down Expand Up @@ -92,15 +97,21 @@ impl ToxicTcpProxy {

// Configure toxics
let client = Client::new(&self.api_listen.to_string());
let proxy = client.proxy(&self.name).await.context(format!(
"Failed to find our configured proxy '{}'",
self.name
))?;
let proxy = Retry::spawn(retry_strategy(), || async {
client.proxy(&self.name).await.context(format!(
"Failed to find our configured proxy '{}'",
self.name
))
})
.await?;
for toxic in &self.toxics {
proxy.add_toxic(toxic).await.context(format!(
"Failed to apply toxic {:?} to proxy '{}'",
toxic, self.name
))?;
Retry::spawn(retry_strategy(), || async {
proxy.add_toxic(toxic).await.context(format!(
"Failed to apply toxic {:?} to proxy '{}'",
toxic, self.name
))
})
.await?;
}

Ok(())
Expand Down

0 comments on commit a18164b

Please sign in to comment.