chore(discv4): limit number of queued pings (#7999)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Oliver Nordbjerg
2024-04-30 16:30:00 +02:00
committed by GitHub
parent d0973bb6dc
commit 6d7cd53ad2

View File

@ -119,10 +119,21 @@ const MIN_PACKET_SIZE: usize = 32 + 65 + 1;
/// Concurrency factor for `FindNode` requests to pick `ALPHA` closest nodes, <https://github.com/ethereum/devp2p/blob/master/discv4.md#recursive-lookup>
const ALPHA: usize = 3;
/// Maximum number of nodes to ping at concurrently. 2 full `Neighbours` responses with 16 _new_
/// nodes. This will apply some backpressure in recursive lookups.
/// Maximum number of nodes to ping at concurrently.
///
/// This corresponds to 2 full `Neighbours` responses with 16 _new_ nodes. This will apply some
/// backpressure in recursive lookups.
const MAX_NODES_PING: usize = 2 * MAX_NODES_PER_BUCKET;
/// Maximum number of pings to keep queued.
///
/// If we are currently sending too many pings, any new pings will be queued. To prevent unbounded
/// growth of the queue, the queue has a maximum capacity, after which any additional pings will be
/// discarded.
///
/// This corresponds to 2 full `Neighbours` responses with 16 new nodes.
const MAX_QUEUED_PINGS: usize = 2 * MAX_NODES_PER_BUCKET;
/// The size of the datagram is limited [`MAX_PACKET_SIZE`], 16 nodes, as the discv4 specifies don't
/// fit in one datagram. The safe number of nodes that always fit in a datagram is 12, with worst
/// case all of them being IPv6 nodes. This is calculated by `(MAX_PACKET_SIZE - (header + expire +
@ -570,7 +581,7 @@ impl Discv4Service {
_tasks: tasks,
ingress: ingress_rx,
egress: egress_tx,
queued_pings: Default::default(),
queued_pings: VecDeque::with_capacity(MAX_QUEUED_PINGS),
pending_pings: Default::default(),
pending_lookup: Default::default(),
pending_find_nodes: Default::default(),
@ -1131,7 +1142,7 @@ impl Discv4Service {
if self.pending_pings.len() < MAX_NODES_PING {
self.send_ping(node, reason);
} else {
} else if self.queued_pings.len() < MAX_QUEUED_PINGS {
self.queued_pings.push_back((node, reason));
}
}