Skip to main content

Sink

Trait Sink 

Source
pub trait Sink:
    Send
    + Sync
    + 'static {
    type Error: StdError + Send + Sync + 'static;

    // Required method
    fn try_send(
        &self,
        data: &[u8],
    ) -> impl Future<Output = Result<(), SinkError<Self::Error>>> + Send;

    // Provided method
    fn health_check(
        &self,
    ) -> impl Future<Output = Result<(), Self::Error>> + Send { ... }
}
Available on crate feature tiered-sink only.
Expand description

A sink that can receive messages asynchronously.

Implement this trait for your message backend (Kafka, S3, HTTP, etc.) to use with TieredSink.

Async methods return impl Future + Send to ensure compatibility with tokio::spawn.

§Example

use hyperi_rustlib::tiered_sink::{Sink, SinkError};

struct MyKafkaSink {
    producer: KafkaProducer,
}

impl Sink for MyKafkaSink {
    type Error = KafkaError;

    async fn try_send(&self, data: &[u8]) -> Result<(), SinkError<Self::Error>> {
        match self.producer.send(data).await {
            Ok(()) => Ok(()),
            Err(e) if e.is_queue_full() => Err(SinkError::Full),
            Err(e) if e.is_broker_unavailable() => Err(SinkError::Unavailable),
            Err(e) => Err(SinkError::Fatal(e)),
        }
    }
}

Required Associated Types§

Source

type Error: StdError + Send + Sync + 'static

The error type returned by this sink.

Required Methods§

Source

fn try_send( &self, data: &[u8], ) -> impl Future<Output = Result<(), SinkError<Self::Error>>> + Send

Try to send data to the sink.

This should be non-blocking or have a short timeout. Return appropriate SinkError variant based on the failure mode:

  • SinkError::Full - Sink is backpressuring, try again later
  • SinkError::Unavailable - Sink is down, circuit break
  • SinkError::Fatal(e) - Unrecoverable error, don’t spool

Provided Methods§

Source

fn health_check(&self) -> impl Future<Output = Result<(), Self::Error>> + Send

Check if the sink is healthy.

Used by circuit breaker to probe if sink has recovered. Default implementation returns Ok (assumes healthy).

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§