litestar icon indicating copy to clipboard operation
litestar copied to clipboard

Enhancement: Rate Limit Config

Open xiang12383 opened this issue 1 year ago • 14 comments

About class starlite.middleware.rate_limit.RateLimitConfig

this, rate_limit: Tuple[Literal['second', 'minute', 'hour', 'day'], int]

A tuple containing a time unit (second, minute, hour, day) and quantity, e.g. (“day”, 100) or (“minute”, 5) could be changed to [ (“day”, 100) ,(“minute”, 5),("second",1)]?

Fund with Polar

xiang12383 avatar Mar 03 '23 02:03 xiang12383

What would the behaviour be if it's a list?

provinzkraut avatar Mar 03 '23 11:03 provinzkraut

What would the behaviour be if it's a list?

For some people, there is more choice.

Sometimes it is more appropriate to combine several conditions.

xiang12383 avatar Mar 03 '23 11:03 xiang12383

But what would the behaviour be? If you define multiple limits, when would which limit be applied? Randomly? This doesn't make a lot of sense to me, since "5 minutes" necessarily includes "1 second", so what happens if I define both?

provinzkraut avatar Mar 03 '23 11:03 provinzkraut

But what would the behaviour be? If you define multiple limits, when would which limit be applied? Randomly? This doesn't make a lot of sense to me, since "5 minutes" necessarily includes "1 second", so what happens if I define both?

Sorry, My statement is not very clear.

rate_limits=(('second',10),('minute',100),('hour',2000)) or rate_limits=[('second',10),('hour',1000),('day',5000)]

('second',10) => 10 requests per second ('minute',100) =>100 requests per minute ('hour',2000) =>2000 requests per hour

If one of the above three conditions is met first, trigger and return 429.

The above conditions can be combined at will.

xiang12383 avatar Mar 03 '23 14:03 xiang12383

But what would the behaviour be? If you define multiple limits, when would which limit be applied? Randomly? This doesn't make a lot of sense to me, since "5 minutes" necessarily includes "1 second", so what happens if I define both?

Sorry, My statement is not very clear.

rate_limits=(('second',10),('minute',100),('hour',2000)) or rate_limits=[('second',10),('hour',1000),('day',5000)]

('second',10) => 10 requests per second ('minute',100) =>100 requests per minute ('hour',2000) =>1000 requests per hour

If one of the above three conditions is met first, trigger and return 429.

The above conditions can be combined at will.

That makes sense.

I don't have an objection - PR is welcome

Goldziher avatar Mar 03 '23 15:03 Goldziher

Just adding this library in-case one decide to use third party lib,

https://limits.readthedocs.io/en/stable/quickstart.html#examples

Examples:

  • 10 per hour
  • 10/hour
  • 10/hour;100/day;2000 per year
  • 100/day, 500/7days

How it use with flask: https://flask-limiter.readthedocs.io/en/stable/

4n1qz5skwv avatar Apr 10 '23 13:04 4n1qz5skwv

I'm seeing that this issue has a PR closed, any objections if I open one to try work on it?

cesarmg1980 avatar Nov 25 '23 16:11 cesarmg1980

Hi @cesarmg1980 , all yours 🙂

JacobCoffee avatar Nov 25 '23 22:11 JacobCoffee

@JacobCoffee following draft-ietf-httpapi-ratelimit-headers-07 it seems that for multiple rete-limits in the Response Headers the key RateLimit-Policy can have multiple values one next to the other like this: RateLimit-Policy: 10;w=1, 50;w=60, but for the RateLimit key there are no specifications on what to show when multiple rate limits were configured, I found this though:

   Content-Type: application/json
   RateLimit: limit=5000, remaining=100, reset=36000
   RateLimit-Policy: 1000;w=3600, 5000;w=86400

Where it seems that the RateLimit header is set to the larger number, is this the path we would like to follow here?

cesarmg1980 avatar Dec 03 '23 04:12 cesarmg1980

I think the header reflecting largest/most restrictive is right, but I'm out of my comfort zone here @litestar-org/members ?

JacobCoffee avatar Dec 03 '23 06:12 JacobCoffee

@cesarmg1980 Is this something you are still interested in working on?

cofin avatar Feb 17 '24 23:02 cofin

@cofin I actually had something working with a unit test that I needed to improve, but due to lack of time I wasn't able to finish, feel free to give it to somebody else. I'll come back later to work on another issue as soon as I have the time.

cesarmg1980 avatar Feb 17 '24 23:02 cesarmg1980

Hi, can I give this issue a try?

davidpogosian avatar Jun 17 '24 02:06 davidpogosian

@davidpogosian Sure! PRs are always welcome :)

provinzkraut avatar Jun 17 '24 06:06 provinzkraut