#rate-limiting #limiter #client #contention #overactive #minimizes #caused

simple-rate-limiter

A simple rate limiter that minimizes contention caused by overactive clients

1 stable release

1.0.0 Jul 12, 2022

#10 in #limiter

Download history 2/week @ 2023-12-31 28/week @ 2024-01-07 2/week @ 2024-01-14 1/week @ 2024-01-21 38/week @ 2024-02-11 31/week @ 2024-02-18 38/week @ 2024-02-25 14/week @ 2024-03-03 28/week @ 2024-03-10 8/week @ 2024-03-17 13/week @ 2024-03-24 31/week @ 2024-03-31

82 downloads per month

MIT license

9KB
114 lines

simple-rate-limiter

A simple rate limiter that minimizes contention caused by overactive clients.

Design

This rate limiter is designed with the following goals:

  1. Limited potential for a maliciously overactive client to degrade the ability to serve others

  2. Minimal synchronization cost between CPU caches, while also being fairly accurate for smoothing out bursts of traffic

  3. Memory overhead linear in the amount of active keys

API

pub struct RateLimiter<K: Eq + Hash>;

impl<K: Eq + Hash> RateLimiter<K> {
    pub fn new(limit: usize, slots: usize) -> Arc<Self>;
    pub fn rotate_slots(&self);
    pub fn check_limited(&self, key: K) -> bool;
}

Micro benchmarks (not to be trusted, measure your own applications)

Machine details: AMD® Ryzen 5 3600, x86_64-pc-linux-gnu, SMT enabled

Dependencies

~0.4–7MB
~11K SLoC