alisaifee / limits

Rate limiting using various strategies and storage backends such as redis & memcached
https://limits.readthedocs.org
MIT License
423 stars 59 forks source link

Extend WindowStats: add time left before reset #188

Closed drygdryg closed 7 months ago

drygdryg commented 11 months ago

https://github.com/alisaifee/limits/blob/0946fdcd3b9509041a823d3bb1b63ea1de9ab6ee/limits/util.py#L36-L44

Expected Behaviour

Currently, WindowStats class contains information about reset time and remaining amount. Could we add the time remaining until the reset? This would be useful in some scenarios with user interaction (for example, Telegram bots).

I think time left before reset can be calculated in this way:

import time

from limits import parse
from limits.storage import MemoryStorage
from limits.strategies import FixedWindowRateLimiter

storage = MemoryStorage()
my_limiter = FixedWindowRateLimiter(storage)
limit = parse('1 per 30 seconds')
my_limiter.hit(limit, 'test')
window_stats = my_limiter.get_window_stats(limit, 'test')
time_before_reset = window_stats.reset_time - time.time()
print(time_before_reset)
alisaifee commented 9 months ago

@drygdryg sorry for the late response. Perhaps I'm not seeing an obvious usecase - but why not compute the "time until reset" where you intend to use it based on reset_time ?

alisaifee commented 7 months ago

Closing due to inactivity.