#rate-limiting #poem-web #web-framework #routes #middleware #ip #requests

poem-ratelimit

poem-ratelimit is a ratelimit middleware for poem web framework

4 releases

0.2.2 Mar 21, 2022
0.2.1 Mar 11, 2022
0.2.0 Mar 10, 2022
0.1.0 Mar 10, 2022

#11 in #poem-web

Apache-2.0

10KB
190 lines

Rate limit middleware for Poem framework

Usage

Check examples, poem-ratelimit is available on crates.io.

A yaml configuration file is used to set limit for global service, per IP and route:

global:
  # Global limit for all connections
  max_requests: 20
  # Seconds to refresh limit, we allow 20req/30s here
  time_window: 30
ip:
  # QPS limit for a single client IP
  max_requests: 10
  time_window: 30
route:
  /:
    # QPS limit for a single route
    max_requests: 5
    time_window: 30

How it works

For every requests, we use sliding window algorithm to check if the request is processable. Sorted lists with key like IP, route is stored in Redis. You can check the lua script in lib.rs.

License

poem-ratelimit is licensed under Apache-2.0.


lib.rs:

Rate limit middleware for poem web framework

This middleware is used to limit the number of requests per second. Redis is used to store data.

For detailed information, please check examples and our website.

Dependencies

~16–30MB
~467K SLoC