@@ -35,10 +35,30 @@ use std::{
35
35
collections:: { HashMap , HashSet , VecDeque } ,
36
36
iter,
37
37
task:: { Context , Poll } ,
38
+ time:: Duration ,
38
39
} ;
39
40
40
41
type FiniteAddrScore = u32 ;
41
42
43
+ pub struct Config {
44
+ timeout : Duration ,
45
+ }
46
+
47
+ impl Default for Config {
48
+ fn default ( ) -> Self {
49
+ Config {
50
+ timeout : Duration :: from_secs ( 5 ) ,
51
+ }
52
+ }
53
+ }
54
+
55
+ impl Config {
56
+ pub fn with_timeout ( mut self , timeout : Duration ) -> Self {
57
+ self . timeout = timeout;
58
+ self
59
+ }
60
+ }
61
+
42
62
pub struct Behaviour {
43
63
inner : RequestResponse < AutoNatCodec > ,
44
64
local_addresses : HashMap < Multiaddr , FiniteAddrScore > ,
@@ -49,8 +69,15 @@ pub struct Behaviour {
49
69
50
70
impl Default for Behaviour {
51
71
fn default ( ) -> Self {
72
+ Behaviour :: new ( Config :: default ( ) )
73
+ }
74
+ }
75
+
76
+ impl Behaviour {
77
+ pub fn new ( config : Config ) -> Self {
52
78
let protocols = iter:: once ( ( AutoNatProtocol , ProtocolSupport :: Full ) ) ;
53
- let cfg = RequestResponseConfig :: default ( ) ;
79
+ let mut cfg = RequestResponseConfig :: default ( ) ;
80
+ cfg. set_request_timeout ( config. timeout ) ;
54
81
let inner = RequestResponse :: new ( AutoNatCodec , protocols, cfg) ;
55
82
Self {
56
83
inner,
@@ -60,9 +87,7 @@ impl Default for Behaviour {
60
87
send_request : VecDeque :: default ( ) ,
61
88
}
62
89
}
63
- }
64
90
65
- impl Behaviour {
66
91
pub fn add_local_address ( & mut self , address : Multiaddr ) {
67
92
if self . local_addresses . get ( & address) . is_none ( ) {
68
93
self . local_addresses . insert ( address, 1 ) ;
0 commit comments