use std::time::Duration;
use async_trait::async_trait;
use rskit_errors::{AppError, AppResult, ErrorCode};
use crate::event::Event;
use crate::message::Message;
fn unsupported(capability: &str) -> AppResult<()> {
Err(AppError::new(
ErrorCode::InvalidInput,
format!("messaging capability {capability} is not supported by this consumer"),
))
}
#[async_trait]
pub trait MessageProducer<T: Send + Sync>: Send + Sync {
async fn send(&self, msg: Message<T>) -> AppResult<()>;
async fn send_batch(&self, msgs: Vec<Message<T>>) -> AppResult<()>;
async fn flush(&self, timeout: Duration) -> AppResult<()>;
async fn close(&self) -> AppResult<()> {
Ok(())
}
}
#[async_trait]
pub trait MessageConsumer<T: Send + Sync>: Send + Sync {
async fn subscribe(&self, topics: &[&str]) -> AppResult<()>;
async fn recv(&self, timeout: Duration) -> AppResult<Message<T>>;
async fn close(&self) -> AppResult<()> {
Ok(())
}
async fn pause(&self) -> AppResult<()> {
unsupported("pause")
}
async fn resume(&self) -> AppResult<()> {
unsupported("resume")
}
}
#[async_trait]
pub trait EventProducer: Send + Sync {
async fn publish(&self, topic: &str, event: Event) -> AppResult<()>;
async fn publish_batch(&self, topic: &str, events: Vec<Event>) -> AppResult<()>;
}
#[async_trait]
pub trait EventConsumer: Send + Sync {
async fn subscribe(&self, topics: &[&str]) -> AppResult<()>;
async fn recv_event(&self, timeout: Duration) -> AppResult<Event>;
}
#[async_trait]
pub trait BrokerComponent: Send + Sync {
async fn start(&self) -> AppResult<()>;
async fn stop(&self) -> AppResult<()>;
fn is_healthy(&self) -> bool;
}