<div align="center">
<img src="https://devhub.vip/pages/docs/img/logo.png" alt="token-bucket logo" height="120">
# token-bucket
**Zero-Dependency Lock-Free Atomic GCRA, Fixed-Window, Token-Bucket & Keyed Rate Limiters**
[](https://crates.io/crates/token-bucket)
[](https://docs.rs/token-bucket)
[](https://github.com/crates-dev/ctares/actions/workflows/rust.yml)
[](https://www.rust-lang.org)
[](../LICENSE)
[Workspace Overview](../README.md) ยท [API Docs](https://docs.rs/token-bucket/latest/)
</div>
---
## Overview
`token-bucket` provides zero-dependency, lock-free atomic `GcraLimiter`, `FixedWindowLimiter`, `TokenBucketLimiter`, and a sharded, bounded `KeyedRateLimiter` for per-IP, per-tenant, and per-route admission control.
## Installation
```sh
cargo add token-bucket
```
## Usage
```rust
use token_bucket::{GcraConfig, KeyedRateLimiter, RateLimitPolicy};
let policy = RateLimitPolicy::Gcra(GcraConfig::from_emission_interval_ns(1_000_000, 10).unwrap());
let limiter: KeyedRateLimiter<String> = KeyedRateLimiter::new(policy, 4096).unwrap();
let decision = limiter.check_at_ns("10.0.0.1", 1_000);
assert!(decision.is_allowed());
```
## Verification
```sh
cargo run -p crate-cli -- fmt --check
cargo clippy -p token-bucket --all-features --all-targets -- -D warnings
cargo test -p token-bucket --all-features
```
## License
This project is licensed under the MIT License. See the [LICENSE](../LICENSE) file for details.
## Contact
For inquiries, please contact the author at [root@devhub.vip](mailto:root@devhub.vip).