node_device_session/webrtc/
budget.rs1use std::sync::Arc;
3use tokio::sync::{OwnedSemaphorePermit, Semaphore};
4
5pub const SESSION_BYTES: usize = 24 * 1024 * 1024;
7
8#[derive(Clone)]
10pub struct ByteBudget(Arc<Semaphore>);
11
12impl Default for ByteBudget {
13 fn default() -> Self {
14 Self(Arc::new(Semaphore::new(SESSION_BYTES)))
15 }
16}
17
18impl ByteBudget {
19 pub fn reserve(&self, bytes: usize) -> Option<OwnedSemaphorePermit> {
21 self.0
22 .clone()
23 .try_acquire_many_owned(bytes.try_into().ok()?)
24 .ok()
25 }
26}