AaronErhardt / actix-governor

A middleware for actix-web that provides rate-limiting backed by governor.
GNU General Public License v3.0
103 stars 21 forks source link

Ratelimit headers #18

Closed TheAwiteb closed 2 years ago

TheAwiteb commented 2 years ago

Ratelimit headers for TooManyRequest error only ( need help 🚁 )

TheAwiteb commented 2 years ago

I use 'static lifetime, I think this is not good, I tried to solve in a way other than this but I couldn't

TheAwiteb commented 2 years ago

I don't think I understood, this seems difficult to me as I am a beginner

AaronErhardt commented 2 years ago

Basically, futures boil down do state machines. The async keyword tries to make is as simple as possible to create futures but sometimes it introduces limitations because the Rust compiler can't guess what you want to do. If you want to know more, you should read this. My idea is to create a custom future that polls the response until it's ready and then inserts the headers. That should be possible with minimal overhead.

The second one is generics. Instead of storing a runtime variable you can describe the configuration by using generics. For the configuration this would mean this signature:

pub struct GovernorConfig<K: KeyExtractor, M: governor::middlewareRateLimitingMiddleware> {
    // fields ...
}

Depending on the configuration this middleware could be NoOpMiddleware (as default) or StateInformationMiddleware. Then you can implement the Service trait twice, one impl for the default behavior and one impl for the headers that uses StateInformationMiddleware.

That's already pretty advanced Rust, but I'm happy to help you with the implementation. And if I find some time I can give it a shot myself.

TheAwiteb commented 2 years ago

Feature moved to #20 #21