baseplate.lib.ratelimit

Configuring a rate limiter for your request context requires a context factory for the backend and a factory for the rate limiter itself:

redis_pool = pool_from_config(app_config)
backend_factory = RedisRateLimitBackendContextFactory(redis_pool)
ratelimiter_factory = RateLimiterContextFactory(backend_factory, allowance, interval)
baseplate.add_to_context('ratelimiter', ratelimiter_factory)

The rate limiter can then be used during a request with:

try:
    context.ratelimiter.consume(context.request_context.user.id)
    print('Ratelimit passed')
except RateLimitExceededException:
    print('Too many requests')

Classes

class baseplate.lib.ratelimit.RateLimiter(backend, allowance, interval)[source]

A class for rate limiting actions.

Parameters
  • backend (RateLimitBackend) – The backend to use for storing rate limit counters.

  • allowance (int) – The maximum allowance allowed per key.

  • interval (int) – The interval (in seconds) to reset allowances.

consume(key, amount=1)[source]

Consume the given amount from the allowance for the given key.

This will raise baseplate.lib.ratelimit.RateLimitExceededException if the allowance for key is exhausted.

Parameters
  • key (str) – The name of the rate limit bucket to consume from.

  • amount (int) – The amount to consume from the rate limit bucket.

Return type

None

class baseplate.lib.ratelimit.RateLimiterContextFactory(backend_factory, allowance, interval)[source]

RateLimiter context factory.

Parameters
make_object_for_context(name, span)[source]

Return an object that can be added to the context object.

Parameters
  • name (str) – The name assigned to this object on the context.

  • span (Span) – The current span this object is being made for.

Return type

RateLimiter

class baseplate.lib.ratelimit.RateLimitExceededException[source]

This exception gets raised whenever a rate limit is exceeded.

Backends

class baseplate.lib.ratelimit.backends.RateLimitBackend[source]

An interface for rate limit backends to implement.

consume(key, amount, allowance, interval)[source]

Consume the given amount from the allowance for the given key.

This will return true if the key remains below the allowance after consuming the given amount.

Parameters
  • key (str) – The name of the rate limit bucket to consume from.

  • amount (int) – The amount to consume from the rate limit bucket.

  • allowance (int) – The maximum allowance for the rate limit bucket.

  • interval (int) – The interval to reset the allowance.

Return type

bool

Memcache

class baseplate.lib.ratelimit.backends.memcache.MemcacheRateLimitBackendContextFactory(memcache_pool, prefix='rl:')[source]

MemcacheRateLimitBackend context factory.

Parameters
  • memcache_pool (PooledClient) – The memcache pool to back this ratelimit.

  • prefix (str) – A prefix to add to keys during rate limiting. This is useful if you will have two different rate limiters that will receive the same keys.

make_object_for_context(name, span)[source]

Return an object that can be added to the context object.

Parameters
  • name (str) – The name assigned to this object on the context.

  • span (Span) – The current span this object is being made for.

Return type

MemcacheRateLimitBackend

class baseplate.lib.ratelimit.backends.memcache.MemcacheRateLimitBackend(memcache, prefix='rl:')[source]

A Memcache backend for rate limiting.

Parameters
  • memcache (MonitoredMemcacheConnection) – A memcached connection.

  • prefix (str) – A prefix to add to keys during rate limiting. This is useful if you will have two different rate limiters that will receive the same keys.

consume(key, amount, allowance, interval)[source]

Consume the given amount from the allowance for the given key.

This will return true if the key remains below the allowance after consuming the given amount.

Parameters
  • key (str) – The name of the rate limit bucket to consume from.

  • amount (int) – The amount to consume from the rate limit bucket.

  • allowance (int) – The maximum allowance for the rate limit bucket.

  • interval (int) – The interval to reset the allowance.

Return type

bool

Redis

class baseplate.lib.ratelimit.backends.redis.RedisRateLimitBackendContextFactory(redis_pool, prefix='rl:')[source]

RedisRateLimitBackend context factory.

Parameters
  • redis_pool (ConnectionPool) – The redis pool to back this ratelimit.

  • prefix (str) – A prefix to add to keys during rate limiting. This is useful if you will have two different rate limiters that will receive the same keys.

make_object_for_context(name, span)[source]

Return an object that can be added to the context object.

Parameters
  • name (str) – The name assigned to this object on the context.

  • span (Span) – The current span this object is being made for.

Return type

RedisRateLimitBackend

class baseplate.lib.ratelimit.backends.redis.RedisRateLimitBackend(redis, prefix='rl:')[source]

A Redis backend for rate limiting.

Parameters
consume(key, amount, allowance, interval)[source]

Consume the given amount from the allowance for the given key.

This will return true if the key remains below the allowance after consuming the given amount.

Parameters
  • key (str) – The name of the rate limit bucket to consume from.

  • amount (int) – The amount to consume from the rate limit bucket.

  • allowance (int) – The maximum allowance for the rate limit bucket.

  • interval (int) – The interval to reset the allowance.

Return type

bool