Skip to content

Commit

Permalink
Optimise getting the next connection attempt address
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed May 17, 2021
1 parent 5b910a8 commit f377a6c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
22 changes: 6 additions & 16 deletions zebra-network/src/address_book.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
//! The `AddressBook` manages information about what peers exist, when they were
//! seen, and what services they provide.
use std::{
collections::{BTreeSet, HashMap},
iter::Extend,
net::SocketAddr,
time::Instant,
};
use std::{collections::HashMap, iter::Extend, net::SocketAddr, time::Instant};

use chrono::{DateTime, Utc};
use tracing::Span;
Expand Down Expand Up @@ -323,19 +318,14 @@ impl AddressBook {
.cloned()
}

/// Return an iterator over peers that are due for a connection attempt,
/// in connection attempt order.
pub fn connection_peers_ordered(&'_ self) -> impl Iterator<Item = MetaAddr> + '_ {
/// Return the next peer that is due for a connection attempt.
pub fn next_attempt_peer(&self) -> Option<MetaAddr> {
let _guard = self.span.enter();

// TODO: optimise, if needed, or get rid of older peers

// Skip recently used peers (including live peers), then sort using BTreeSet
self.by_addr
.values()
// Skip recently used peers (including live peers)
self.peers_unordered()
.filter(move |peer| !self.recently_used_addr(&peer.addr))
.collect::<BTreeSet<_>>()
.into_iter()
.max()
.cloned()
}

Expand Down
2 changes: 1 addition & 1 deletion zebra-network/src/peer_set/candidate_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ where
let mut guard = self.address_book.lock().unwrap();
// It's okay to return without sleeping here, because we're returning
// `None`. We only need to sleep before yielding an address.
let connect = guard.connection_peers_ordered().next()?;
let connect = guard.next_attempt_peer()?;

let connect = MetaAddr::update_attempt(&connect.addr);
guard.update(connect)?
Expand Down

0 comments on commit f377a6c

Please sign in to comment.