gcra 0.6.0

A basic implementation of GCRA algorithm for rate limiting
Documentation
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use std::time::Instant;
use thiserror::Error;

use crate::rate_limit::RateLimit;

#[derive(Error, Debug, PartialEq, Eq)]
pub enum GcraError {
    /// Cost of the increment exceeds the rate limit and  will never succeed
    #[error("Cost of the increment ({cost}) exceeds the rate limit ({rate_limit:?}) and will never succeed)")]
    DeniedIndefinitely { cost: u32, rate_limit: RateLimit },
    /// Limited request until after the [Instant]
    #[error("Denied until {next_allowed_at:?}")]
    DeniedUntil { next_allowed_at: Instant },
}

/// Holds the minmum amount of state necessary to implement a GRCA leaky buckets.
/// Refer to: [understanding GCRA](https://blog.ian.stapletoncordas.co/2018/12/understanding-generic-cell-rate-limiting.html)
#[derive(Default, Debug, Clone, PartialEq, Eq, Hash, Copy)]
pub struct GcraState {
    /// GCRA's Theoretical Arrival Time (**TAT**)
    /// An unset value signals a new state
    pub tat: Option<Instant>,
}

impl GcraState {
    /// Check if we are allowed to proceed. If so updated our internal state and return true.
    ///
    /// Simply passes the current Instant to [`check_and_modify_at()`]
    #[inline]
    pub fn check_and_modify(&mut self, rate_limit: &RateLimit, cost: u32) -> Result<(), GcraError> {
        let arrived_at = Instant::now();
        self.check_and_modify_at(rate_limit, arrived_at, cost)
    }

    /// Check if we are allowed to proceed at the given arrival time.
    /// If so updated our internal state and return true.
    /// Explaination of GCRA can be found [here](https://blog.ian.stapletoncordas.co/2018/12/understanding-generic-cell-rate-limiting.html)
    ///
    /// # Returns
    /// If denied, will return an [Result::Err] where the value is the next allowed timestamp.
    pub fn check_and_modify_at(
        &mut self,
        rate_limit: &RateLimit,
        arrived_at: Instant,
        cost: u32,
    ) -> Result<(), GcraError> {
        let increment_interval = rate_limit.increment_interval(cost);

        let compute_tat = |new_tat: Instant| {
            if increment_interval > rate_limit.period {
                return Err(GcraError::DeniedIndefinitely {
                    cost,
                    rate_limit: rate_limit.clone(),
                });
            }

            Ok(new_tat + increment_interval)
        };

        let tat = match self.tat {
            Some(tat) => tat,
            None => {
                // First ever request. Allow passage and update self.
                self.tat = Some(compute_tat(arrived_at)?);
                return Ok(());
            }
        };

        // We had a previous request
        if tat < arrived_at {
            // prev request was really old
            let new_tat = std::cmp::max(tat, arrived_at);
            self.tat = Some(compute_tat(new_tat)?);
            Ok(())
        } else {
            // prev request was recent and there's a possibility that we've reached the limit
            let delay_variation_tolerance = rate_limit.period;
            let new_tat = compute_tat(tat)?;

            let next_allowed_at = new_tat - delay_variation_tolerance;
            if next_allowed_at <= arrived_at {
                self.tat = Some(new_tat);
                Ok(())
            } else {
                // Denied, must wait until next_allowed_at
                Err(GcraError::DeniedUntil { next_allowed_at })
            }
        }
    }

    /// Reverts rate_limit by cost, and updated our internal state.
    ///
    /// Simply passes the current Instant to [`revert_at()`]
    #[inline]
    pub fn revert(&mut self, rate_limit: &RateLimit, cost: u32) -> Result<(), GcraError> {
        let arrived_at = Instant::now();
        self.revert_at(rate_limit, arrived_at, cost)
    }

    /// Reverts rate_limit by cost, and updated our internal state.
    ///
    /// This is a hack that substracts the incremental cost from the TAT.
    pub fn revert_at(
        &mut self,
        rate_limit: &RateLimit,
        arrived_at: Instant,
        cost: u32,
    ) -> Result<(), GcraError> {
        let increment_interval = rate_limit.increment_interval(cost);

        let compute_revert_tat = |new_tat: Instant| new_tat - increment_interval;

        let tat = match self.tat {
            Some(tat) => tat,
            None => {
                // First ever request. Nothing to do.
                return Ok(());
            }
        };

        // We had a previous request
        if tat < arrived_at {
            // Reset state: prev request was really old
            self.tat = None;
        } else {
            // prev request was recent
            self.tat = Some(compute_revert_tat(tat));
        }
        Ok(())
    }

