abersheeran / asgi-ratelimit

A ASGI Middleware to rate limit
Apache License 2.0
292 stars 11 forks source link

Exclude certain paths? #22

Closed mlexs closed 3 years ago

mlexs commented 3 years ago

I'm having a rather custom requirement to exclude (not count the rate limit) against certain URL paths.

For example given the endpoint

domain.com/api/find/:country

I'd like to exclude some paths then, e.g.:

config={
    r"^/api/find/germany": [ # do nothing ],
    r"^/api/find/belgium": [
        Rule(minute=50, group="belgium")
    ],
    ...
}

I think it's not currently possible? After looking at the code.

Do you think such functionality could benefit other users?

mlexs commented 3 years ago

Me thinks this exclusion should happen in the core module once the URL is matched and before it proceeds to look up user/group?

abersheeran commented 3 years ago

You can do it. Just like the follow code:

config={
    r"^/api/find/germany": [Rule()],
    r"^/api/find/belgium": [
        Rule(minute=50, group="belgium")
    ],
    ...
}

Not giving any time parameter to Rule means that the request restriction is lifted. And, RatelimitMiddleware will use the first matched route, so you can use it first if you put the whitelist route in front of other routes.