Skip to content

Commit

Permalink
clears cached getaddr response if it can't refresh for over a minute …
Browse files Browse the repository at this point in the history
…after the refresh time
  • Loading branch information
arya2 committed Nov 20, 2023
1 parent d272729 commit 88effb4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
4 changes: 0 additions & 4 deletions zebrad/src/components/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ pub const GETDATA_SENT_BYTES_LIMIT: usize = 1_000_000;
/// many peers instead of just a few peers.)
pub const GETDATA_MAX_BLOCK_COUNT: usize = 16;

/// The maximum duration that a `CachedPeerAddrResponse` is considered fresh before the inbound service
/// should get new peer addresses from the address book to send as a `GetAddr` response.
const INBOUND_CACHED_ADDRS_REFRESH_INTERVAL: Duration = Duration::from_secs(10 * 60);

type BlockDownloadPeerSet =
Buffer<BoxService<zn::Request, zn::Response, zn::BoxError>, zn::Request>;
type State = Buffer<BoxService<zs::Request, zs::Response, zs::BoxError>, zs::Request>;
Expand Down
26 changes: 20 additions & 6 deletions zebrad/src/components/inbound/cached_peer_addr_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ use std::{

use super::*;

/// The maximum duration that a `CachedPeerAddrResponse` is considered fresh before the inbound service
/// should get new peer addresses from the address book to send as a `GetAddr` response.
const INBOUND_CACHED_ADDRS_REFRESH_INTERVAL: Duration = Duration::from_secs(10 * 60);

/// The maximum duration that a `CachedPeerAddrResponse` is considered fresh before the inbound service
/// should get new peer addresses from the address book to send as a `GetAddr` response.
const INBOUND_CACHED_ADDRS_MAX_FRESH_DURATION: Duration = Duration::from_secs(60);

/// Caches and refreshes a partial list of peer addresses to be returned as a `GetAddr` response.
pub struct CachedPeerAddrResponse {
/// A shared list of peer addresses.
Expand Down Expand Up @@ -44,6 +52,8 @@ impl CachedPeerAddrResponse {
return;
}

let max_fresh_time = self.refresh_time + INBOUND_CACHED_ADDRS_MAX_FRESH_DURATION;

// try getting a lock on the address book if it's time to refresh the cached addresses
match self
.address_book
Expand All @@ -56,20 +66,24 @@ impl CachedPeerAddrResponse {
self.value = zn::Response::Peers(peers);
}

Ok(_) if now > max_fresh_time => {
self.value = zn::Response::Nil;
}

Err(TryLockError::WouldBlock) if now > max_fresh_time => {
warn!("getaddrs response hasn't been refreshed in some time");
self.value = zn::Response::Nil;
}

Ok(_) => {
debug!(
"could not refresh cached response because our address \
book has no available peers"
);
}

Err(TryLockError::WouldBlock) => {
let next_refresh_time = self.refresh_time + INBOUND_CACHED_ADDRS_REFRESH_INTERVAL;
Err(TryLockError::WouldBlock) => {}

if now > next_refresh_time {
warn!("getaddrs response hasn't been refreshed in some time");
};
}
Err(TryLockError::Poisoned(_)) => {
panic!("previous thread panicked while holding the address book lock")
}
Expand Down

0 comments on commit 88effb4

Please sign in to comment.