Skip to main content

liminal_sdk/
pressure.rs

1use alloc::string::String;
2use core::time::Duration;
3
4/// Application-visible backpressure result for a publish operation.
5///
6/// Pressure responses are part of the successful publish path so applications can
7/// react to delivery, buffering, and shedding decisions without those signals
8/// being hidden inside transport-specific errors.
9#[derive(Clone, Debug, PartialEq, Eq)]
10pub enum PressureResponse {
11    /// The message was accepted and delivered to the consumer.
12    Accept,
13    /// The message was accepted into a buffer and will be delivered later.
14    Defer {
15        /// Estimated delay before the buffered message can be delivered.
16        delay: Duration,
17    },
18    /// The consumer was overwhelmed and the message was shed.
19    Reject {
20        /// Human-readable reason describing why the message was shed.
21        reason: String,
22    },
23}
24
25/// Genuine delivery ack for a publish, distinct from the backpressure signal.
26///
27/// [`PressureResponse`] reports the bus's admission decision (accepted / buffered
28/// / shed); a `DeliveryAck` reports whether the message was actually received by a
29/// subscriber. A caller that must only treat a send as done when a worker genuinely
30/// accepted it (for example the aion outbox) inspects [`DeliveryAck::is_accepted`]:
31/// `true` means at least one subscriber received the message, `false` means the
32/// publish succeeded but reached no subscriber (an empty channel, or a duplicate
33/// suppressed by dedup-on-delivery).
34#[derive(Clone, Debug, PartialEq, Eq)]
35pub struct DeliveryAck {
36    pressure: PressureResponse,
37    accepted: bool,
38}
39
40impl DeliveryAck {
41    /// Builds a delivery ack from the backpressure response and acceptance flag.
42    #[must_use]
43    pub const fn new(pressure: PressureResponse, accepted: bool) -> Self {
44        Self { pressure, accepted }
45    }
46
47    /// Whether the message was genuinely accepted by at least one subscriber.
48    #[must_use]
49    pub const fn is_accepted(&self) -> bool {
50        self.accepted
51    }
52
53    /// The backpressure response the bus returned for this publish.
54    #[must_use]
55    pub const fn pressure(&self) -> &PressureResponse {
56        &self.pressure
57    }
58}