rskit_messaging/traits.rs
1use std::time::Duration;
2
3use async_trait::async_trait;
4use rskit_errors::{AppError, AppResult, ErrorCode};
5
6use crate::event::Event;
7use crate::message::Message;
8
9fn unsupported(capability: &str) -> AppResult<()> {
10 Err(AppError::new(
11 ErrorCode::InvalidInput,
12 format!("messaging capability {capability} is not supported by this consumer"),
13 ))
14}
15
16// ── Messaging traits ─────────────────────────────────────────────────────────
17
18/// A producer that sends messages to a broker.
19#[async_trait]
20pub trait MessageProducer<T: Send + Sync>: Send + Sync {
21 /// Send a single message.
22 async fn send(&self, msg: Message<T>) -> AppResult<()>;
23
24 /// Send a batch of messages.
25 async fn send_batch(&self, msgs: Vec<Message<T>>) -> AppResult<()>;
26
27 /// Flush pending messages within the given timeout.
28 async fn flush(&self, timeout: Duration) -> AppResult<()>;
29
30 /// Close or shut down the producer. Implementations with no persistent resources may no-op.
31 async fn close(&self) -> AppResult<()> {
32 Ok(())
33 }
34}
35
36/// A consumer that receives messages from a broker.
37#[async_trait]
38pub trait MessageConsumer<T: Send + Sync>: Send + Sync {
39 /// Subscribe to one or more topics.
40 async fn subscribe(&self, topics: &[&str]) -> AppResult<()>;
41
42 /// Receive the next message. Blocks until a message is available.
43 async fn recv(&self) -> AppResult<Message<T>>;
44
45 /// Close or shut down the consumer. Implementations with no persistent resources may no-op.
46 async fn close(&self) -> AppResult<()> {
47 Ok(())
48 }
49
50 /// Pause message delivery when the adapter supports it.
51 async fn pause(&self) -> AppResult<()> {
52 unsupported("pause")
53 }
54
55 /// Resume paused message delivery when the adapter supports it.
56 async fn resume(&self) -> AppResult<()> {
57 unsupported("resume")
58 }
59}
60
61/// A producer that publishes structured [`Event`]s to topics.
62#[async_trait]
63pub trait EventProducer: Send + Sync {
64 /// Publish a single event to the given topic.
65 async fn publish(&self, topic: &str, event: Event) -> AppResult<()>;
66
67 /// Publish a batch of events to the given topic.
68 async fn publish_batch(&self, topic: &str, events: Vec<Event>) -> AppResult<()>;
69}
70
71/// A consumer that receives structured [`Event`]s from topics.
72#[async_trait]
73pub trait EventConsumer: Send + Sync {
74 /// Subscribe to one or more topics.
75 async fn subscribe(&self, topics: &[&str]) -> AppResult<()>;
76
77 /// Receive the next event. Blocks until an event is available.
78 async fn recv_event(&self) -> AppResult<Event>;
79}
80
81// ── Broker lifecycle trait ───────────────────────────────────────────────────
82
83/// Lifecycle management for a message-broker connection.
84///
85/// This is intentionally simpler than the bootstrap `Component` trait — it
86/// captures the start / stop / health contract that every broker adapter
87/// needs without pulling in the full component registry. Implementations
88/// can bridge to the bootstrap `Component` trait where needed.
89#[async_trait]
90pub trait BrokerComponent: Send + Sync {
91 /// Establish the broker connection and perform any setup.
92 async fn start(&self) -> AppResult<()>;
93 /// Gracefully disconnect from the broker.
94 async fn stop(&self) -> AppResult<()>;
95 /// Instant health check — returns `true` when the broker connection is
96 /// usable.
97 fn is_healthy(&self) -> bool;
98}