Crates.io | gcr |
lib.rs | gcr |
version | 0.1.3 |
source | src |
created_at | 2024-10-22 12:45:45.096937 |
updated_at | 2024-10-29 16:00:34.893806 |
description | A fast, simple, and small Generic Cell Rate (GCR) algorithm implementation with zero dependencies |
homepage | |
repository | https://github.com/rob-maron/gcr |
max_upload_size | |
id | 1418616 |
size | 16,437 |
A fast, simple, and small Generic Cell Rate (GCR
) algorithm implementation with zero dependencies
that allows for dynamic rate adjustment.
use gcr::Gcr;
let mut rate = Gcr::new(10, Duration::from_secs(1), Some(30)).unwrap();
// 10 units allowed every second with a max burst of 30 units at once
rate.request(20).unwrap(); // Leftover capacity is now 10
rate.request(20).unwrap_err(); // Returns `DeniedFor(1 second)`
[Gcr::adjust
] can be used to change the rate of the limiter while preserving the current capacity.
It accepts the same parameters as [Gcr::new
].
rate.adjust(20, Duration::from_secs(1), Some(30)).unwrap();
// 20 units allowed every second with a max burst of 30 units at once
[Gcr::capacity
] can be used to get the current capacity of the rate limiter without making a request.
rate.capacity();