Skip to content

Commit 0ea2091

Browse files
committed
Posibility to specify request timeout
1 parent 7804b5e commit 0ea2091

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ struct Args {
4040

4141
#[arg(short = 'F', long = "form")]
4242
form_params: Vec<String>,
43+
44+
// timeout
45+
#[arg(short, long, value_name = "TIMEOUT", default_value_t = 5)]
46+
timeout: u64,
4347
}
4448

4549
#[tokio::main]
@@ -98,6 +102,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
98102
header_map,
99103
args.concurrency,
100104
args.duration,
105+
args.timeout,
101106
);
102107

103108
scheduler.run().await;

src/scheduler/scheduler.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use std::{collections::HashMap, sync::Arc};
1+
use std::{collections::HashMap, sync::Arc, time::Duration};
22

33
use reqwest::{header::HeaderMap, Client, Method};
4+
use tokio::time;
45

56
use crate::metrics::metrics::Metrics;
67

@@ -10,9 +11,10 @@ pub struct Scheduler<'a> {
1011
url: String,
1112
body: Option<String>,
1213
form_params: HashMap<String, String>,
14+
headers: HeaderMap,
1315
concurrency: u64,
1416
duration: u64,
15-
headers: HeaderMap,
17+
timeout: u64,
1618
}
1719

1820
impl<'a> Scheduler<'a> {
@@ -25,16 +27,18 @@ impl<'a> Scheduler<'a> {
2527
headers: HeaderMap,
2628
concurrency: u64,
2729
duration: u64,
30+
timeout: u64,
2831
) -> Self {
2932
Scheduler {
3033
metrics,
3134
method,
3235
url,
3336
body,
3437
form_params,
38+
headers,
3539
concurrency,
3640
duration,
37-
headers,
41+
timeout,
3842
}
3943
}
4044

@@ -52,6 +56,7 @@ impl<'a> Scheduler<'a> {
5256
let form_params = self.form_params.clone();
5357
let headers = headers.clone();
5458
let duration = self.duration;
59+
let timeout = self.timeout;
5560
let metrics = Arc::clone(self.metrics);
5661

5762
tasks.push(tokio::spawn(async move {
@@ -62,8 +67,9 @@ impl<'a> Scheduler<'a> {
6267
url,
6368
body,
6469
form_params,
65-
duration,
6670
headers,
71+
duration,
72+
timeout,
6773
)
6874
.await;
6975
}));
@@ -107,10 +113,15 @@ impl<'a> Scheduler<'a> {
107113
url: String,
108114
body: Option<String>,
109115
form_params: HashMap<String, String>,
110-
duration: u64,
111116
headers: HeaderMap,
117+
duration: u64,
118+
timeout: u64,
112119
) {
113-
let client = Client::builder().default_headers(headers).build().unwrap();
120+
let client = Client::builder()
121+
.default_headers(headers)
122+
.timeout(Duration::from_secs(timeout))
123+
.build()
124+
.unwrap();
114125

115126
loop {
116127
let result =

0 commit comments

Comments
 (0)