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)

class core.ratelimit.RateLimit(resource, maxRate, minutes, method)

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.

Initializes a new RateLimit gate.

Parameters:
  • resource (str) – Name of the resource to protect

  • maxRate (int) – Amount of tries allowed in the give time-span

  • minutes (int) – Length of the time-span in minutes

  • method (Literal[ip, user]) – Lock by IP or by the current user

rateLimitKind = 'viur-ratelimit'
_getEndpointKey()
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)

Return type:

viur.core.db.Key | str

_getCurrentTimeKey()
Returns:

the current lockperiod used in second position of the memcache key

Return type:

str

decrementQuota()

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

Return type:

None

isQuotaAvailable()

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

Return type:

bool

assertQuotaIsAvailable(setRetryAfterHeader=True)

Assert quota is available.

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

Parameters:

setRetryAfterHeader (bool) – 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.

Return type:

bool

core.ratelimit.cleanOldRateLocks(*args, **kwargs)
Return type:

None