1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::prelude::v1::*;
use crate::nanos::Nanos;
use crate::state::StateStore;
use crate::{clock, NegativeMultiDecision, Quota};
use std::num::NonZeroU32;
use std::time::Duration;
use std::{cmp, fmt};
#[cfg(feature = "std")]
use crate::Jitter;
#[derive(Debug, PartialEq)]
pub struct NotUntil<'a, P: clock::Reference> {
limiter: &'a Gcra,
tat: Nanos,
start: P,
}
impl<'a, P: clock::Reference> NotUntil<'a, P> {
pub fn earliest_possible(&self) -> P {
let tat: Nanos = self.tat;
self.start + tat
}
pub fn wait_time_from(&self, from: P) -> Duration {
let earliest = self.earliest_possible();
earliest.duration_since(earliest.min(from)).into()
}
#[cfg(feature = "std")]
pub(crate) fn earliest_possible_with_offset(&self, jitter: Jitter) -> P {
let tat = jitter + self.tat;
self.start + tat
}
#[cfg(feature = "std")]
pub(crate) fn wait_time_with_offset(&self, from: P, jitter: Jitter) -> Duration {
let earliest = self.earliest_possible_with_offset(jitter);
earliest.duration_since(earliest.min(from)).into()
}
}
impl<'a, P: clock::Reference> fmt::Display for NotUntil<'a, P> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "rate-limited until {:?}", self.start + self.tat)
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct Gcra {
t: Nanos,
tau: Nanos,
}
impl Gcra {
pub(crate) fn new(quota: Quota) -> Self {
let tau: Nanos = (quota.replenish_1_per * quota.max_burst.get()).into();
let t: Nanos = quota.replenish_1_per.into();
Gcra { tau, t }
}
fn starting_state(&self, t0: Nanos) -> Nanos {
t0 + self.t
}
pub(crate) fn test_and_update<K, P: clock::Reference>(
&self,
start: P,
key: &K,
state: &impl StateStore<Key = K>,
t0: P,
) -> Result<(), NotUntil<P>> {
let t0 = t0.duration_since(start);
let tau = self.tau;
let t = self.t;
state.measure_and_replace(key, |tat| {
let tat = tat.unwrap_or_else(|| self.starting_state(t0));
let earliest_time = tat.saturating_sub(tau);
if t0 < earliest_time {
Err(NotUntil {
limiter: self,
tat: earliest_time,
start,
})
} else {
Ok(((), cmp::max(tat, t0) + t))
}
})
}
pub(crate) fn test_n_all_and_update<K, P: clock::Reference>(
&self,
start: P,
key: &K,
n: NonZeroU32,
state: &impl StateStore<Key = K>,
t0: P,
) -> Result<(), NegativeMultiDecision<NotUntil<P>>> {
let t0 = t0.duration_since(start);
let tau = self.tau;
let t = self.t;
let additional_weight = t * (n.get() - 1) as u64;
if additional_weight + t > tau {
return Err(NegativeMultiDecision::InsufficientCapacity(
(tau.as_u64() / t.as_u64()) as u32,
));
}
state.measure_and_replace(key, |tat| {
let tat = tat.unwrap_or_else(|| self.starting_state(t0));
let earliest_time = (tat + additional_weight).saturating_sub(tau);
if t0 < earliest_time {
Err(NegativeMultiDecision::BatchNonConforming(
n.get(),
NotUntil {
limiter: self,
tat: earliest_time,
start,
},
))
} else {
Ok(((), cmp::max(tat, t0) + t + additional_weight))
}
})
}
}