File tree Expand file tree Collapse file tree 2 files changed +26
-4
lines changed
Expand file tree Collapse file tree 2 files changed +26
-4
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 11mod args;
2+ use std:: time:: Duration ;
3+
24use args:: Args ;
35use clap:: Parser ;
46use reqwest:: Client ;
7+ use tokio:: sync:: broadcast;
58
69#[ tokio:: main]
710async 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 ( ( ) )
You can’t perform that action at this time.
0 commit comments