How often one visitor may do one thing
A site that lets visitors write (vote, comment, ask) has to stop one person doing it a thousand times a minute. The rule here is the plainest one: for each kind of request and each visitor, count the requests in a fixed window of a minute, and refuse the ones past a limit.
The archive object is the one place every write of a site passes, and there is one of it, so it can count for itself, with no help from anywhere else. Nothing is written to storage: who a visitor is (their address) is a key here for a minute and is gone, and a count lost when the object restarts only lets someone start again.
This is the same idea as Visits in lmjtfy's budget package, and it
lives here so every site's archive has it.
17use std::collections::HashMap;
How long a count runs: a minute, from the first request counted.
20const WINDOW_MS: f64 = 60_000.0;
The counts of the minute
The key is a pair, the kind of request and who made it, because voting and commenting each have a limit of their own. The value is when that visitor's minute began and how many requests they have made in it. It is in memory only; see the top of the file for why.
31impl Meter {
Counts one request of kind what by who, and says whether it is within per_minute. The
minute starts at the first one counted. A request over the limit is not counted, so hammering
does not push the end of the wait further off.
Three things happen, in order. Everyone whose minute has ended is forgotten, which is the only tidying the table gets. This visitor's entry is found, or made with a minute starting now. Then the count is checked against the limit, and only a request that passes adds to it. A limit of 0 admits nothing.
41 pub fn admit(&mut self, what: &str, who: &str, now_ms: f64, per_minute: u32) -> bool { 42 // Whoever's minute is over is forgotten. 43 self.0.retain(|_, (started, _)| now_ms - *started < WINDOW_MS); 44 let (_, count) = self.0.entry((what.to_owned(), who.to_owned())).or_insert((now_ms, 0)); 45 if *count >= per_minute { 46 return false; 47 } 48 *count += 1; 49 true 50 }
How many (what, who) are being counted right now. The tests use it to see that old counts are forgotten.
The tests
The window, the separation of visitors and kinds, the limit of nothing,
and the forgetting, with times given in milliseconds so a minute is
60_000. Run them with cargo test -p aldebaran-worker --features archive.
Five requests a minute: the sixth is refused, and the minute ends 60 seconds after the first request, not after the last refusal.
76 #[test] 77 fn a_visitor_is_stopped_at_the_limit_and_starts_again_after_a_minute() { 78 let mut meter = Meter::default(); 79 for n in 0..5 { 80 assert!(meter.admit("comment", "a", f64::from(n) * 1000.0, 5), "{n}"); 81 } 82 assert!(!meter.admit("comment", "a", 5_000.0, 5)); 83 assert!(!meter.admit("comment", "a", 59_999.0, 5)); 84 // A minute after the first one, not after the last refusal. 85 assert!(meter.admit("comment", "a", 60_000.0, 5)); 86 }
One visitor's limit is not another's, and one kind of request's limit is not another kind's.
90 #[test] 91 fn each_visitor_and_each_kind_of_request_is_counted_apart() { 92 let mut meter = Meter::default(); 93 assert!(meter.admit("rate", "a", 0.0, 1)); 94 assert!(!meter.admit("rate", "a", 1.0, 1)); 95 assert!(meter.admit("rate", "b", 1.0, 1)); 96 assert!(meter.admit("comment", "a", 1.0, 1)); 97 }
A limit of nothing lets nothing through.
The table does not grow without end: each call forgets everyone whose minute is over, so it holds only the visitors of the last minute.
109 #[test] 110 fn nobody_is_remembered_past_their_minute() { 111 let mut meter = Meter::default(); 112 assert!(meter.is_empty()); 113 meter.admit("rate", "a", 0.0, 10); 114 meter.admit("rate", "b", 30_000.0, 10); 115 assert_eq!(meter.len(), 2); 116 meter.admit("rate", "c", 61_000.0, 10); 117 assert_eq!(meter.len(), 2); 118 meter.admit("rate", "d", 95_000.0, 10); 119 assert_eq!(meter.len(), 2); 120 } 121}