use crate::model::Timestamp;
use crate::vtn::notify::Delivery;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OutboxId(pub i64);
impl core::fmt::Display for OutboxId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Queued {
pub id: OutboxId,
pub attempts: u32,
pub delivery: Delivery,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OutboxStats {
pub pending: u64,
pub dead: u64,
pub oldest_pending_seconds: Option<i64>,
}
impl OutboxStats {
pub fn is_idle(&self) -> bool {
self.pending == 0 && self.dead == 0
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeadLetter {
pub id: i64,
pub enqueued_at: Timestamp,
pub attempts: u32,
pub last_error: Option<crate::std_shim::String>,
pub subscription_id: Option<crate::model::ObjectId>,
pub object_type: crate::model::ObjectType,
pub object_id: crate::model::ObjectId,
pub destination: crate::std_shim::String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RetryPolicy {
pub max_attempts: u32,
pub base_delay: core::time::Duration,
pub max_delay: core::time::Duration,
}
impl Default for RetryPolicy {
fn default() -> Self {
Self {
max_attempts: 8,
base_delay: core::time::Duration::from_secs(2),
max_delay: core::time::Duration::from_secs(300),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BreakerPolicy {
pub threshold: u32,
pub cooldown: core::time::Duration,
}
impl Default for BreakerPolicy {
fn default() -> Self {
Self {
threshold: 3,
cooldown: core::time::Duration::from_secs(15 * 60),
}
}
}
impl BreakerPolicy {
pub fn disabled() -> Self {
Self {
threshold: u32::MAX,
cooldown: core::time::Duration::ZERO,
}
}
pub fn trips(&self, consecutive_failures: u32) -> bool {
consecutive_failures >= self.threshold
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SubscriberHealth {
pub subscription_id: crate::model::ObjectId,
pub consecutive_failures: u32,
pub cut_off_since: Option<Timestamp>,
pub retry_at: Option<Timestamp>,
pub last_error: Option<crate::std_shim::String>,
}
impl SubscriberHealth {
pub fn is_cut_off(&self, now: Timestamp) -> bool {
self.retry_at.is_some_and(|at| now < at)
}
}
impl RetryPolicy {
pub fn delay_after(&self, attempts: u32) -> core::time::Duration {
let shift = attempts.min(16);
self.base_delay
.saturating_mul(1u32 << shift)
.min(self.max_delay)
}
pub fn may_retry(&self, attempts: u32) -> bool {
attempts < self.max_attempts
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_delay_grows_and_then_stops_growing() {
let policy = RetryPolicy {
max_attempts: 8,
base_delay: core::time::Duration::from_secs(2),
max_delay: core::time::Duration::from_secs(300),
};
assert_eq!(policy.delay_after(0), core::time::Duration::from_secs(2));
assert_eq!(policy.delay_after(1), core::time::Duration::from_secs(4));
assert_eq!(policy.delay_after(3), core::time::Duration::from_secs(16));
assert_eq!(policy.delay_after(20), core::time::Duration::from_secs(300));
}
#[test]
fn attempts_are_bounded() {
let policy = RetryPolicy::default();
assert!(policy.may_retry(0));
assert!(policy.may_retry(7));
assert!(!policy.may_retry(8));
assert!(!policy.may_retry(100));
}
#[test]
fn an_empty_queue_is_idle() {
assert!(OutboxStats::default().is_idle());
assert!(
!OutboxStats {
pending: 1,
..Default::default()
}
.is_idle()
);
assert!(
!OutboxStats {
dead: 1,
..Default::default()
}
.is_idle()
);
}
}
pub(crate) mod support {
use super::*;
pub(crate) fn next_attempt(
now: Timestamp,
attempts: u32,
policy: &RetryPolicy,
) -> Option<Timestamp> {
if !policy.may_retry(attempts) {
return None;
}
let delay = policy.delay_after(attempts.saturating_sub(1));
let seconds = i64::try_from(delay.as_secs()).unwrap_or(i64::MAX);
now.checked_add(jiff::Span::new().seconds(seconds)).ok()
}
pub(crate) fn age_seconds(now: Timestamp, oldest: Option<Timestamp>) -> Option<i64> {
oldest.map(|t| (now.as_second() - t.as_second()).max(0))
}
#[cfg(any(feature = "sqlite", feature = "postgres"))]
pub(crate) fn subject(
delivery: &Delivery,
) -> (crate::model::ObjectType, crate::model::ObjectId) {
(
delivery.notification.object.object_type(),
delivery.notification.object.id().clone(),
)
}
}