Greetings, I was just reading the rate limiting implementation of litestar just to learn a few things.
I saw in the retrieve_cache_history method there is a while loop that attempts to prune older requests that arent a part of the current time frame. Im unsure why it is there though and I was hoping someone could help my understanding of the implementation.
Given the condition right before the while loop that checks if it is time to reset the cache of requests, and resets it if need be. I was thinking that this is enough? Would it not make the pruning process uneeded? Im unsure if im missing something that may not be so obvious at first glance.
async def retrieve_cached_history(self, key: str, store: Store) -> CacheObject:
"""Retrieve a list of time stamps for the given duration unit.
Args:
key: Cache key.
store: A :class:`Store <.stores.base.Store>`
Returns:
An :class:`CacheObject`.
"""
duration = DURATION_VALUES[self.unit]
now = int(time())
cached_string = await store.get(key)
if cached_string:
cache_object = CacheObject(**decode_json(value=cached_string))
if cache_object.reset <= now:
return CacheObject(history=[], reset=now + duration)
while cache_object.history and cache_object.history[-1] <= now - duration:
cache_object.history.pop()
return cache_object
return CacheObject(history=[], reset=now + duration)