Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fire all timers at the same time. #2212

Merged
merged 3 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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