reliar-store-postgres 0.8.0

PostgreSQL provider for the Reliar transactional outbox and inbox: migrations, migrate(), enqueue and the SKIP LOCKED claim.
Documentation
#![allow(dead_code)]
//! `CountingMetrics` โ€” a harness-local [`OutboxMetrics`] stimulus (ADR 0043 A.5) for the one trial
//! in this binary that observes the hook (`zero_stats_interval_never_ticks`). Counting calls made
//! *to* it is stimulus bookkeeping about the code under test (ADR 0043 ยง2's explicit carve-out),
//! never a stand-in for stored state โ€” the mixed-batch metrics property itself moved to
//! `tests/system`'s `e16` against a real broker.

use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::Duration;

use reliar_outbox::OutboxMetrics;

#[derive(Debug, Default)]
struct Inner {
    pending: Option<u64>,

    expired_pending: Option<u64>,

    oldest_pending_age: Option<Duration>,
}

/// Every unimplemented [`OutboxMetrics`] hook keeps the trait's no-op default โ€” only the three
/// stats-tick gauges this binary's one metrics trial needs are recorded.
#[derive(Clone, Debug, Default)]
pub(crate) struct CountingMetrics {
    inner: Arc<Mutex<Inner>>,
}

impl CountingMetrics {
    fn lock(&self) -> MutexGuard<'_, Inner> {
        self.inner.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// The last observed pending count, if [`OutboxMetrics::pending`] was ever called.
    pub(crate) fn pending(&self) -> Option<u64> {
        self.lock().pending
    }

    /// The last observed expired-pending count, if [`OutboxMetrics::expired_pending`] was ever
    /// called.
    pub(crate) fn expired_pending(&self) -> Option<u64> {
        self.lock().expired_pending
    }

    /// The last observed outbox lag, if [`OutboxMetrics::oldest_pending_age`] was ever called.
    pub(crate) fn oldest_pending_age(&self) -> Option<Duration> {
        self.lock().oldest_pending_age
    }
}

impl OutboxMetrics for CountingMetrics {
    fn pending(&self, n: u64) {
        self.lock().pending = Some(n);
    }

    fn expired_pending(&self, n: u64) {
        self.lock().expired_pending = Some(n);
    }

    fn oldest_pending_age(&self, age: Duration) {
        self.lock().oldest_pending_age = Some(age);
    }
}