Code Monkey home page Code Monkey logo

actix-rl's Introduction

actix-rl: RateLimiter for actix-web

Description

actix-rl is a RateLimit middleware for the actix-web library. It supports asynchronous processing and currently provides two storage options: in-memory storage (MemStore) and Redis storage (RedisStore).

If you have other storage options, feel free to submit a Pull Request. PR is welcome.

Features

Feature Component Description
default MemStore Store data in memory
redis-store RedisStore Store data using an async connection from redis

Usage

Usage:

  1. Define a Store where the program stores information and sets timeouts.
  2. Define a Controller. The Controller is used to define middleware response behaviors, such as how to return HTTP information during exceptions. You can also use the library's provided default functions, but in most cases, you will need to customize the response to return necessary limit information to the client.
  3. Finally, add a Middleware to your HTTP Server using the wrap function (from actix-web).

Examples

You can find examples in examples folder.

Store

The Store is used to store caching information.

Let's take MemCache as an example:

// data timeout for each key is 10 seconds, with 1024 init capacity.
let store = actix_rl::store::MemCache::new(1024, chrono::Duration::seconds(10));

Controller

Controller is a set of functions. To create a default one:

let controller = controller::Controller::new();

You can determine which requests should be checked, by modifying Controller:

let controller = controller.with_do_rate_limit(|req| !req.path().start_with("/healthz"));

In this case, only those requests without prefix /healthz will be checked by RateLimiter.

For more functions, please check the doc of Controller.

RateLimiter

Define a RateLimiter and wrap to HTTP server:

let rate_limiter = actix_rl::middleware::RateLimitMiddleware::new(
    store,
    10, // max count is 10, which means max 10 hits per 10 seconds.
    controller,
);

Then, add it to actix-web HTTP server wrap:

App::new()
   .wrap(rate_limiter)
    // ...

actix-rl's People

Contributors

caojen avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.