Skip to content

Commit 7e68df1

Browse files
add time argument implement
1 parent 05fc206 commit 7e68df1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ pub struct Args {
99
pub url: String,
1010

1111
#[arg(short = 't', long = "time", default_value_t = 60)]
12-
pub time: u32,
12+
pub time: u64,
1313
}

src/main.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,49 @@
11
mod args;
2+
use std::time::Duration;
3+
24
use args::Args;
35
use clap::Parser;
46
use reqwest::Client;
7+
use tokio::sync::broadcast;
58

69
#[tokio::main]
710
async fn main() -> anyhow::Result<()> {
811
let args = Args::parse();
912
let mut handles = Vec::new();
1013
let client = Client::new();
14+
let (shutdown_tx, _) = broadcast::channel(1);
1115

1216
for _ in 0..args.concurrent_count {
1317
let url = args.url.clone();
1418
let client = client.clone();
19+
let mut shutdown_rx = shutdown_tx.subscribe();
20+
1521
let handle = tokio::spawn(async move {
16-
if let Ok(resp) = client.get(&url).send().await {
17-
let _ = resp.bytes().await;
22+
loop {
23+
tokio::select! {
24+
biased;
25+
_ = shutdown_rx.recv() => {
26+
break;
27+
}
28+
result = client.get(&url).send() => {
29+
if let Ok(resp) = result {
30+
let _ = resp.bytes().await;
31+
}
32+
tokio::task::yield_now().await;
33+
}
34+
}
1835
}
1936
});
2037
handles.push(handle);
2138
}
2239

40+
tokio::time::sleep(Duration::from_secs(args.time)).await;
41+
let _ = shutdown_tx.send(());
42+
2343
for handle in handles {
24-
handle.await.unwrap();
44+
if let Err(e) = handle.await {
45+
eprintln!("Task exited with error: {:?}", e);
46+
}
2547
}
2648

2749
Ok(())

0 commit comments

Comments
 (0)