torrentdht 0.1.1

DHT discovery with mTLS handshake handoff; optional BitTorrent when the host opts in
Documentation
//! Coalescing flush queue: at most one pending job per block key.
//!
//! An unbounded mpsc of `Bytes` grows without bound under rewrite pressure even
//! when stale disk writes are skipped. This queue replaces superseded payloads
//! in place so RAM tracks dirty keys, not rewrite rate.

use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use bytes::Bytes;
use tokio::sync::{Mutex, Notify};

use crate::types::BlockKey;

#[derive(Debug, Clone)]
pub struct FlushJob {
    pub key: BlockKey,
    pub absolute_offset: u64,
    pub data: Bytes,
    pub crc32c: u32,
}

pub struct FlushCoalescer {
    pending: Mutex<HashMap<BlockKey, FlushJob>>,
    inflight: Mutex<HashSet<BlockKey>>,
    notify: Notify,
    closed: AtomicBool,
}

impl FlushCoalescer {
    pub fn new() -> Arc<Self> {
        Arc::new(Self {
            pending: Mutex::new(HashMap::new()),
            inflight: Mutex::new(HashSet::new()),
            notify: Notify::new(),
            closed: AtomicBool::new(false),
        })
    }

    /// Submit or replace the pending job for this block (drops older bytes).
    pub async fn submit(&self, job: FlushJob) {
        {
            let mut pending = self.pending.lock().await;
            pending.insert(job.key, job);
        }
        self.notify.notify_waiters();
    }

    pub fn close(&self) {
        self.closed.store(true, Ordering::SeqCst);
        self.notify.notify_waiters();
    }

    pub async fn is_idle(&self) -> bool {
        self.pending.lock().await.is_empty() && self.inflight.lock().await.is_empty()
    }

    /// Take up to `max` jobs. Returns `None` when closed and fully drained.
    pub async fn take_batch(&self, max: usize) -> Option<Vec<FlushJob>> {
        let max = max.max(1);
        loop {
            {
                let mut pending = self.pending.lock().await;
                let mut inflight = self.inflight.lock().await;
                if !pending.is_empty() {
                    let mut batch = Vec::with_capacity(max.min(pending.len()));
                    let keys: Vec<BlockKey> = pending.keys().copied().take(max).collect();
                    for key in keys {
                        if let Some(job) = pending.remove(&key) {
                            inflight.insert(key);
                            batch.push(job);
                        }
                    }
                    return Some(batch);
                }
                if self.closed.load(Ordering::SeqCst) && inflight.is_empty() {
                    return None;
                }
            }
            self.notify.notified().await;
        }
    }

    /// After a successful disk write: clear inflight; return whether RAM may drop the block.
    ///
    /// Returns false when a newer job was queued while this write was in flight.
    pub async fn complete_ok(&self, key: BlockKey) -> bool {
        self.inflight.lock().await.remove(&key);
        let clear_memory = !self.pending.lock().await.contains_key(&key);
        self.notify.notify_waiters();
        clear_memory
    }

    /// After a failed disk write: clear inflight and re-queue so flush_all can retry.
    pub async fn complete_err_requeue(&self, job: FlushJob) {
        self.inflight.lock().await.remove(&job.key);
        {
            let mut pending = self.pending.lock().await;
            pending.entry(job.key).or_insert(job);
        }
        self.notify.notify_waiters();
    }

    /// Drop inflight without requeue (corrupt payload that must not retry forever).
    pub async fn complete_err_drop(&self, key: BlockKey) {
        self.inflight.lock().await.remove(&key);
        self.notify.notify_waiters();
    }
}