TerminalWitchcraft / actix-ratelimit

Rate limiter framework for Actix web
MIT License
126 stars 25 forks source link

Rate limiting sensible to race conditions #16

Open sadraskol opened 3 years ago

sadraskol commented 3 years ago

Hi there! We've identified a potential race condition when counting down rate limits. Let me explain:

List of actions

There are two actions that can are atomically executed for each request:

Say there's only 1 request left for the rate limiting. If two (probably more) requests are issued concurrently the following can happen:

What now?

The current code only deals with usize counters, so the behavior depends on the implementation of the stores, but overall I think the code is too optimistic for this scenario.

One solution would be to use an atomically safe Get + Decr, using locks for instance. But I think the cost is too high for the normal case.

I would rather document the possibility of this scenarion and I would recommend 2 changes to the current implementation:

If you agree, I can contribute to make these changes. I wanted to make sure we agree and we didn't forget anything before moving on.