1
1
use std:: collections:: HashMap ;
2
2
use std:: net:: SocketAddr ;
3
+ use std:: time:: Duration ;
3
4
4
5
use futures:: prelude:: * ;
5
6
use hyper:: Client ;
@@ -19,18 +20,14 @@ pub async fn search_gateway(options: SearchOptions) -> Result<Gateway, SearchErr
19
20
20
21
send_search_request ( & mut socket, options. broadcast_address ) . await ?;
21
22
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 ??;
29
24
30
25
let ( addr, root_url) = handle_broadcast_resp ( & from, & response_body) ?;
31
26
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 ??;
34
31
35
32
let addr = match addr {
36
33
SocketAddr :: V4 ( a) => Ok ( a) ,
@@ -49,6 +46,16 @@ pub async fn search_gateway(options: SearchOptions) -> Result<Gateway, SearchErr
49
46
} )
50
47
}
51
48
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
+
52
59
// Create a new search
53
60
async fn send_search_request ( socket : & mut UdpSocket , addr : SocketAddr ) -> Result < ( ) , SearchError > {
54
61
debug ! (
0 commit comments