Skip to main content

EventBuffer

Struct EventBuffer 

Source
pub struct EventBuffer { /* private fields */ }
Expand description

A restart-safe, FIFO event buffer backed by a single SQLite file.

Events are appended with enqueue while the upstream sink is unreachable and replayed in insertion order with drain_and_send once it recovers. The file is opened in WAL mode so buffered events survive a process restart.

Implementations§

Source§

impl EventBuffer

Source

pub fn new(path: impl AsRef<Path>, cap: usize) -> Result<Self>

Open (creating if absent) a buffer at path retaining at most cap events.

Enables WAL journaling and synchronous = NORMAL for a durability/perf balance, and creates the events table if it does not yet exist. Parent directories are created as needed.

Source

pub fn from_config(config: &SqliteBufferConfig) -> Result<Self>

Open a buffer from operator SqliteBufferConfig.

Source

pub fn cap(&self) -> usize

The maximum number of events this buffer retains before eviction.

Source

pub fn journal_mode(&self) -> Result<String>

The SQLite journal_mode in force on this buffer’s connection ("wal" once opened).

Source

pub fn synchronous(&self) -> Result<i64>

The SQLite synchronous level in force on this buffer’s connection (1 = NORMAL).

Source

pub fn len(&self) -> Result<usize>

Number of events currently buffered.

Source

pub fn is_empty(&self) -> Result<bool>

Whether the buffer currently holds no events.

Source

pub fn enqueue(&self, event: &AuditEntry) -> Result<()>

Append event to the buffer.

The entry is serialized to JSON and stored as a BLOB. When the buffer would exceed its cap, the oldest events are evicted to make room and the METRIC_EVENTS_DROPPED counter is bumped by the number dropped — the loss is metered, never silent. Each accepted event bumps METRIC_EVENTS_BUFFERED.

Source

pub async fn drain_and_send(&self, sink: &dyn AuditSink) -> Result<usize>

Replay buffered events to sink in insertion (FIFO) order.

Each event is sent and, only after the sink acknowledges it, deleted from the buffer — so a crash mid-flush replays at-least-once rather than losing data. Draining stops at the first sink failure (the upstream is treated as still-unreachable), leaving the remaining events buffered for a later retry. Returns the number of events flushed; each one bumps METRIC_EVENTS_FLUSHED.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.