abersheeran / asgi-ratelimit

A ASGI Middleware to rate limit
Apache License 2.0
302 stars 13 forks source link

path matching breaks on 1st match #16

Closed euri10 closed 3 years ago

euri10 commented 3 years ago

not sure if that should be considered as a bug but here is this config I was writing in tests to write custom rules ()

    rate_limit = RateLimitMiddleware(
        hello_world,
        auth_func,
        redisbackend(),
        {
            r"/multiple": [FixedRule(second=1, minute=3)],
            r"/custom": [CustomRule(rules=[LimitFrequency(limit=3, granularity=2)])],
            r"/multiple_custom": [CustomRule(rules=[LimitFrequency(limit=3, granularity=2), LimitFrequency(limit=4, granularity=3)])],
        },
    )

as the path matching breaks on the 1st match, if you send a

        response = await client.get(
            "/multiple_custom", headers={"user": "user", "group": "default"}
        )

it will think you are using the FixedRule from the /multiple endpoint while in fact you wanted not. should I use r"^/multiple$": [FixedRule(second=1, minute=3)], instead the match does not occur as expected.

As a user it could be confusing.

abersheeran commented 3 years ago

Uh, but there seems to be no better way to solve this problem. Perhaps the matching order should be clearly specified in the instructions for use.

euri10 commented 3 years ago

yep a doc hint is likely the best option

On Sat, Apr 3, 2021 at 12:33 PM Aber @.***> wrote:

Uh, but there seems to be no better way to solve this problem. Perhaps the matching order should be clearly specified in the instructions for use.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/abersheeran/asgi-ratelimit/issues/16#issuecomment-812846708, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAINSPUI5P54BOZWOLNRTBDTG3VHLANCNFSM42J4Y7GA .

-- benoit barthelet http://pgp.mit.edu/pks/lookup?op=get&search=0xF150E01A72F6D2EE