alisaifee/limits

Extend WindowStats: add time left before reset

drygdryg opened this issue · 2 comments

limits/limits/util.py

Lines 36 to 44 in 0946fdc

class WindowStats(NamedTuple):
"""
Tuple to describe a rate limited window
"""
#: Time as seconds since the Epoch when this window will be reset
reset_time: int
#: Quantity remaining in this window
remaining: int

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)