Skip to main content

arcly_http/messaging/
mod.rs

1//! Event consumer mesh — the inbound half of the event architecture.
2//!
3//! The transactional outbox (`data::outbox`) made *producing* events safe;
4//! this module gives *consuming* them the same framework-grade guarantees,
5//! with NestJS-style ergonomics:
6//!
7//! ```ignore
8//! pub struct OrderEventsConsumer;
9//!
10//! #[EventConsumer]
11//! impl OrderEventsConsumer {
12//!     #[EventPattern("order.confirmed")]
13//!     async fn on_confirmed(ctx: EventContext) -> Result<(), String> {
14//!         let evt: OrderConfirmed = ctx.payload()?;
15//!         ctx.inject::<InventoryService>().reserve(evt.order_id).await
16//!     }
17//! }
18//! ```
19//!
20//! ## Guarantees
21//!
22//! - **Frozen dispatch** — `#[EventPattern]` handlers register through
23//!   `inventory` (the same link-time mechanism as `#[Controller]`); the
24//!   topic→handler map is built once at runtime start and never mutated.
25//! - **At-least-once + dedupe** — when an `IdempotencyStore` is in the DI
26//!   container, each `idempotency_key` is consumed exactly once per TTL.
27//! - **Bounded retries → DLQ** — a failing message is `nack`ed and retried
28//!   up to `max_retries`, then dead-lettered with the failure reason; one
29//!   poison message can never wedge a topic.
30//! - **Trace continuity** — the envelope's `traceparent` (stamped by the
31//!   outbox producer) becomes this hop's parent span; async hops chain in
32//!   the trace UI instead of starting orphan roots.
33//!
34//! The transport (Kafka / AMQP / NATS / in-process bridge) is app-provided
35//! via [`MessageTransport`] — the same boundary rule as every other driver.
36
37pub mod event;
38pub mod runtime;
39
40pub use runtime::ConsumerRuntime;
41
42use futures::future::BoxFuture;
43
44use crate::core::engine::FrozenDiContainer;
45use crate::observability::propagation::TraceContext;
46
47// ─── Envelope ─────────────────────────────────────────────────────────────────
48
49/// One inbound message, transport-agnostic.
50#[derive(Clone, Debug)]
51pub struct InboundMessage {
52    pub topic: String,
53    pub payload: serde_json::Value,
54    /// Consumers dedupe on this under at-least-once delivery.
55    pub idempotency_key: String,
56    pub tenant: Option<String>,
57    /// W3C trace context stamped by the producer (outbox), if any.
58    pub traceparent: Option<String>,
59}
60
61// ─── Transport contract ───────────────────────────────────────────────────────
62
63/// App-provided broker adapter (Kafka / AMQP / NATS / in-process bridge).
64/// Boxed error for infrastructure trait contracts (transports, sinks,
65/// publishers). `String` and `&str` convert via `.into()`, so simple
66/// implementations stay simple while real ones keep their source chain.
67pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
68
69pub trait MessageTransport: Send + Sync + 'static {
70    /// Pull up to `max` messages. An empty Vec means "nothing right now".
71    fn poll(&self, max: usize) -> BoxFuture<'_, Result<Vec<InboundMessage>, BoxError>>;
72    /// Acknowledge successful processing (message will not be redelivered).
73    fn ack<'a>(&'a self, msg: &'a InboundMessage) -> BoxFuture<'a, Result<(), BoxError>>;
74    /// Negative-ack: requeue for a later retry.
75    fn nack<'a>(&'a self, msg: &'a InboundMessage) -> BoxFuture<'a, Result<(), BoxError>>;
76    /// Park a poison message out of band, with the final failure reason.
77    fn dead_letter<'a>(
78        &'a self,
79        msg: &'a InboundMessage,
80        reason: &'a str,
81    ) -> BoxFuture<'a, Result<(), BoxError>>;
82}
83
84// ─── Consumer-side context ────────────────────────────────────────────────────
85
86/// What an `#[EventPattern]` handler receives — DI + payload + trace, no
87/// HTTP types anywhere.
88#[non_exhaustive]
89pub struct EventContext {
90    pub message: InboundMessage,
91    pub container: &'static FrozenDiContainer,
92    /// Continues the producer's trace (or a fresh root when none was carried).
93    pub trace: TraceContext,
94    /// The envelope's tenant, resolved + validated against the same
95    /// `TenantRegistry` as HTTP traffic (suspended tenants never reach
96    /// handlers — their events are dead-lettered upstream).
97    pub tenant: Option<std::sync::Arc<crate::web::tenant::TenantConfig>>,
98}
99
100impl EventContext {
101    /// Resolve a singleton service. O(1), no locks. Panics when absent.
102    #[inline]
103    pub fn inject<T: Send + Sync + 'static>(&self) -> &'static T {
104        self.container.get::<T>()
105    }
106
107    /// Non-panicking variant of [`Self::inject`].
108    #[inline]
109    pub fn try_inject<T: Send + Sync + 'static>(&self) -> Option<&'static T> {
110        self.container.try_get::<T>()
111    }
112
113    /// Deserialize the payload into a typed event.
114    ///
115    /// A malformed payload is permanent by definition, so the error is
116    /// already [`EventError::DeadLetter`] — `ctx.payload()?` does the right
117    /// thing without manual mapping.
118    pub fn payload<T: serde::de::DeserializeOwned>(&self) -> Result<T, EventError> {
119        serde_json::from_value(self.message.payload.clone())
120            .map_err(|e| EventError::DeadLetter(format!("payload decode failed: {e}")))
121    }
122
123    /// `traceparent` for forwarding to the next hop (HTTP call, next queue).
124    pub fn traceparent(&self) -> String {
125        self.trace.to_traceparent()
126    }
127}
128
129// ─── Typed handler errors ─────────────────────────────────────────────────────
130
131/// What the mesh should do with a failed message. Handlers may keep
132/// returning `Result<(), String>` (strings convert to [`EventError::Retry`])
133/// or return `EventError` directly to decide the message's fate:
134///
135/// ```ignore
136/// #[EventPattern("order.confirmed")]
137/// async fn on_confirmed(ctx: EventContext) -> Result<(), EventError> {
138///     let order: Order = ctx
139///         .payload()
140///         .map_err(EventError::DeadLetter)?; // malformed: retrying won't help
141///     warehouse.reserve(&order).await
142///         .map_err(|e| EventError::Retry(e.to_string()))      // transient
143/// }
144/// ```
145#[derive(Debug)]
146pub enum EventError {
147    /// Transient failure (downstream 5xx, lock contention): nack → bounded
148    /// retries → dead-letter at `max_retries`.
149    Retry(String),
150    /// Permanent failure (malformed payload, violated business invariant):
151    /// dead-letter **immediately** — burning the retry budget on a poison
152    /// message only delays the alert.
153    DeadLetter(String),
154}
155
156impl From<String> for EventError {
157    fn from(s: String) -> Self {
158        Self::Retry(s)
159    }
160}
161impl From<&str> for EventError {
162    fn from(s: &str) -> Self {
163        Self::Retry(s.to_owned())
164    }
165}
166impl std::fmt::Display for EventError {
167    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
168        match self {
169            Self::Retry(m) => write!(f, "retryable: {m}"),
170            Self::DeadLetter(m) => write!(f, "poison: {m}"),
171        }
172    }
173}
174impl std::error::Error for EventError {}
175
176// ─── Handler registration (filled in by #[EventConsumer]) ────────────────────
177
178/// Static descriptor emitted by the `#[EventPattern]` expansion and collected
179/// at link time — the runtime freezes these into its dispatch map at start.
180pub struct EventHandlerDescriptor {
181    pub topic: &'static str,
182    pub consumer: &'static str,
183    pub handler: fn(EventContext) -> BoxFuture<'static, Result<(), EventError>>,
184}
185
186inventory::collect!(&'static EventHandlerDescriptor);