Skip to content

Commit 3dc008a

Browse files
authored
Add Token Bucket and GCRA rate limiting algorithms (#152)
* added token bucket rate limiter * added GCRA rate limiter; integrated new rate limiting algos with volga * updated token bucket rate limiter to use microseconds for last_seen and eviction grace.
1 parent 657d211 commit 3dc008a

20 files changed

Lines changed: 2186 additions & 51 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Fast, simple, and high-performance web framework for Rust, built on top of
55
Volga is designed to make building HTTP services straightforward and explicit,
66
while keeping performance predictable and overhead minimal.
77

8-
[![latest](https://img.shields.io/badge/latest-0.8.1-blue)](https://crates.io/crates/volga)
8+
[![latest](https://img.shields.io/badge/latest-0.8.2-blue)](https://crates.io/crates/volga)
99
[![latest](https://img.shields.io/badge/rustc-1.90+-964B00)](https://crates.io/crates/volga)
1010
[![License: MIT](https://img.shields.io/badge/License-MIT-violet.svg)](https://github.com/RomanEmreis/volga/blob/main/LICENSE)
1111
[![Build](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml/badge.svg)](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml)
@@ -46,7 +46,7 @@ Volga is a good fit if you:
4646
### Dependencies
4747
```toml
4848
[dependencies]
49-
volga = "0.8.1"
49+
volga = "0.8.2"
5050
tokio = { version = "1", features = ["full"] }
5151
```
5252
### Simple request handler

examples/rate_limiting/src/main.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::time::Duration;
88
use serde::Deserialize;
99
use volga::{
1010
auth::{Claims, roles, DecodingKey},
11-
rate_limiting::{FixedWindow, SlidingWindow, by},
11+
rate_limiting::{FixedWindow, SlidingWindow, TokenBucket, Gcra, by},
1212
App
1313
};
1414

@@ -26,20 +26,25 @@ fn main() {
2626
.with_sliding_window(
2727
SlidingWindow::new(100, Duration::from_secs(15))
2828
)
29-
.with_sliding_window(
30-
SlidingWindow::new(100, Duration::from_secs(30))
29+
.with_token_bucket(
30+
TokenBucket::new(100, 1.0)
31+
)
32+
.with_gcra(
33+
Gcra::new(50.0, 10)
3134
.with_name("burst")
3235
);
3336

34-
app.use_fixed_window(by::ip());
37+
app.use_token_bucket(by::ip());
3538

3639
app.group("/api", |api| {
37-
api.sliding_window(by::header("x-api-key"));
40+
api.fixed_window(by::header("x-api-key"));
3841

3942
api.map_get("/fixed", async || "Hello from fixed window!");
4043
api.map_get("/sliding", async || "Hello from sliding window!")
44+
.sliding_window(by::header("x-api-key"));
45+
api.map_get("/gcra", async || "Hello from GCRA!")
4146
.authorize::<Claims>(roles(["admin", "user"]))
42-
.sliding_window(by::user(|c: &Claims| c.sub.as_str()).using("burst"));
47+
.gcra(by::user(|c: &Claims| c.sub.as_str()).using("burst"));
4348
});
4449

4550
app.run_blocking();

volga-dev-cert/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "volga-dev-cert"
3-
version = "0.8.1"
3+
version = "0.8.2"
44
readme = "README.md"
55
description = "A Rust library for generating self-signed TLS certificates for local development."
66
edition.workspace = true

volga-dev-cert/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Volga Development Certificates
22
A Rust library for generating self-signed TLS certificates for local development.
33

4-
[![latest](https://img.shields.io/badge/latest-0.8.1-blue)](https://crates.io/crates/volga)
4+
[![latest](https://img.shields.io/badge/latest-0.8.2-blue)](https://crates.io/crates/volga)
55
[![latest](https://img.shields.io/badge/rustc-1.90+-964B00)](https://crates.io/crates/volga)
66
[![License: MIT](https://img.shields.io/badge/License-MIT-violet.svg)](https://github.com/RomanEmreis/volga/blob/main/LICENSE)
77
[![Build](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml/badge.svg)](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml)

volga-di/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "volga-di"
3-
version = "0.8.1"
3+
version = "0.8.2"
44
readme = "README.md"
55
description = "Dependency Injection tools for Volga Web Framework"
66
edition.workspace = true

volga-di/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Volga DI
22
A standalone, flexible, and easy-to-configure DI container.
33

4-
[![latest](https://img.shields.io/badge/latest-0.8.1-blue)](https://crates.io/crates/volga)
4+
[![latest](https://img.shields.io/badge/latest-0.8.2-blue)](https://crates.io/crates/volga)
55
[![latest](https://img.shields.io/badge/rustc-1.90+-964B00)](https://crates.io/crates/volga)
66
[![License: MIT](https://img.shields.io/badge/License-MIT-violet.svg)](https://github.com/RomanEmreis/volga/blob/main/LICENSE)
77
[![Build](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml/badge.svg)](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml)
@@ -15,12 +15,12 @@ A standalone, flexible, and easy-to-configure DI container.
1515
#### Standalone
1616
```toml
1717
[dependencies]
18-
volga-di = "0.8.1"
18+
volga-di = "0.8.2"
1919
```
2020
#### Part of Volga Web Framework
2121
```toml
2222
[dependencies]
23-
volga = { version = "0.8.1", features = ["di"] }
23+
volga = { version = "0.8.2", features = ["di"] }
2424
```
2525

2626
### Example

volga-macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "volga-macros"
3-
version = "0.8.1"
3+
version = "0.8.2"
44
readme = "README.md"
55
description = "Macros for Volga Web Framework"
66
edition.workspace = true

volga-macros/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Volga Macros
22
Macros library for Volga Web Framework
33

4-
[![latest](https://img.shields.io/badge/latest-0.8.1-blue)](https://crates.io/crates/volga)
4+
[![latest](https://img.shields.io/badge/latest-0.8.2-blue)](https://crates.io/crates/volga)
55
[![latest](https://img.shields.io/badge/rustc-1.90+-964B00)](https://crates.io/crates/volga)
66
[![License: MIT](https://img.shields.io/badge/License-MIT-violet.svg)](https://github.com/RomanEmreis/volga/blob/main/LICENSE)
77
[![Build](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml/badge.svg)](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml)
@@ -13,6 +13,6 @@ Macros library for Volga Web Framework
1313
## Dependencies
1414
```toml
1515
[dependencies]
16-
volga = { version = "0.8.1", features = ["macros"] }
16+
volga = { version = "0.8.2", features = ["macros"] }
1717
```
1818

volga-rate-limiter/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "volga-rate-limiter"
3-
version = "0.8.1"
3+
version = "0.8.2"
44
readme = "README.md"
55
description = "Macros for Volga Web Framework"
66
edition.workspace = true

volga-rate-limiter/README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A lightweight and efficient rate-limiting library for Rust.
55
This crate provides in-memory rate limiting algorithms designed
66
for high-performance HTTP services and middleware.
77

8-
[![latest](https://img.shields.io/badge/latest-0.8.1-blue)](https://crates.io/crates/volga)
8+
[![latest](https://img.shields.io/badge/latest-0.8.2-blue)](https://crates.io/crates/volga)
99
[![latest](https://img.shields.io/badge/rustc-1.90+-964B00)](https://crates.io/crates/volga)
1010
[![License: MIT](https://img.shields.io/badge/License-MIT-violet.svg)](https://github.com/RomanEmreis/volga/blob/main/LICENSE)
1111
[![Build](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml/badge.svg)](https://github.com/RomanEmreis/volga/actions/workflows/rust.yml)
@@ -33,15 +33,26 @@ synchronize state across multiple processes or machines.
3333

3434
The following rate-limiting algorithms are provided:
3535

36-
- [`FixedWindowRateLimiter`]
36+
- `FixedWindowRateLimiter`
3737
- Counts requests in discrete, fixed-size time windows
3838
- Very fast and simple
3939
- May allow short bursts at window boundaries
4040

41-
- [`SlidingWindowRateLimiter`]
41+
- `SlidingWindowRateLimiter`
4242
- Uses a sliding time window with linear weighting
4343
- Provides smoother request distribution
4444
- Slightly more expensive than a fixed window
45+
46+
- `TokenBucketRateLimiter`
47+
- Allows bursts up to a token bucket capacity
48+
- Enforces a steady average refill rate
49+
- Simple and flexible for bursty traffic
50+
51+
- `GcraRateLimiter`
52+
- Uses the Generic Cell Rate Algorithm (GCRA)
53+
- Smooths traffic with explicit burst tolerance
54+
- Accurate average rate enforcement
55+
4556

4657
## Time Source Abstraction
4758

@@ -52,7 +63,7 @@ This allows:
5263
- Custom time implementations if needed
5364

5465
The default implementation, [`SystemTimeSource`], is based on
55-
`std::time::SystemTime`.
66+
`std::time::Instant`.
5667

5768
## Concurrency Model
5869

0 commit comments

Comments
 (0)