aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs34
1 files changed, 22 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs
index 8e61fc9..9d0ae2f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -27,9 +27,12 @@ struct Args {
/// port for serving http traffic
#[arg(short, long, default_value_t = 3200)]
port: u16,
- /// delay all responses (in milliseconds)
- #[arg(short, long, default_value_t = 0)]
- delay: u32,
+ /// minimum delay for responses (in milliseconds)
+ #[arg(long, default_value_t = 0)]
+ delay_min: u64,
+ /// minimum delay for responses (in milliseconds)
+ #[arg(long, default_value_t = 0)]
+ delay_max: u64,
}
#[tokio::main]
@@ -40,6 +43,8 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error + Send + Sync>> {
let svc = RandomPageService::new(&args);
+ tracing::info!(port = args.port, "starting pitch lake");
+
let addr = SocketAddr::from(([127, 0, 0, 1], args.port));
let listener = TcpListener::bind(addr).await?;
@@ -72,7 +77,7 @@ impl RandomPageService {
Self {
ctx: Arc::new(PageGenerator {
seed: args.seed.clone(),
- delay: args.delay,
+ delay: args.delay_min..=(args.delay_max.max(args.delay_min)),
dict: dictionary_string.split_whitespace().collect(),
dict_set: dictionary_string.split_whitespace().collect(),
}),
@@ -103,8 +108,7 @@ impl Service<Request<hyper::body::Incoming>> for RandomPageService {
let ctx = self.ctx.clone();
Box::pin(async move {
- let path = req.uri().path();
- match ctx.build_page(path).await {
+ match ctx.build_page(req).await {
Ok(body) => mk_response(body, StatusCode::OK),
Err((err_message, code)) => mk_response(err_message.to_string(), code),
}
@@ -119,7 +123,7 @@ const N_LINKS: RangeInclusive<usize> = 3..=10;
#[derive(Debug)]
struct PageGenerator {
seed: String,
- delay: u32,
+ delay: RangeInclusive<u64>,
dict: Vec<&'static str>,
dict_set: HashSet<&'static str>,
}
@@ -127,12 +131,18 @@ struct PageGenerator {
impl PageGenerator {
async fn build_page(
&self,
- route: &str,
+ req: Request<hyper::body::Incoming>,
) -> Result<String, (&'static str, StatusCode)> {
- if self.delay > 0 {
- tokio::time::sleep(tokio::time::Duration::from_millis(self.delay as u64))
+ let delay = if *self.delay.start() > 0 {
+ let delay_amount = rand::thread_rng().gen_range(self.delay.clone());
+ tokio::time::sleep(tokio::time::Duration::from_millis(delay_amount))
.await;
- }
+ Some(delay_amount)
+ } else {
+ None
+ };
+
+ let route = req.uri().path();
for segment in route.split('/').filter(|v| v.len() > 0) {
if !self.dict_set.contains(segment) {
@@ -156,7 +166,7 @@ impl PageGenerator {
.collect::<Vec<String>>()
.join("\n");
- tracing::info!(route);
+ tracing::info!(route, delay);
Ok(format!(
r#"