Module mtracker

Source
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
AccountInner
Contents of an enabled Account
Global 🔒
Global parts of State
MemoryQuotaTracker
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
ParticipationInner
Contents of an enabled Participation
State 🔒
Memory tracker inner, including mutable state
WeakAccount
Weak handle onto an Account
WeakAccountInner
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§

ReclaimFuture
Future returned by the IsParticipant::reclaim reclamation request