Skip to content

Commit 7e42b36

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 9c9b4a8 commit 7e42b36

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
@@ -378,6 +378,12 @@ where {
378378
/// Blocks the current thread until next event is ready and returns it.
379379
///
380380
/// Typically you would spawn a thread or task that calls this in a loop.
381+
///
382+
/// **Note**: Users must handle events as soon as possible to avoid an increased event queue
383+
/// memory footprint. We will start dropping any generated events after
384+
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
385+
///
386+
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
381387
#[cfg(feature = "std")]
382388
pub fn wait_next_event(&self) -> Event {
383389
self.pending_events.wait_next_event()
@@ -386,20 +392,38 @@ where {
386392
/// Returns `Some` if an event is ready.
387393
///
388394
/// Typically you would spawn a thread or task that calls this in a loop.
395+
///
396+
/// **Note**: Users must handle events as soon as possible to avoid an increased event queue
397+
/// memory footprint. We will start dropping any generated events after
398+
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
399+
///
400+
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
389401
pub fn next_event(&self) -> Option<Event> {
390402
self.pending_events.next_event()
391403
}
392404

393405
/// Asynchronously polls the event queue and returns once the next event is ready.
394406
///
395407
/// Typically you would spawn a thread or task that calls this in a loop.
408+
///
409+
/// **Note**: Users must handle events as soon as possible to avoid an increased event queue
410+
/// memory footprint. We will start dropping any generated events after
411+
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
412+
///
413+
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
396414
pub async fn next_event_async(&self) -> Event {
397415
self.pending_events.next_event_async().await
398416
}
399417

400418
/// Returns and clears all events without blocking.
401419
///
402420
/// Typically you would spawn a thread or task that calls this in a loop.
421+
///
422+
/// **Note**: Users must handle events as soon as possible to avoid an increased event queue
423+
/// memory footprint. We will start dropping any generated events after
424+
/// [`MAX_EVENT_QUEUE_SIZE`] has been reached.
425+
///
426+
/// [`MAX_EVENT_QUEUE_SIZE`]: crate::events::MAX_EVENT_QUEUE_SIZE
403427
pub fn get_and_clear_pending_events(&self) -> Vec<Event> {
404428
self.pending_events.get_and_clear_pending_events()
405429
}

0 commit comments

Comments
 (0)