Skip to content

Commit c718ef2

Browse files
committed
Add http timeout option for gateway searching
1 parent a1fcc09 commit c718ef2

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/aio/search.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22
use std::net::SocketAddr;
3+
use std::time::Duration;
34

45
use futures::prelude::*;
56
use hyper::Client;
@@ -19,18 +20,14 @@ pub async fn search_gateway(options: SearchOptions) -> Result<Gateway, SearchErr
1920

2021
send_search_request(&mut socket, options.broadcast_address).await?;
2122

22-
let search_response = receive_search_response(&mut socket);
23-
24-
// Receive search response, optionally with a timeout
25-
let (response_body, from) = match options.timeout {
26-
Some(t) => timeout(t, search_response).await?,
27-
None => search_response.await,
28-
}?;
23+
let (response_body, from) = run_with_timeout(options.timeout, receive_search_response(&mut socket)).await??;
2924

3025
let (addr, root_url) = handle_broadcast_resp(&from, &response_body)?;
3126

32-
let (control_schema_url, control_url) = get_control_urls(&addr, &root_url).await?;
33-
let control_schema = get_control_schemas(&addr, &control_schema_url).await?;
27+
let (control_schema_url, control_url) =
28+
run_with_timeout(options.http_timeout, get_control_urls(&addr, &root_url)).await??;
29+
let control_schema =
30+
run_with_timeout(options.http_timeout, get_control_schemas(&addr, &control_schema_url)).await??;
3431

3532
let addr = match addr {
3633
SocketAddr::V4(a) => Ok(a),
@@ -49,6 +46,16 @@ pub async fn search_gateway(options: SearchOptions) -> Result<Gateway, SearchErr
4946
})
5047
}
5148

49+
async fn run_with_timeout<F>(timeout_value: Option<Duration>, fut: F) -> Result<F::Output, SearchError>
50+
where
51+
F: Future + Send,
52+
{
53+
match timeout_value {
54+
Some(t) => Ok(timeout(t, fut).await?),
55+
None => Ok(fut.await),
56+
}
57+
}
58+
5259
// Create a new search
5360
async fn send_search_request(socket: &mut UdpSocket, addr: SocketAddr) -> Result<(), SearchError> {
5461
debug!(

src/common/options.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub struct SearchOptions {
2323
pub broadcast_address: SocketAddr,
2424
/// Timeout for a search iteration (defaults to 10s)
2525
pub timeout: Option<Duration>,
26+
/// Http requests timeout (defaults to 30s)
27+
pub http_timeout: Option<Duration>,
2628
}
2729

2830
impl Default for SearchOptions {
@@ -31,6 +33,7 @@ impl Default for SearchOptions {
3133
bind_addr: SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0)),
3234
broadcast_address: "239.255.255.250:1900".parse().unwrap(),
3335
timeout: Some(Duration::from_secs(10)),
36+
http_timeout: None,
3437
}
3538
}
3639
}

0 commit comments

Comments
 (0)