#Rate limiting understanding

1 messages · Page 1 of 1 (latest)

zealous igloo
#

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)
tiny walrusBOT
#
Notes for Rate limiting understanding
At your assistance

@zealous igloo

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

topaz nymphBOT
#

litestar/middleware/rate_limit.py line 137

async def retrieve_cached_history(self, key: str, store: Store) -> CacheObject:
cunning stump
#

They do slightly different things. One resets the whole CacheObject, while the other does not purge all entries within

#

Whether this is strictly necessary or not, I do not know. It's an optimisation 🤷

zealous igloo