Skip to main content

SinkDrain

Trait SinkDrain 

Source
pub trait SinkDrain<T: Send>: Send + 'static {
    // Required method
    fn write_batch(
        &mut self,
        batch: Vec<T>,
    ) -> impl Future<Output = Result<(), DrainError>> + Send;

    // Provided methods
    fn flush_durable(
        &mut self,
    ) -> impl Future<Output = Result<(), DrainError>> + Send { ... }
    fn close(&mut self) -> impl Future<Output = Result<(), DrainError>> + Send { ... }
}
Available on crate feature concurrency only.
Expand description

A drain consumes batches of messages and writes them to the backend.

Implementations are captured by BackgroundSink::spawn and live inside the actor task. They run only on the actor – never on consumer hot paths.

CRITICAL: drain methods may take time (disk fsync, network roundtrip). Use true-async I/O (tokio::fs, async clients like rdkafka / reqwest / redis) or tokio::task::spawn_blocking for unavoidable sync work. NEVER put std::fs::* / std::io::Write::* / std::thread::sleep directly in write_batch – that pins the actor task’s tokio worker, and tests/sync_in_async.rs will fail the lint.

Required Methods§

Source

fn write_batch( &mut self, batch: Vec<T>, ) -> impl Future<Output = Result<(), DrainError>> + Send

Flush a batch. Implementer chooses the async I/O strategy. Returned Err is logged + counted by the actor; the actor continues draining subsequent batches.

Takes &mut self – drains typically own mutable I/O state (file handles, connection pools, write buffers). The actor holds the only reference; this method is never called concurrently for a given drain instance.

Provided Methods§

Source

fn flush_durable( &mut self, ) -> impl Future<Output = Result<(), DrainError>> + Send

Block until every entry written to this drain so far is durable (synced to disk for file backends, acked by the broker for network backends). Called by the actor when it processes a BackgroundSink::flush() barrier – BEFORE acking the barrier – so callers of flush() see real durability, not just “the bytes were handed to the kernel”.

Default: no-op (the trait stays additive; non-durable drains pay nothing). Implementers with durability semantics (fsync, Kafka producer flush, etc.) override.

Source

fn close(&mut self) -> impl Future<Output = Result<(), DrainError>> + Send

One-shot close at actor shutdown. Default: no-op. Typical implementations flush remaining state, close file handles, return network connections to a pool.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§