Skip to content

Commit

Permalink
Limit OnionMessenger event buffer size.
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinewallace committed Apr 16, 2024
1 parent 1f22d24 commit 55d6cb3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,17 @@ where
}
msgs
}

fn enqueue_event(&self, event: Event) {
const MAX_EVENTS_BUFFER_SIZE: usize = (1 << 10) * 256;
let mut pending_events = self.pending_events.lock().unwrap();
let total_buffered_bytes: usize = pending_events
.iter()
.map(|ev| ev.serialized_length())
.sum();
if total_buffered_bytes >= MAX_EVENTS_BUFFER_SIZE { return }
pending_events.push(event);
}
}

fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap<PublicKey, OnionMessageRecipient>) -> bool {
Expand Down Expand Up @@ -1134,7 +1145,7 @@ where
log_trace!(logger, "Forwarding an onion message to peer {}", next_node_id);
},
_ if self.intercept_oms_for_offline_peers => {
self.pending_events.lock().unwrap().push(
self.enqueue_event(
Event::OnionMessageForOfflinePeer {
peer_node_id: next_node_id, message: onion_message
}
Expand Down Expand Up @@ -1162,7 +1173,7 @@ where
.or_insert_with(|| OnionMessageRecipient::ConnectedPeer(VecDeque::new()))
.mark_connected();
if self.intercept_oms_for_offline_peers {
self.pending_events.lock().unwrap().push(
self.enqueue_event(
Event::OnionMessagePeerConnected { peer_node_id: *their_node_id }
);
}
Expand Down

0 comments on commit 55d6cb3

Please sign in to comment.