core.ratelimit

Module Contents

Classes

RateLimit

This class is used to restrict access to certain functions to maxRate calls per minute.

Functions

cleanOldRateLocks(*args, **kwargs) → None

class core.ratelimit.RateLimit(resource: str, maxRate: int, minutes: int, method: Literal[ip, user])

Bases: object

This class is used to restrict access to certain functions to maxRate calls per minute.

Usage: Create an instance of this object in you modules __init__ function. then call isQuotaAvailable before executing the action to check if there is quota available and after executing the action decrementQuota.

rateLimitKind = viur-ratelimit
_getEndpointKey(self) Union[viur.core.db.Key, str]
Warning

It’s invalid to call _getEndpointKey if method is set to user and there’s no user logged in!

Returns

the key associated with the current endpoint (it’s IP or the key of the current user)

_getCurrentTimeKey(self) str
Returns

the current lockperiod used in second position of the memcache key

decrementQuota(self) None

Removes one attempt from the pool of available Quota for that user/ip

isQuotaAvailable(self) bool

Checks if there’s currently quota available for the current user/ip :return: True if there’s quota available, False otherwise

assertQuotaIsAvailable(self, setRetryAfterHeader: bool = True) bool

Assert quota is available.

If not quota is available a viur.core.errors.TooManyRequests exception will be raised.

Parameters

setRetryAfterHeader – Set the Retry-After header on the current request response, if the quota is exceeded.

Returns

True if quota is available.

Raises

viur.core.errors.TooManyRequests, if no quote is available.

core.ratelimit.cleanOldRateLocks(*args, **kwargs) None