Skip to content

Commit

Permalink
protocols/mdns: Do not fire all timers at the same time. (#2212)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Inden <mail@max-inden.de>
  • Loading branch information
dvc94ch and mxinden authored Sep 7, 2021
1 parent 733a0b6 commit 67722c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
5 changes: 4 additions & 1 deletion protocols/mdns/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
- Add support for IPv6. To enable set the multicast address
in `MdnsConfig` to `IPV6_MDNS_MULTICAST_ADDRESS`.
See [PR 2161] for details.


- Prevent timers from firing at the same time. See [PR 2212] for details.

[PR 2161]: https://github.com/libp2p/rust-libp2p/pull/2161/
[PR 2212]: https://github.com/libp2p/rust-libp2p/pull/2212/

# 0.31.0 [2021-07-12]

Expand Down
31 changes: 22 additions & 9 deletions protocols/mdns/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ impl Mdns {
Async::new(socket)?
};
let if_watch = if_watch::IfWatcher::new().await?;
// randomize timer to prevent all converging and firing at the same time.
let query_interval = {
use rand::Rng;
let mut rng = rand::thread_rng();
let jitter = rng.gen_range(0..100);
config.query_interval + Duration::from_millis(jitter)
};
Ok(Self {
recv_socket,
send_socket,
Expand All @@ -162,9 +169,9 @@ impl Mdns {
discovered_nodes: SmallVec::new(),
closest_expiration: None,
events: Default::default(),
query_interval: config.query_interval,
query_interval,
ttl: config.ttl,
timeout: Timer::interval(config.query_interval),
timeout: Timer::interval(query_interval),
multicast_addr: config.multicast_addr,
})
}
Expand All @@ -179,10 +186,19 @@ impl Mdns {
self.discovered_nodes.iter().map(|(p, _, _)| p)
}

fn reset_timer(&mut self) {
self.timeout.set_interval(self.query_interval);
}

fn fire_timer(&mut self) {
self.timeout
.set_interval_at(Instant::now(), self.query_interval);
}

fn inject_mdns_packet(&mut self, packet: MdnsPacket, params: &impl PollParameters) {
match packet {
MdnsPacket::Query(query) => {
self.timeout.set_interval(self.query_interval);
self.reset_timer();
log::trace!("sending response");
for packet in build_query_response(
query.query_id(),
Expand Down Expand Up @@ -279,8 +295,7 @@ impl NetworkBehaviour for Mdns {
}

fn inject_new_listen_addr(&mut self, _id: ListenerId, _addr: &Multiaddr) {
self.timeout
.set_interval_at(Instant::now(), self.query_interval);
self.fire_timer();
}

fn poll(
Expand All @@ -302,8 +317,7 @@ impl NetworkBehaviour for Mdns {
if let Err(err) = socket.join_multicast_v4(&multicast, &addr) {
log::error!("join multicast failed: {}", err);
} else {
self.timeout
.set_interval_at(Instant::now(), self.query_interval);
self.fire_timer();
}
}
}
Expand All @@ -313,8 +327,7 @@ impl NetworkBehaviour for Mdns {
if let Err(err) = socket.join_multicast_v6(&multicast, 0) {
log::error!("join multicast failed: {}", err);
} else {
self.timeout
.set_interval_at(Instant::now(), self.query_interval);
self.fire_timer();
}
}
}
Expand Down

0 comments on commit 67722c5

Please sign in to comment.