Expand description
Memory quota tracker, core and low-level API
Β§Example
use std::{collections::VecDeque, sync::{Arc, Mutex}};
use tor_rtcompat::{CoarseInstant, CoarseTimeProvider, PreferredRuntime};
use tor_memquota::{mtracker, MemoryQuotaTracker, MemoryReclaimedError, EnabledToken};
use void::{ResultVoidExt, Void};
#[derive(Debug)]
struct TrackingQueue(Mutex<Result<Inner, MemoryReclaimedError>>);
#[derive(Debug)]
struct Inner {
partn: mtracker::Participation,
data: VecDeque<(Box<[u8]>, CoarseInstant)>,
}
impl TrackingQueue {
fn push(&self, now: CoarseInstant, bytes: Box<[u8]>) -> Result<(), MemoryReclaimedError> {
let mut inner = self.0.lock().unwrap();
let inner = inner.as_mut().map_err(|e| e.clone())?;
inner.partn.claim(bytes.len())?;
inner.data.push_back((bytes, now));
Ok(())
}
}
impl mtracker::IsParticipant for TrackingQueue {
fn get_oldest(&self, _: EnabledToken) -> Option<CoarseInstant> {
let inner = self.0.lock().unwrap();
Some(inner.as_ref().ok()?.data.front()?.1)
}
fn reclaim(self: Arc<Self>, _: EnabledToken) -> mtracker::ReclaimFuture {
let mut inner = self.0.lock().unwrap();
*inner = Err(MemoryReclaimedError::new());
Box::pin(async { mtracker::Reclaimed::Collapsing })
}
}
let runtime = PreferredRuntime::create().unwrap();
let config = tor_memquota::Config::builder().max(1024*1024*1024).build().unwrap();
let trk = MemoryQuotaTracker::new(&runtime, config).unwrap();
let account = trk.new_account(None).unwrap();
let queue: Arc<TrackingQueue> = account.register_participant_with(
runtime.now_coarse(),
|partn| {
Ok::<_, Void>((Arc::new(TrackingQueue(Mutex::new(Ok(Inner {
partn,
data: VecDeque::new(),
})))), ()))
},
).unwrap().void_unwrap().0;
queue.push(runtime.now_coarse(), Box::new([0; 24])).unwrap();ModulesΒ§
- bookkeeping π
- Quantity bookkeeping
- reclaim π
- Reclamation algorithm
- total_
qty_ πnotifier TotalQtyNotifier
MacrosΒ§
- find_
in_ πtracker - Given a
&Weak<MemoryQuotaTracker>, find an account and maybe participant - find_
in_ πtracker_ eh - Error handling helper for
find_in_tracker
StructsΒ§
- AId π
- Identifies an Account
- ARecord π
- Account record, within
State.accounts - Account
- Handle onto an Account
- Account
Inner - Contents of an enabled
Account - Global π
- Global parts of
State - Memory
Quota Tracker - Memory data tracker
- PId π
- Identifies a Participant within an Account
- PRecord π
- Participant record, within
ARecord.ps - Participation
- Handle onto a participantβs participation in a tracker
- Participation
Inner - Contents of an enabled
Participation - State π
- Memory tracker inner, including mutable state
- Weak
Account - Weak handle onto an Account
- Weak
Account Inner - Contents of an enabled
WeakAccount
EnumsΒ§
- Reclaimed
- Outcome of
IsParticipant::reclaim
ConstantsΒ§
- MAX_
CACHE π - Maximum amount weβll βcacheβ locally in a
Participation - TARGET_
CACHE_ πCLAIMING - Target cache size when we seem to be claiming
- TARGET_
CACHE_ πRELEASING - Target cache size when we seem to be releasing
TraitsΒ§
- IsParticipant
- Participants provide an impl of the hooks in this trait
Type AliasesΒ§
- Reclaim
Future - Future returned by the
IsParticipant::reclaimreclamation request