New: Extensible rate limiting middleware for Actix Web

35 points jacob-pro

https://github.com/jacob-pro/actix-extensible-rate-limit

Recently I've written a new rate limit middleware for Actix-Web.

You may be asking - why do need another crate?

The initial motiviation is that a lot of the existing crates unfortunately don't seem to be maintained / up to date with the latest actix web 4.0

But the primary benefit is that I was finding using some of the existing crates quite restrictive, they seem to be designed for very specific use cases, and difficult to extend if you want to do something different.

This crate is intended to support both basic and complex scenarios:

​

  • Derive a custom rate limit key from the request context (based on a header, client IP address, a request path, combination of these, or implement it yourself with an arbitrary future)
  • You can pass in dynamic rate limits and intervals based on the request context, for example you could write a future that maps user id to particular RPS limit, and pass this to the rate limiter.
  • In-memory and redis backends are provided, but you can also implement your own backend if you want to do something different.
  • You can set a custom 429 response, and you can choose to transform response headers based on the rate limit result (e.g adding x-ratelimit-remaining).
  • You can choose to roll back a rate limit count after the request has completed based on the response code. E.g. you may not want 5xx errors to count against a user's rate limit.

​

I would be glad to hear any feedback / suggestions :)