#rate-limiting #governor #actix

actix-governor

由 governor crate 支持的 actix-web 的速率限制中间件

16 个版本

0.5.0 2023 年 10 月 4 日
0.4.1 2023 年 6 月 20 日
0.4.0 2023 年 1 月 15 日
0.4.0-beta.32022 年 12 月 5 日
0.2.3 2020 年 12 月 31 日

152HTTP 服务器

Download history 1626/week @ 2024-04-08 1625/week @ 2024-04-15 1454/week @ 2024-04-22 1487/week @ 2024-04-29 1595/week @ 2024-05-06 2160/week @ 2024-05-13 2062/week @ 2024-05-20 2438/week @ 2024-05-27 2428/week @ 2024-06-03 2217/week @ 2024-06-10 2182/week @ 2024-06-17 1935/week @ 2024-06-24 1880/week @ 2024-07-01 2135/week @ 2024-07-08 2016/week @ 2024-07-15 2068/week @ 2024-07-22

每月 8,206 次下载
6 个 crate 中使用

GPL-3.0-or-later

82KB
1.5K SLoC

CI Docs crates.io

Actix Governor

actix-web 提供速率限制支持的中间件 governor

特性

  • 易于使用
  • 高度可定制
  • 高性能
  • 健壮且灵活的 API

示例

use actix_governor::{Governor, GovernorConfigBuilder};
use actix_web::{web, App, HttpServer, Responder};

async fn index() -> impl Responder {
    "Hello world!"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Allow bursts with up to five requests per IP address
    // and replenishes one element every two seconds
    let governor_conf = GovernorConfigBuilder::default()
        .per_second(2)
        .burst_size(5)
        .finish()
        .unwrap();

    HttpServer::new(move || {
        App::new()
            // Enable Governor middleware
            .wrap(Governor::new(&governor_conf))
            // Route hello world service
            .route("/", web::get().to(index))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

将此添加到您的 Cargo.toml

[dependencies]
actix-governor = "0.4"

依赖项

~17–29MB
~515K SLoC