apicrud.RateLimit

class apicrud.RateLimit(enable=False, interval=None)

Rate Limiting

Parameters
  • enable (bool) – enable limits [default: config.RATELIMIT_ENABLE]

  • interval (int) – seconds to count limit [config.RATELIMIT_INTERVAL]

__init__(enable=False, interval=None)

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__([enable, interval])

Initialize self.

call([limit, service, uid])

Apply the granted rate limit for uid to a given service Two redis keys are used, for even and odd intervals as measured modulo the Unix epoch time.

reset([service, uid])

Clear the current entry for a service

call(limit=None, service='*', uid=None)

Apply the granted rate limit for uid to a given service Two redis keys are used, for even and odd intervals as measured modulo the Unix epoch time. The current interval’s key is incremented if we haven’t reached the limit yet, then we delete that key at first call seen next interval and increment the other key. The keys expire within interval-1 seconds to keep the cache clear after the user stops sending calls.

Parameters
  • limit (int) – limit for interval [default: from Grants]

  • service (str) – name of a service

  • uid (str) – a user ID

Returns

true if limit exceeded

Return type

bool

reset(service='*', uid=None)

Clear the current entry for a service

Parameters
  • service (str) – name of a service

  • uid (str) – a user ID