@@ -13,7 +13,7 @@ use n0_future::{
13
13
StreamExt ,
14
14
} ;
15
15
use nested_enum_utils:: common_fields;
16
- use snafu:: { Backtrace , OptionExt , Snafu } ;
16
+ use snafu:: { Backtrace , GenerateImplicitData , OptionExt , Snafu } ;
17
17
use url:: Url ;
18
18
19
19
use crate :: node_info:: { LookupError , NodeInfo } ;
@@ -51,40 +51,30 @@ pub enum DnsError {
51
51
} ,
52
52
#[ snafu( display( "invalid DNS response: not a query for _iroh.z32encodedpubkey" ) ) ]
53
53
InvalidResponse { } ,
54
- #[ snafu( display( "no calls succeeded: [{}]" , errors. iter( ) . map( |e| e. to_string( ) ) . collect:: <Vec <_>>( ) . join( "" ) ) ) ]
55
- Staggered { errors : Vec < DnsError > } ,
56
54
}
57
55
58
- // TODO(matheus23): Remove this, or remove DnsError::Staggered, but don't keep both.
59
- mod staggered {
60
- use snafu:: { Backtrace , GenerateImplicitData , Snafu } ;
61
-
62
- use super :: LookupError ;
63
-
64
- /// Error returned when an input value is too long for [`crate::node_info::UserData`].
65
- #[ allow( missing_docs) ]
66
- #[ derive( Debug , Snafu ) ]
67
- #[ snafu( display( "no calls succeeded: [{}]" , errors. iter( ) . map( |e| e. to_string( ) ) . collect:: <Vec <_>>( ) . join( "" ) ) ) ]
68
- pub struct Error {
69
- backtrace : Option < Backtrace > ,
70
- #[ snafu( implicit) ]
71
- span_trace : n0_snafu:: SpanTrace ,
72
- errors : Vec < LookupError > ,
73
- }
56
+ /// Error returned when an input value is too long for [`crate::node_info::UserData`].
57
+ #[ allow( missing_docs) ]
58
+ #[ derive( Debug , Snafu ) ]
59
+ #[ snafu( module) ]
60
+ #[ snafu( display( "no calls succeeded: [{}]" , errors. iter( ) . map( |e| e. to_string( ) ) . collect:: <Vec <_>>( ) . join( "" ) ) ) ]
61
+ pub struct StaggeredError < E : std:: fmt:: Debug + std:: fmt:: Display > {
62
+ backtrace : Option < Backtrace > ,
63
+ #[ snafu( implicit) ]
64
+ span_trace : n0_snafu:: SpanTrace ,
65
+ errors : Vec < E > ,
66
+ }
74
67
75
- impl Error {
76
- pub ( crate ) fn new ( errors : Vec < LookupError > ) -> Self {
77
- Self {
78
- errors,
79
- backtrace : GenerateImplicitData :: generate ( ) ,
80
- span_trace : n0_snafu:: SpanTrace :: generate ( ) ,
81
- }
68
+ impl < E : std:: fmt:: Debug + std:: fmt:: Display > StaggeredError < E > {
69
+ pub ( crate ) fn new ( errors : Vec < E > ) -> Self {
70
+ Self {
71
+ errors,
72
+ backtrace : GenerateImplicitData :: generate ( ) ,
73
+ span_trace : n0_snafu:: SpanTrace :: generate ( ) ,
82
74
}
83
75
}
84
76
}
85
77
86
- pub use staggered:: * ;
87
-
88
78
/// The DNS resolver used throughout `iroh`.
89
79
#[ derive( Debug , Clone ) ]
90
80
pub struct DnsResolver ( TokioResolver ) ;
@@ -252,12 +242,10 @@ impl DnsResolver {
252
242
host : impl ToString ,
253
243
timeout : Duration ,
254
244
delays_ms : & [ u64 ] ,
255
- ) -> Result < impl Iterator < Item = IpAddr > , DnsError > {
245
+ ) -> Result < impl Iterator < Item = IpAddr > , StaggeredError < DnsError > > {
256
246
let host = host. to_string ( ) ;
257
247
let f = || self . lookup_ipv4 ( host. clone ( ) , timeout) ;
258
- stagger_call ( f, delays_ms)
259
- . await
260
- . map_err ( |errors| StaggeredSnafu { errors } . build ( ) )
248
+ stagger_call ( f, delays_ms) . await
261
249
}
262
250
263
251
/// Perform an ipv6 lookup with a timeout in a staggered fashion.
@@ -271,12 +259,10 @@ impl DnsResolver {
271
259
host : impl ToString ,
272
260
timeout : Duration ,
273
261
delays_ms : & [ u64 ] ,
274
- ) -> Result < impl Iterator < Item = IpAddr > , DnsError > {
262
+ ) -> Result < impl Iterator < Item = IpAddr > , StaggeredError < DnsError > > {
275
263
let host = host. to_string ( ) ;
276
264
let f = || self . lookup_ipv6 ( host. clone ( ) , timeout) ;
277
- stagger_call ( f, delays_ms)
278
- . await
279
- . map_err ( |errors| StaggeredSnafu { errors } . build ( ) )
265
+ stagger_call ( f, delays_ms) . await
280
266
}
281
267
282
268
/// Race an ipv4 and ipv6 lookup with a timeout in a staggered fashion.
@@ -291,12 +277,10 @@ impl DnsResolver {
291
277
host : impl ToString ,
292
278
timeout : Duration ,
293
279
delays_ms : & [ u64 ] ,
294
- ) -> Result < impl Iterator < Item = IpAddr > , DnsError > {
280
+ ) -> Result < impl Iterator < Item = IpAddr > , StaggeredError < DnsError > > {
295
281
let host = host. to_string ( ) ;
296
282
let f = || self . lookup_ipv4_ipv6 ( host. clone ( ) , timeout) ;
297
- stagger_call ( f, delays_ms)
298
- . await
299
- . map_err ( |errors| StaggeredSnafu { errors } . build ( ) )
283
+ stagger_call ( f, delays_ms) . await
300
284
}
301
285
302
286
/// Looks up node info by [`NodeId`] and origin domain name.
@@ -335,11 +319,9 @@ impl DnsResolver {
335
319
& self ,
336
320
name : & str ,
337
321
delays_ms : & [ u64 ] ,
338
- ) -> Result < NodeInfo , staggered :: Error > {
322
+ ) -> Result < NodeInfo , StaggeredError < LookupError > > {
339
323
let f = || self . lookup_node_by_domain_name ( name) ;
340
- stagger_call ( f, delays_ms)
341
- . await
342
- . map_err ( staggered:: Error :: new)
324
+ stagger_call ( f, delays_ms) . await
343
325
}
344
326
345
327
/// Looks up node info by [`NodeId`] and origin domain name.
@@ -353,11 +335,9 @@ impl DnsResolver {
353
335
node_id : & NodeId ,
354
336
origin : & str ,
355
337
delays_ms : & [ u64 ] ,
356
- ) -> Result < NodeInfo , staggered :: Error > {
338
+ ) -> Result < NodeInfo , StaggeredError < LookupError > > {
357
339
let f = || self . lookup_node_by_id ( node_id, origin) ;
358
- stagger_call ( f, delays_ms)
359
- . await
360
- . map_err ( staggered:: Error :: new)
340
+ stagger_call ( f, delays_ms) . await
361
341
}
362
342
}
363
343
@@ -447,10 +427,15 @@ impl<A: Iterator<Item = IpAddr>, B: Iterator<Item = IpAddr>> Iterator for Lookup
447
427
///
448
428
/// The first call is performed immediately. The first call to succeed generates an Ok result
449
429
/// ignoring any previous error. If all calls fail, an error summarizing all errors is returned.
450
- async fn stagger_call < T , E , F : Fn ( ) -> Fut , Fut : Future < Output = Result < T , E > > > (
430
+ async fn stagger_call <
431
+ T ,
432
+ E : std:: fmt:: Debug + std:: fmt:: Display ,
433
+ F : Fn ( ) -> Fut ,
434
+ Fut : Future < Output = Result < T , E > > ,
435
+ > (
451
436
f : F ,
452
437
delays_ms : & [ u64 ] ,
453
- ) -> Result < T , Vec < E > > {
438
+ ) -> Result < T , StaggeredError < E > > {
454
439
let mut calls = n0_future:: FuturesUnorderedBounded :: new ( delays_ms. len ( ) + 1 ) ;
455
440
// NOTE: we add the 0 delay here to have a uniform set of futures. This is more performant than
456
441
// using alternatives that allow futures of different types.
@@ -472,7 +457,7 @@ async fn stagger_call<T, E, F: Fn() -> Fut, Fut: Future<Output = Result<T, E>>>(
472
457
}
473
458
}
474
459
475
- Err ( errors)
460
+ Err ( StaggeredError :: new ( errors) )
476
461
}
477
462
478
463
#[ cfg( test) ]
0 commit comments