    /// Get the remaing resources that we have available for the guard at the instant provided.
    pub fn remaining_resources(&self, rate_limit: &RateLimit, now: Instant) -> u32 {
        if rate_limit.period.is_zero() {
            return 0;
        }

        let time_to_tat = match self.tat.and_then(|tat| tat.checked_duration_since(now)) {
            Some(duration_until) => duration_until,
            None => return rate_limit.resource_limit,
        };

        // Logically this makes more sense as:
        //   consumed_resources = time_to_tat * (resource_limit/period)
        // but we run it this way because of Duration's arithmetic functions
        let consumed_resources =
            (time_to_tat * rate_limit.resource_limit).div_duration_f32(rate_limit.period);
        rate_limit.resource_limit - consumed_resources.ceil() as u32
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use super::*;

    #[test]
    fn test_rate_limit_unused_counts() {
        let base_tat = Instant::now();
        let rate_limit = RateLimit::new(10, Duration::from_secs(1));

        assert_eq!(
            4,
            GcraState {
                tat: Some(base_tat + Duration::from_millis(550))
            }
            .remaining_resources(&rate_limit, base_tat),
            "Remaining count should ceiled"
        );
        assert_eq!(
            0,
            GcraState {
                tat: Some(base_tat + Duration::from_millis(950))
            }
            .remaining_resources(&rate_limit, base_tat),
            "Remaining count should ceiled, thus preventing any additional requests"
        );

        assert_eq!(
            9,
            GcraState {
                tat: Some(base_tat + Duration::from_millis(100))
            }
            .remaining_resources(&rate_limit, base_tat),
            "Remaining count is based on max_period timeout"
        );
    }

    #[test]
    fn gcra_basics() {
        let mut gcra = GcraState::default();
        let rate_limit = RateLimit::new(1, Duration::from_secs(1));

        let first_req_ts = Instant::now();
        assert_eq!(
            Ok(()),
            gcra.check_and_modify(&rate_limit, 1),
            "request #1 should pass"
        );
        let after_first_tat = gcra.tat;
        assert!(
            after_first_tat.is_some(),
            "state should be modified and have a TAT in the future"
        );

        let next_allowed_ts = match gcra.check_and_modify(&rate_limit, 1) {
            Err(GcraError::DeniedUntil { next_allowed_at }) => next_allowed_at,
            _ => panic!("request #2 should be denied temporarily"),
        };
        assert!(
            next_allowed_ts >= first_req_ts + Duration::from_secs(1),
            "we should only be allowed after the burst period"
        );
        assert_eq!(after_first_tat, gcra.tat, "State should be unchanged.")
    }

    #[test]
    fn gcra_limited() {
        const LIMIT: u32 = 5;
        let mut gcra = GcraState::default();
        let rate_limit = RateLimit::new(LIMIT, Duration::from_secs(1));

        let req_ts = Instant::now();
        for i in 0..LIMIT {
            assert_eq!(
                Ok(()),
                gcra.check_and_modify_at(&rate_limit, req_ts, 1),
                "request #{} should pass",
                i + 1
            );
        }

        assert_eq!(
            Some(req_ts + rate_limit.period),
            gcra.tat,
            "state should be modified and have a TAT for the full period",
        );

        // Trigger another event
        let denied_result = gcra.check_and_modify_at(&rate_limit, req_ts, 1);

        assert_eq!(
            Some(req_ts + rate_limit.period),
            gcra.tat,
            "state should not have changed when at limit",
        );

        assert_eq!(
            Err(GcraError::DeniedUntil {
                next_allowed_at: req_ts + rate_limit.emission_interval
            }),
            denied_result,
            "next request should be denied",
        );
    }

    #[test]
    fn gcra_revert_new() {
        const LIMIT: u32 = 5;
        let mut gcra = GcraState::default();
        let rate_limit = RateLimit::new(LIMIT, Duration::from_secs(1));

        let req_ts = Instant::now();
        // Revert before any calls
        assert!(
            gcra.revert_at(&rate_limit, req_ts, 1).is_ok(),
            "revert should have released resources"
        );
        assert_eq!(None, gcra.tat, "state should not have changed at all",);
    }

    #[test]
    fn gcra_revert_existing() {
        const LIMIT: u32 = 5;
        let mut gcra = GcraState::default();
        let rate_limit = RateLimit::new(LIMIT, Duration::from_secs(1));

        let req_ts = Instant::now();
        assert_eq!(
            Ok(()),
            gcra.check_and_modify_at(&rate_limit, req_ts, 5),
            "use up all resources",
        );

        assert_eq!(
            Some(req_ts + rate_limit.period),
            gcra.tat,
            "state should be modified and have a TAT for the full period",
        );

        // Revert
        assert!(
            gcra.revert_at(&rate_limit, req_ts, 1).is_ok(),
            "revert should have released resources"
        );
        assert_eq!(
            Some(req_ts + rate_limit.period - rate_limit.increment_interval(1)),
            gcra.tat,
            "state should not have changed when at limit",
        );

        // Confirm revert re-enables
        assert_eq!(
            Ok(()),
            gcra.check_and_modify_at(&rate_limit, req_ts, 1),
            "additional resources should have been freed",
        );
    }

    #[test]
    fn gcra_revert_existing_ancient() {
        const LIMIT: u32 = 5;
        let mut gcra = GcraState::default();
        let rate_limit = RateLimit::new(LIMIT, Duration::from_secs(1));

        let past_req_ts = Instant::now() - Duration::from_secs(100);
        assert_eq!(
            Ok(()),
            gcra.check_and_modify_at(&rate_limit, past_req_ts, 5),
            "use up all resources, but in distant past",
        );
        assert_eq!(
            Some(past_req_ts + rate_limit.period),
            gcra.tat,
            "state should be modified and have a TAT for the past",
        );

        // Revert using current time
        let req_ts = Instant::now();
        assert!(
            gcra.revert_at(&rate_limit, req_ts, 1).is_ok(),
            "revert should have released resources"
        );
        assert_eq!(
            None, gcra.tat,
            "state should have reset since it was so old",
        );

        // Confirm revert had 0 effect
        assert_eq!(
            Ok(()),
            gcra.check_and_modify_at(&rate_limit, req_ts, 1),
            "additional resources should have been freed",
        );
        assert_eq!(
            gcra.tat,
            Some(req_ts + rate_limit.increment_interval(1)),
            "new TAT state should have been moved forward according to cost like normal"
        );
    }

    #[test]
    fn gcra_leaky() {
        // const INCREMENT_INTERVAL: u64 = 500;
        const INCREMENT_INTERVAL: Duration = Duration::from_millis(500);

        let mut gcra = GcraState::default();
        let rate_limit = RateLimit::new(10, 10 * INCREMENT_INTERVAL);
        assert_eq!(INCREMENT_INTERVAL, rate_limit.emission_interval);

        let arrived_at = Instant::now();
        assert_eq!(
            Ok(()),
            gcra.check_and_modify_at(&rate_limit, arrived_at, 1),
            "request #1 should pass"
        );
        assert_eq!(
            gcra.tat,
            Some(arrived_at + INCREMENT_INTERVAL),
            "new TAT state should have been moved forward according to cost"
        );

        assert_eq!(
            Ok(()),
            gcra.check_and_modify(&rate_limit, 9),
            "request #2 should consume all remaining resources and pass"
        );
        assert!(
            matches!(gcra.check_and_modify(&rate_limit, 1), Err(_allowed_at)),
            "request #3 should fail since all resources consumed"
        );

        let current_tat = gcra.tat.expect("should have a tat state after use");
        assert!(current_tat > Instant::now(), "tat in the future");

        assert!(
            matches!(
                // manually force time check that we know will fail
                gcra.check_and_modify_at(
                    &rate_limit,
                    current_tat - rate_limit.period - Duration::from_millis(1),
                    1
                ),
                Err(_allowed_at)
            ),
            "request #4 before leak period should fail. INCREMENT_INTERVAL has not passed yet."
        );

        assert!(
            matches!(
                gcra.check_and_modify_at(&rate_limit, current_tat - rate_limit.period, 1),
                Err(_allowed_at)
            ),
            "request #5 after leak period should pass. INCREMENT_INTERVAL has passed"
        );
    }

    #[test]
    fn gcra_cost_indefinitely_denied() {
        let mut gcra = GcraState::default();
        let rate_limit = RateLimit::new(5, Duration::from_secs(1));

        assert_eq!(
            Ok(()),
            gcra.check_and_modify(&rate_limit, 1),
            "request #1 should pass"
        );

        let over_limit_cost = rate_limit.resource_limit + 1;
        match gcra.check_and_modify(&rate_limit, over_limit_cost) {
            Err(GcraError::DeniedIndefinitely {
                cost,
                rate_limit: rl,
            }) => {
                assert_eq!(over_limit_cost, cost);
                assert_eq!(rate_limit, rl);
            }
            e => panic!("request #2 would never succeed {:?}", e),
        };
    }

    #[test]
    fn gcra_cost_temporarily_denied() {
        let mut gcra = GcraState::default();
        let rate_limit = RateLimit::new(5, Duration::from_secs(1));

        let first_req_ts = Instant::now();
        assert_eq!(
            Ok(()),
            gcra.check_and_modify(&rate_limit, 1),
            "request #1 should pass"
        );

        let after_first_tat = gcra.tat;
        assert!(
            after_first_tat.is_some(),
            "state should be modified and have a TAT in the future"
        );

        let next_allowed_ts = match gcra.check_and_modify(&rate_limit, rate_limit.resource_limit) {
            Err(GcraError::DeniedUntil { next_allowed_at }) => next_allowed_at,
            _ => panic!("request #2 is only temporarily denied"),
        };

        assert!(
            next_allowed_ts >= first_req_ts + rate_limit.increment_interval(1),
            "we should only be allowed after the burst period {:?} >= {:?}",
            next_allowed_ts,
            first_req_ts + rate_limit.period
        );
        assert_eq!(after_first_tat, gcra.tat, "State should be unchanged.")
    }

    #[test]
    fn gcra_refreshed_after_period() {
        let past_time = Instant::now() - Duration::from_millis(1001);
        let mut gcra = GcraState {
            tat: Some(past_time),
        };
        let rate_limit = RateLimit::new(1, Duration::from_secs(1));
        assert_eq!(
            Ok(()),
            gcra.check_and_modify(&rate_limit, 1),
            "request #1 should pass"
        );

        assert!(
            matches!(gcra.check_and_modify(&rate_limit, 1), Err(_allowed_at)),
            "request #2 should fail"
        );
    }
}