Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
uses iterators for handling ping/pong messages
Browse files Browse the repository at this point in the history
  • Loading branch information
behzadnouri committed Oct 12, 2020
1 parent b012388 commit 4456c9f
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions core/src/cluster_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1772,10 +1772,10 @@ impl ClusterInfo {
})
});

if let Some(response) = self.handle_ping_messages(ping_messages, recycler) {
if let Some(response) = self.handle_ping_messages(ping_messages.into_iter(), recycler) {
let _ = response_sender.send(response);
}
self.handle_pong_messages(pong_messages, Instant::now());
self.handle_pong_messages(pong_messages.into_iter(), Instant::now());
for (from, data) in pull_responses {
self.handle_pull_response(&from, data, &timeouts);
}
Expand Down Expand Up @@ -2046,14 +2046,12 @@ impl ClusterInfo {
}
}

fn handle_ping_messages(
&self,
msgs: Vec<(Ping, SocketAddr)>,
recycler: &PacketsRecycler,
) -> Option<Packets> {
fn handle_ping_messages<I>(&self, pings: I, recycler: &PacketsRecycler) -> Option<Packets>
where
I: Iterator<Item = (Ping, SocketAddr)>,
{
let mut verify_failed = 0;
let packets: Vec<_> = msgs
.into_iter()
let packets: Vec<_> = pings
.filter_map(|(ping, addr)| {
if ping.verify() {
let pong = Pong::new(&ping, &self.keypair).ok()?;
Expand All @@ -2076,11 +2074,15 @@ impl ClusterInfo {
}
}

fn handle_pong_messages(&self, msgs: Vec<(Pong, SocketAddr)>, now: Instant) {
if !msgs.is_empty() {
fn handle_pong_messages<I>(&self, pongs: I, now: Instant)
where
I: Iterator<Item = (Pong, SocketAddr)>,
{
let mut pongs = pongs.peekable();
if pongs.peek().is_some() {
let mut verify_failed = 0;
let mut ping_cache = self.ping_cache.write().unwrap();
for (pong, addr) in msgs {
for (pong, addr) in pongs {
if pong.verify() {
ping_cache.add(&pong, addr, now);
} else {
Expand Down

0 comments on commit 4456c9f

Please sign in to comment.