Skip to content

Commit 59733af

Browse files
committed
Upper-bound the event queue size
We add a size limit on the event queue, after which we'll just start dropping events to ensure we could never OOM. Additionally, we document the requirement that users need to handle generated events ASAP.
1 parent 69c64b8 commit 59733af

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lightning-liquidity/src/events.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ use crate::sync::{Arc, Mutex};
2424
use core::future::Future;
2525
use core::task::{Poll, Waker};
2626

27+
/// The maximum queue size we allow before starting to drop events.
28+
pub const MAX_EVENT_QUEUE_SIZE: usize = 1000;
29+
2730
pub(crate) struct EventQueue {
2831
queue: Arc<Mutex<VecDeque<Event>>>,
2932
waker: Arc<Mutex<Option<Waker>>>,
@@ -47,7 +50,11 @@ impl EventQueue {
4750
pub fn enqueue(&self, event: Event) {
4851
{
4952
let mut queue = self.queue.lock().unwrap();
50-
queue.push_back(event);
53+
if queue.len() < MAX_EVENT_QUEUE_SIZE {
54+
queue.push_back(event);
55+
} else {
56+
return;
57+
}
5158
}
5259

5360
if let Some(waker) = self.waker.lock().unwrap().take() {

lightning-liquidity/src/manager.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ where {
369369
/// Blocks the current thread until next event is ready and returns it.
370370
///
371371
/// Typically you would spawn a thread or task that calls this in a loop.
372+
///
373+
/// **Note**: Users must handle events as soon as possible to avoid an increased event queue
374+
/// memory footprint. We will start dropping any generated events after
375+
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
376+
///
377+
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
372378
#[cfg(feature = "std")]
373379
pub fn wait_next_event(&self) -> Event {
374380
self.pending_events.wait_next_event()
@@ -377,20 +383,38 @@ where {
377383
/// Returns `Some` if an event is ready.
378384
///
379385
/// Typically you would spawn a thread or task that calls this in a loop.
386+
///
387+
/// **Note**: Users must handle events as soon as possible to avoid an increased event queue
388+
/// memory footprint. We will start dropping any generated events after
389+
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
390+
///
391+
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
380392
pub fn next_event(&self) -> Option<Event> {
381393
self.pending_events.next_event()
382394
}
383395

384396
/// Asynchronously polls the event queue and returns once the next event is ready.
385397
///
386398
/// Typically you would spawn a thread or task that calls this in a loop.
399+
///
400+
/// **Note**: Users must handle events as soon as possible to avoid an increased event queue
401+
/// memory footprint. We will start dropping any generated events after
402+
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
403+
///
404+
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
387405
pub async fn next_event_async(&self) -> Event {
388406
self.pending_events.next_event_async().await
389407
}
390408

391409
/// Returns and clears all events without blocking.
392410
///
393411
/// Typically you would spawn a thread or task that calls this in a loop.
412+
///
413+
/// **Note**: Users must handle events as soon as possible to avoid an increased event queue
414+
/// memory footprint. We will start dropping any generated events after
415+
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
416+
///
417+
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
394418
pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
395419
self.pending_events.get_and_clear_pending_events()
396420
}

0 commit comments

Comments
 (0)