# actix_limitation - Rust
Rate limiter using a fixed window counter for arbitrary keys, backed by Redis for Actix Web.
Keywords: rust, rustlang, rust-lang, actix_limitation

# Crate actix_limitation

source · [−]
Expand description

Rate limiter using a fixed window counter for arbitrary keys, backed by Redis for Actix Web.

    [dependencies]
    actix-web = "4"
    actix-limitation = "0.3"

    use std::time::Duration;
    use actix_web::{get, web, App, HttpServer, Responder};
    use actix_limitation::{Limiter, RateLimiter};

    #[get("/{id}/{name}")]
    async fn index(info: web::Path<(u32, String)>) -> impl Responder {
        format!("Hello {}! id:{}", info.1, info.0)
    }

    #[actix_web::main]
    async fn main() -> std::io::Result<()> {
        let limiter = web::Data::new(
            Limiter::builder("redis://127.0.0.1")
                .cookie_name("session-id".to_owned())
                .session_key("rate-api-id".to_owned())
                .limit(5000)
                .period(Duration::from_secs(3600)) // 60 minutes
                .build()
                .unwrap(),
        );

        HttpServer::new(move || {
            App::new()
                .wrap(RateLimiter::default())
                .app_data(limiter.clone())
                .service(index)
        })
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
    }

## Structs

Builder

Rate limiter builder.

Limiter

Rate limiter.

RateLimiter

Rate limit middleware.

Status

A report for a given key containing the limit status.

## Enums

Error

Failure modes of the rate limiter.

## Constants

DEFAULT_COOKIE_NAME

Default cookie name.

DEFAULT_PERIOD_SECS

Default period (in seconds).

DEFAULT_REQUEST_LIMIT

Default request limit.

DEFAULT_SESSION_KEY

Default session key.