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 { ... }
}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§
Sourcefn write_batch(
&mut self,
batch: Vec<T>,
) -> impl Future<Output = Result<(), DrainError>> + Send
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§
Sourcefn flush_durable(
&mut self,
) -> impl Future<Output = Result<(), DrainError>> + Send
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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".