#rate-limiting #web-apps #limit #rate #axum #extractor #state-management

axum-limit

A rate limiting library using token buckets, centered around extractor-based limits for async web applications

3 releases

0.1.0-alpha.2 Aug 27, 2024
0.1.0-alpha.1 Apr 24, 2024

#410 in Asynchronous

Download history 1/week @ 2024-09-08 6/week @ 2024-09-15 10/week @ 2024-09-22 9/week @ 2024-09-29 4/week @ 2024-10-06 1/week @ 2024-10-13 7/week @ 2024-11-03 1/week @ 2024-11-10 2/week @ 2024-11-17 39/week @ 2024-11-24 44/week @ 2024-12-01 87/week @ 2024-12-08 42/week @ 2024-12-15 13/week @ 2024-12-22

196 downloads per month

MIT license

20KB
305 lines

axum-limit

crates.io crates.io download LICENSE dependency status GitHub Workflow Status

This crate provides an efficient rate limiting mechanism using token buckets, specifically designed for asynchronous web applications with a strong focus on extractor-based rate limits.

Features

  • Configurable rate limits using extractors, allowing for flexible limit strategies per route.
  • Supports various time granularities for rate limits (per second, per minute, per hour, and per day).
  • Easily integrates with Axum, using extractors to apply rate limits seamlessly within your application routes.
  • Utilizes DashMap for concurrent state management across asynchronous tasks.

Example

Here is a basic example showing how to use the crate with Axum routes:

use http::Uri;
use axum_limit::{Limit, LimitState, LimitPerSecond};
use axum::{Router, routing::get, response::IntoResponse};

async fn route_handler(_: LimitPerSecond<5, Uri>) -> impl IntoResponse {
    // Handler logic here, automatically enforcing the rate limit
}

fn main() {
    let _app: Router<()> = Router::new()
        .route("/your_route", get(route_handler))
        .with_state(LimitState::<Uri>::default());
}

This example demonstrates setting up a rate limit of 5 requests per second on a specific route. The Limit extractor automatically enforces these limits based on the incoming requests.

For more comprehensive examples, please check the examples directory in this repository.

Dependencies

~2.7–8MB
~67K SLoC