brandur/redis-cell

Time window calculation problem

cnsky2016 opened this issue · 1 comments

CL.THROTTLE user123 1 120 60 means 120 tokens on the key are allowed over a 60 second period,but i test it's 2 tokens on the key are allowed over a 1 second exactly, 3 tokens over a 1 second will fail 。

thanks!

Hey @cnsky2016,

This is an inherent property of the algorithm that redis-cell is using — the "tokens over time" configuration is just converted to a rate (e.g. 120 tokens / 60 seconds = 2 tokens per second) which is used to calculate replenishment. Its advantage is that the storage overhead per key is extremely minimal. There's no way to change this behavior without switching out that algorithm I'm afraid.

In your case I'd recommend allowing a larger initial burst, like starting at 60 tokens then having a replenishment at 2 tokens per second. It's not exactly the behavior you wanted, but it should be pretty close.

If you're worried about those 60 tokens spent too quickly, you could get really fancy by introducing a second rate — e.g. one that allows a maximum of 10 tokens per second. That would make sure that user couldn't spend those initial 60 tokens from the first rate instantaneously.