Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,15 @@ jobs:
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo test --all --release

no-std:
name: No-std
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo check --no-default-features
- run: cargo clippy --no-default-features --lib -- -D warnings
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ keywords = ["ratelimit", "token-bucket", "rate-limiter", "throttle", "concurrenc
categories = ["concurrency", "algorithms"]
rust-version = "1.85"

[features]
default = ["std"]
std = []

[dependencies]
thiserror = "2.0"
thiserror = { version = "2.0", default-features = false }
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ A lock-free token bucket ratelimiter for rate limiting and admission control.
cargo add ratelimit
```

`std` is enabled by default. For `no_std` targets, disable default features:

```toml
ratelimit = { version = "1", default-features = false }
```

## Usage

`Ratelimiter::new`, `Ratelimiter::builder`, and `StdClock` are available with
the default `std` feature.

```rust
use ratelimit::Ratelimiter;

Expand Down Expand Up @@ -54,6 +63,26 @@ ratelimiter.set_rate(5_000);

A rate of 0 means unlimited -- `try_wait()` will always succeed.

## no_std

Disable default features and provide your own monotonic clock:

```rust
use core::time::Duration;
use ratelimit::{Clock, Ratelimiter};

struct FixedClock;

impl Clock for FixedClock {
fn elapsed(&self) -> Duration {
Duration::from_millis(10)
}
}

let ratelimiter = Ratelimiter::with_clock(1_000, FixedClock);
assert!(ratelimiter.try_wait().is_ok());
```

## Design

This crate implements a lock-free token bucket algorithm. Tokens accumulate
Expand Down
Loading
Loading