Skip to main content

menger_notes/
quota.rs

1//! **The meter's seam** — every byte a note's body grows by is
2//! reserved before it is written and committed once it is (V2-31: the
3//! quota service will meter `notes.bytes` per author; EDITOR-10 does the
4//! same for images). Until that service lands, [`AllowAll`] allows every
5//! write; the call sites are [`crate::crdt`]'s create and append.
6//! V2-31: the quota service answers it (`src/quota_allowance.rs`'s
7//! `NotesMetered`): `notes.bytes` per change, `notes.count` per note made.
8
9/// A reservation the meter made, handed back at commit.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct Reserved {
12    /// Whose bytes: the writer's principal.
13    pub author: String,
14    /// How many.
15    pub bytes: u64,
16    /// V2-31: the quota service's hold, to commit (none: nothing held).
17    pub hold: Option<String>,
18}
19
20/// V2-39: a refusal with its reason — `over_allowance`, `box_full`,
21/// `too_many_at_once` — and its words with the numbers.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct Refused {
24    pub reason: String,
25    pub message: String,
26}
27
28/// What meters the notes service's writes.
29pub trait Quota: Send + Sync {
30    /// Reserve `bytes` of `notes.bytes` for `author`; `Err` refuses the
31    /// write, in words, and nothing is written.
32    fn reserve(&self, author: &str, bytes: u64) -> Result<Reserved, String>;
33    /// The write the reservation was for is in the file.
34    fn commit(&self, reserved: Reserved);
35    /// V2-31: one note made (`notes.count`) for `author`; refused like
36    /// `reserve`. Every one allowed unless the meter says.
37    fn reserve_count(&self, author: &str) -> Result<Reserved, String> {
38        Ok(Reserved { author: author.to_string(), bytes: 0, hold: None })
39    }
40    /// V2-31: a squash moved what `author` keeps by `delta` bytes — less
41    /// (a credit: the log shrank) or, rarely, more (their share of the
42    /// snapshot past what was folded). Never refused: the squash is done.
43    fn moved(&self, _author: &str, _delta: i64) {}
44    /// V2-39: one question asked (`questions.count`) for `author`, before
45    /// it is kept; refused with its reason. Every one allowed unless the
46    /// meter says.
47    fn reserve_question(&self, author: &str) -> Result<Reserved, Refused> {
48        Ok(Reserved { author: author.to_string(), bytes: 0, hold: None })
49    }
50    /// V2-39: the write the reservation was for was refused: nothing counts.
51    fn release(&self, _reserved: Reserved) {}
52}
53
54/// Every write allowed — the meter until V2-31's.
55#[derive(Debug, Clone, Copy, Default)]
56pub struct AllowAll;
57
58impl Quota for AllowAll {
59    fn reserve(&self, author: &str, bytes: u64) -> Result<Reserved, String> {
60        Ok(Reserved { author: author.to_string(), bytes, hold: None })
61    }
62    fn commit(&self, _reserved: Reserved) {}
63}