baseplate.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.ratelimit.RateLimiter(backend, allowance, interval)

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)

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

This will raise baseplate.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.
class baseplate.ratelimit.RateLimiterContextFactory(backend_factory, allowance, interval)

RateLimiter context factory.

Parameters:
make_object_for_context(name, span)

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

class baseplate.ratelimit.RateLimitExceededException

This exception gets raised whenever a rate limit is exceeded.

Backends

class baseplate.ratelimit.backends.RateLimitBackend

An interface for rate limit backends to implement.

consume(key, amount, allowance, interval)

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.

Memcache

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

MemcacheRateLimitBackend context factory.

Parameters:
  • memcache_pool – An instance of PooledClient
  • 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)

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

class baseplate.ratelimit.backends.memcache.MemcacheRateLimitBackend(memcache, prefix='rl:')

A Memcache backend for rate limiting.

Parameters:
consume(key, amount, allowance, interval)

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.

Redis

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

RedisRateLimitBackend context factory.

Parameters:
  • redis_pool – An instance of redis.ConnectionPool
  • 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)

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

class baseplate.ratelimit.backends.redis.RedisRateLimitBackend(redis, prefix='rl:')

A Redis backend for rate limiting.

Parameters:
consume(key, amount, allowance, interval)

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.