node-device-session 7.7.11

Shared server for /econ-v1/device-session/1 behind transport-neutral traits (C-5)
Documentation
//! One allocation budget shared by WebRTC DataChannel queues and HTTP bodies.
use std::sync::Arc;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};

/// Soft cap on in-flight session bytes (DC queues + HTTP request decode).
pub const SESSION_BYTES: usize = 24 * 1024 * 1024;

/// Shared byte budget for one WebRTC answerer session.
#[derive(Clone)]
pub struct ByteBudget(Arc<Semaphore>);

impl Default for ByteBudget {
    fn default() -> Self {
        Self(Arc::new(Semaphore::new(SESSION_BYTES)))
    }
}

impl ByteBudget {
    /// Reserve `bytes` from the session budget, or `None` when exhausted.
    pub fn reserve(&self, bytes: usize) -> Option<OwnedSemaphorePermit> {
        self.0
            .clone()
            .try_acquire_many_owned(bytes.try_into().ok()?)
            .ok()
    }
}