Add more methods for creating MetaAddrs

This refactor lets us remove `MetaAddr::update_last_seen()`.
This commit is contained in:
teor 2021-03-25 20:14:52 +10:00
parent 6fe81d8992
commit 1a159dfcb6
3 changed files with 33 additions and 13 deletions

View File

@ -140,6 +140,33 @@ impl MetaAddr {
}
}
/// Create a new `MetaAddr` for a peer that we want to reconnect to.
pub fn new_reconnect(addr: &SocketAddr, services: &PeerServices) -> MetaAddr {
MetaAddr {
addr: *addr,
services: *services,
last_seen: Utc::now(),
last_connection_state: AttemptPending,
}
}
/// Create a new `MetaAddr` for a peer that has just had an error.
pub fn new_errored(addr: &SocketAddr, services: &PeerServices) -> MetaAddr {
MetaAddr {
addr: *addr,
services: *services,
last_seen: Utc::now(),
last_connection_state: Failed,
}
}
/// Create a new `MetaAddr` for a peer that has just shut down.
pub fn new_shutdown(addr: &SocketAddr, services: &PeerServices) -> MetaAddr {
// TODO: if the peer shut down in the Responded state, preserve that
// state. All other states should be treated as (timeout) errors.
MetaAddr::new_errored(addr, services)
}
/// The last time we interacted with this peer.
///
/// The exact meaning depends on `last_connection_state`:
@ -158,11 +185,6 @@ impl MetaAddr {
self.last_seen
}
/// Update the last time we interacted with this peer to the current time.
pub fn update_last_seen(&mut self) {
self.last_seen = Utc::now();
}
/// Return a sanitized version of this `MetaAddr`, for sending to a remote peer.
pub fn sanitize(&self) -> MetaAddr {
let interval = crate::constants::TIMESTAMP_TRUNCATION_SECONDS;

View File

@ -8,7 +8,7 @@ use futures::stream::{FuturesUnordered, StreamExt};
use tokio::time::{sleep, sleep_until, Sleep};
use tower::{Service, ServiceExt};
use crate::{types::MetaAddr, AddressBook, BoxError, PeerAddrState, Request, Response};
use crate::{types::MetaAddr, AddressBook, BoxError, Request, Response};
/// The `CandidateSet` manages the `PeerSet`'s peer reconnection attempts.
///
@ -218,10 +218,9 @@ where
let mut peer_set_guard = self.peer_set.lock().unwrap();
// It's okay to early return here because we're returning None
// instead of yielding the next connection.
let mut reconnect = peer_set_guard.reconnection_peers().next()?;
let reconnect = peer_set_guard.reconnection_peers().next()?;
reconnect.update_last_seen();
reconnect.last_connection_state = PeerAddrState::AttemptPending;
let reconnect = MetaAddr::new_reconnect(&reconnect.addr, &reconnect.services);
peer_set_guard.update(reconnect);
reconnect
};
@ -233,9 +232,8 @@ where
}
/// Mark `addr` as a failed peer.
pub fn report_failed(&mut self, mut addr: MetaAddr) {
addr.update_last_seen();
addr.last_connection_state = PeerAddrState::Failed;
pub fn report_failed(&mut self, addr: &MetaAddr) {
let addr = MetaAddr::new_errored(&addr.addr, &addr.services);
self.peer_set.lock().unwrap().update(addr);
}
}

View File

@ -365,7 +365,7 @@ where
}
Right((Some(Err(candidate)), _)) => {
debug!(?candidate.addr, "marking candidate as failed");
candidates.report_failed(candidate);
candidates.report_failed(&candidate);
// The demand signal that was taken out of the queue
// to attempt to connect to the failed candidate never
// turned into a connection, so add it back: