Skip to content

Commit 3fc78f0

Browse files
committed
protocols/autonat/behaviour: timeout config
1 parent 2e042d8 commit 3fc78f0

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

protocols/autonat/src/behaviour.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,30 @@ use std::{
3535
collections::{HashMap, HashSet, VecDeque},
3636
iter,
3737
task::{Context, Poll},
38+
time::Duration,
3839
};
3940

4041
type FiniteAddrScore = u32;
4142

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+
4262
pub struct Behaviour {
4363
inner: RequestResponse<AutoNatCodec>,
4464
local_addresses: HashMap<Multiaddr, FiniteAddrScore>,
@@ -49,8 +69,15 @@ pub struct Behaviour {
4969

5070
impl Default for Behaviour {
5171
fn default() -> Self {
72+
Behaviour::new(Config::default())
73+
}
74+
}
75+
76+
impl Behaviour {
77+
pub fn new(config: Config) -> Self {
5278
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);
5481
let inner = RequestResponse::new(AutoNatCodec, protocols, cfg);
5582
Self {
5683
inner,
@@ -60,9 +87,7 @@ impl Default for Behaviour {
6087
send_request: VecDeque::default(),
6188
}
6289
}
63-
}
6490

65-
impl Behaviour {
6691
pub fn add_local_address(&mut self, address: Multiaddr) {
6792
if self.local_addresses.get(&address).is_none() {
6893
self.local_addresses.insert(address, 1);

protocols/autonat/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
//! Implementation of the [AutoNAT] protocol.
2222
23-
pub use self::behaviour::Behaviour;
23+
pub use self::behaviour::{Behaviour, Config};
2424

2525
mod behaviour;
2626
mod protocol;

0 commit comments

Comments
 (0)