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::reclaim
reclamation request