core.ratelimit ============== .. py:module:: core.ratelimit Classes ------- .. autoapisummary:: core.ratelimit.RateLimit Functions --------- .. autoapisummary:: core.ratelimit.cleanOldRateLocks Module Contents --------------- .. py:class:: RateLimit(resource, maxRate, minutes, method) Bases: :py:obj:`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. :param resource: Name of the resource to protect :param maxRate: Amount of tries allowed in the give time-span :param minutes: Length of the time-span in minutes :param method: Lock by IP or by the current user .. py:attribute:: rateLimitKind :value: 'viur-ratelimit' .. py:attribute:: resource .. py:attribute:: maxRate .. py:attribute:: minutes .. py:attribute:: steps .. py:attribute:: secondsPerStep .. py:attribute:: useUser .. py:method:: _getEndpointKey() :warning: It's invalid to call _getEndpointKey if method is set to user and there's no user logged in! :return: the key associated with the current endpoint (it's IP or the key of the current user) .. py:method:: _getCurrentTimeKey() :return: the current lockperiod used in second position of the memcache key .. py:method:: decrementQuota() Removes one attempt from the pool of available Quota for that user/ip .. py:method:: isQuotaAvailable() Checks if there's currently quota available for the current user/ip :return: True if there's quota available, False otherwise .. py:method:: assertQuotaIsAvailable(setRetryAfterHeader = True) Assert quota is available. If not quota is available a :class:`viur.core.errors.TooManyRequests` exception will be raised. :param setRetryAfterHeader: Set the Retry-After header on the current request response, if the quota is exceeded. :return: True if quota is available. :raises: :exc:`viur.core.errors.TooManyRequests`, if no quote is available. .. py:function:: cleanOldRateLocks(*args, **kwargs)