irontide-core 1.0.1

Core types for BitTorrent: hashes, metainfo, magnets, piece arithmetic
Documentation
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

/// M224 D3: RAII counter increment for the global connection cap. Wraps
/// `Arc<AtomicUsize>` so the live count cannot leak even if the listener/admit
/// loop panics or drops a connection mid-pipeline.
/// dropped automatically when the owning connection is dropped or forwarded.
#[derive(Debug)]
pub struct LiveConnectionGuard {
    counter: Arc<AtomicUsize>,
}

impl LiveConnectionGuard {
    /// Create a new guard and increment the backing counter atomically.
    pub fn new(counter: Arc<AtomicUsize>) -> Self {
        counter.fetch_add(1, Ordering::SeqCst);
        Self { counter }
    }
}

impl Drop for LiveConnectionGuard {
    fn drop(&mut self) {
        self.counter.fetch_sub(1, Ordering::SeqCst);
    }
}