haematite 0.6.1

Content-addressed, branchable, actor-native storage engine
Documentation
use std::sync::atomic::{AtomicU64, Ordering};

use super::CarrierAction;

/// Atomic carrier counters used to prove disconnected quiescence.
#[derive(Debug, Default)]
pub struct CarrierCounters {
    armed_timer_gauge: AtomicU64,
    timer_wakes: AtomicU64,
    connect_attempts: AtomicU64,
    frames_sent: AtomicU64,
}

impl CarrierCounters {
    /// Takes one consistent-purpose observation of all carrier counters.
    pub fn snapshot(&self) -> CarrierCounterSnapshot {
        CarrierCounterSnapshot {
            armed_timer_gauge: self.armed_timer_gauge.load(Ordering::Relaxed),
            timer_wakes: self.timer_wakes.load(Ordering::Relaxed),
            connect_attempts: self.connect_attempts.load(Ordering::Relaxed),
            frames_sent: self.frames_sent.load(Ordering::Relaxed),
        }
    }

    /// Records actions at the single execution boundary.
    pub fn record_actions(&self, actions: &[CarrierAction]) {
        for action in actions {
            match action {
                CarrierAction::StartAttempt { generation: _ } => {
                    self.connect_attempts.fetch_add(1, Ordering::Relaxed);
                }
                CarrierAction::EmitEvent(_)
                | CarrierAction::CancelAttempt { generation: _ }
                | CarrierAction::CloseSocket { key: _, cause: _ } => {}
            }
        }
    }

    pub(crate) fn record_frame_sent(&self) {
        self.frames_sent.fetch_add(1, Ordering::Relaxed);
    }
}

/// Observed values of all carrier quiescence counters.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct CarrierCounterSnapshot {
    /// Number of armed carrier deadlines; this core always reports zero.
    pub armed_timer_gauge: u64,
    /// Number of carrier deadline wakes; this core always reports zero.
    pub timer_wakes: u64,
    /// Number of connection attempts started by qualifying events.
    pub connect_attempts: u64,
    /// Number of frames successfully handed off by the bounded queue.
    pub frames_sent: u64,
}