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

fix: immediately refill slots #6997

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Changes from 2 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
55 changes: 34 additions & 21 deletions crates/net/network/src/peers/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ impl PeersManager {
handle_rx: UnboundedReceiverStream::new(handle_rx),
queued_actions: Default::default(),
reputation_weights,
refill_slots_interval: tokio::time::interval_at(
now + refill_slots_interval,
refill_slots_interval,
),
refill_slots_interval: tokio::time::interval(refill_slots_interval),
release_interval: tokio::time::interval_at(now + unban_interval, unban_interval),
connection_info,
ban_list,
Expand Down Expand Up @@ -1314,6 +1311,16 @@ impl PeersConfig {
let nodes: HashSet<NodeRecord> = serde_json::from_reader(reader)?;
Ok(self.with_basic_nodes(nodes))
}

/// Returns settings for testing
#[cfg(test)]
fn test() -> Self {
Self {
refill_slots_interval: Duration::from_millis(100),
backoff_durations: PeerBackoffDurations::test(),
..Default::default()
}
}
}

/// The durations to use when a backoff should be applied to a peer.
Expand Down Expand Up @@ -1357,6 +1364,17 @@ impl PeerBackoffDurations {
let now = std::time::Instant::now();
now + backoff_time.min(self.max)
}

/// Returns durations for testing.
#[cfg(test)]
const fn test() -> Self {
PeerBackoffDurations {
low: Duration::from_millis(200),
medium: Duration::from_millis(200),
high: Duration::from_millis(200),
max: Duration::from_millis(200),
}
}
}

impl Default for PeerBackoffDurations {
Expand Down Expand Up @@ -1520,9 +1538,8 @@ mod tests {
let peer = PeerId::random();
let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)), 8008);

let backoff_durations =
PeerBackoffDurations { low: Duration::from_millis(200), ..Default::default() };
let config = PeersConfig { backoff_durations, ..Default::default() };
let backoff_durations = PeerBackoffDurations::test();
let config = PeersConfig { backoff_durations, ..PeersConfig::test() };
mattsse marked this conversation as resolved.
Show resolved Hide resolved
let mut peers = PeersManager::new(config);
peers.add_peer(peer, socket_addr, None);

Expand Down Expand Up @@ -1580,12 +1597,8 @@ mod tests {
let peer = PeerId::random();
let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)), 8008);

let backoff_durations = PeerBackoffDurations {
high: Duration::from_millis(200),
low: Duration::from_millis(200),
..Default::default()
};
let config = PeersConfig { backoff_durations, ..Default::default() };
let backoff_durations = PeerBackoffDurations::test();
let config = PeersConfig { backoff_durations, ..PeersConfig::test() };
let mut peers = PeersManager::new(config);
mattsse marked this conversation as resolved.
Show resolved Hide resolved
peers.add_peer(peer, socket_addr, None);

Expand Down Expand Up @@ -1642,7 +1655,7 @@ mod tests {
async fn test_low_backoff() {
let peer = PeerId::random();
let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)), 8008);
let config = PeersConfig::default();
let config = PeersConfig::test();
let mut peers = PeersManager::new(config);
peers.add_peer(peer, socket_addr, None);
let peer_struct = peers.peers.get_mut(&peer).unwrap();
Expand Down Expand Up @@ -1744,7 +1757,7 @@ mod tests {
async fn test_remove_on_max_backoff_count() {
let peer = PeerId::random();
let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)), 8008);
let config = PeersConfig::default();
let config = PeersConfig::test();
let mut peers = PeersManager::new(config.clone());
peers.add_peer(peer, socket_addr, None);
let peer_struct = peers.peers.get_mut(&peer).unwrap();
Expand Down Expand Up @@ -1877,7 +1890,7 @@ mod tests {
async fn test_dropped_incoming() {
let socket_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(1, 0, 1, 2)), 8008);
let ban_duration = Duration::from_millis(500);
let config = PeersConfig { ban_duration, ..Default::default() };
let config = PeersConfig { ban_duration, ..PeersConfig::test() };
let mut peers = PeersManager::new(config);

assert!(peers.on_incoming_pending_session(socket_addr.ip()).is_ok());
Expand Down Expand Up @@ -2068,7 +2081,7 @@ mod tests {
let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2));
let socket_addr = SocketAddr::new(ip, 8008);
let ban_list = BanList::new(HashSet::new(), vec![ip]);
let config = PeersConfig::default().with_ban_list(ban_list);
let config = PeersConfig::test().with_ban_list(ban_list);
let mut peer_manager = PeersManager::new(config);
let a = peer_manager.on_incoming_pending_session(socket_addr.ip());
// because we have no active peers this should be fine for testings
Expand All @@ -2089,7 +2102,7 @@ mod tests {
let socket_addr = SocketAddr::new(ip, 8008);
let given_peer_id = PeerId::random();
let ban_list = BanList::new(vec![given_peer_id], HashSet::new());
let config = PeersConfig::default().with_ban_list(ban_list);
let config = PeersConfig::test().with_ban_list(ban_list);
let mut peer_manager = PeersManager::new(config);
peer_manager.on_incoming_session_established(given_peer_id, socket_addr);

Expand Down Expand Up @@ -2144,7 +2157,7 @@ mod tests {
async fn test_trusted_peers_are_prioritized() {
let trusted_peer = PeerId::random();
let trusted_sock = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)), 8008);
let config = PeersConfig::default().with_trusted_nodes(HashSet::from([NodeRecord {
let config = PeersConfig::test().with_trusted_nodes(HashSet::from([NodeRecord {
address: IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)),
tcp_port: 8008,
udp_port: 8008,
Expand Down Expand Up @@ -2182,7 +2195,7 @@ mod tests {
async fn test_connect_trusted_nodes_only() {
let trusted_peer = PeerId::random();
let trusted_sock = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)), 8008);
let config = PeersConfig::default()
let config = PeersConfig::test()
.with_trusted_nodes(HashSet::from([NodeRecord {
address: IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2)),
tcp_port: 8008,
Expand Down Expand Up @@ -2220,7 +2233,7 @@ mod tests {
async fn test_tick() {
let ip = IpAddr::V4(Ipv4Addr::new(127, 0, 1, 2));
let socket_addr = SocketAddr::new(ip, 8008);
let config = PeersConfig::default();
let config = PeersConfig::test();
let mut peer_manager = PeersManager::new(config);
let peer_id = PeerId::random();
peer_manager.add_peer(peer_id, socket_addr, None);
Expand Down
Loading