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§
Sourcefn 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 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.
fn as_any(&self) -> &dyn Any
Provided Methods§
Sourcefn on_connect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>
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(())
}))
}
}Sourcefn on_disconnect_hook(&self) -> Option<BoxFuture<'_, Result<()>>>
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.
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,
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".