Skip to main content

MessagePublisher

Trait MessagePublisher 

Source
pub trait MessagePublisher:
    Send
    + Sync
    + 'static {
    // Required methods
    fn send_batch<'life0, 'async_trait>(
        &'life0 self,
        messages: Vec<CanonicalMessage>,
    ) -> Pin<Box<dyn Future<Output = Result<SentBatch, PublisherError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn as_any(&self) -> &dyn Any;

    // Provided methods
    fn on_connect_hook(&self) -> Option<BoxFuture<'_, Result<()>>> { ... }
    fn on_disconnect_hook(&self) -> Option<BoxFuture<'_, Result<()>>> { ... }
    fn send<'life0, 'async_trait>(
        &'life0 self,
        message: CanonicalMessage,
    ) -> Pin<Box<dyn Future<Output = Result<Sent, PublisherError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn flush<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn status<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = EndpointStatus> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}

Required Methods§

Source

fn send_batch<'life0, 'async_trait>( &'life0 self, messages: Vec<CanonicalMessage>, ) -> Pin<Box<dyn Future<Output = Result<SentBatch, PublisherError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Sends a batch of messages.

This method must be implemented by all publishers. If in doubt, implement send_batch to send messages one at a time.

Source

fn as_any(&self) -> &dyn Any

Provided Methods§

Source

fn on_connect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>

Returns an optional lifecycle hook that runs once after the publisher connection is created.

The route awaits this hook before it reports itself as ready. Returning an error fails route startup and lets the outer route runner reconnect or surface the startup failure.

Use this for per-connection setup that should be shared by all messages published through this publisher, such as loading an embedding model, warming a connection pool, creating SQLite tables or indexes, setting up a Kafka producer transaction context, or authenticating a RabbitMQ channel.

struct SqliteEmbeddingPublisher {
    model: Arc<tokio::sync::Mutex<Option<EmbeddingModel>>>,
    db: sqlx::SqlitePool,
}

impl MessagePublisher for SqliteEmbeddingPublisher {
    fn on_connect_hook(&self) -> Option<BoxFuture<'_, anyhow::Result<()>>> {
        Some(Box::pin(async move {
            let mut model = self.model.lock().await;
            if model.is_none() {
                *model = Some(EmbeddingModel::load("all-MiniLM-L6-v2").await?);
            }
            sqlx::query("CREATE INDEX IF NOT EXISTS idx_embeddings_id ON embeddings(id)")
                .execute(&self.db)
                .await?;
            Ok(())
        }))
    }
}
Source

fn on_disconnect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>

Returns an optional lifecycle hook that runs before the publisher is dropped.

The route awaits this hook during shutdown or reconnect cleanup. Errors are logged as warnings and do not replace the route’s original result.

Source

fn send<'life0, 'async_trait>( &'life0 self, message: CanonicalMessage, ) -> Pin<Box<dyn Future<Output = Result<Sent, PublisherError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source

fn flush<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source

fn status<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = EndpointStatus> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Dyn Compatibility§

This trait is dyn compatible.

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

Implementations on Foreign Types§

Source§

impl<T: MessagePublisher + ?Sized> MessagePublisher for Arc<T>

Source§

fn on_connect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>

Source§

fn on_disconnect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>

Source§

fn send<'life0, 'async_trait>( &'life0 self, message: CanonicalMessage, ) -> Pin<Box<dyn Future<Output = Result<Sent, PublisherError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn send_batch<'life0, 'async_trait>( &'life0 self, messages: Vec<CanonicalMessage>, ) -> Pin<Box<dyn Future<Output = Result<SentBatch, PublisherError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn flush<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn status<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = EndpointStatus> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn as_any(&self) -> &dyn Any

Source§

impl<T: MessagePublisher + ?Sized> MessagePublisher for Box<T>

Source§

fn on_connect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>

Source§

fn on_disconnect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>

Source§

fn send<'life0, 'async_trait>( &'life0 self, message: CanonicalMessage, ) -> Pin<Box<dyn Future<Output = Result<Sent, PublisherError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn send_batch<'life0, 'async_trait>( &'life0 self, messages: Vec<CanonicalMessage>, ) -> Pin<Box<dyn Future<Output = Result<SentBatch, PublisherError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn flush<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn status<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = EndpointStatus> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

fn as_any(&self) -> &dyn Any

Implementors§