fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
477 stars 66 forks source link

Use `Math.ceil` for timeLeft calculation #325

Closed gurgunday closed 10 months ago

gurgunday commented 10 months ago

Last (small) breaking change, promise 😁

Here's the case for it: it changes nothing on our side, but sending an endpoint Retry-After: 0 is arguably worse than sending Retry-After: 1 when they still have 900ms of blockage

Also, the whole idea of sending Retry-After: 0 makes no sense

And finally, all tests that contained checks against multiple values have been modified

Uzlopak commented 10 months ago

I really had to think about it and I think it is the correct way.

Imagining following case:

You have 1.9 seconds left. Math.floor would return 1 and means in 1 second you can make your next request. You make your next request in 1 second and get 0.9 as the internal value and get again a 429 error. But with Math.ceil we get for 1.9 seconds the value 2 and you would do the next request in 2 seconds and dont get the 429 error.