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.RateLimitExceededExceptionif the allowance for key is exhausted.Parameters:
-
class
baseplate.ratelimit.RateLimiterContextFactory(backend_factory, allowance, interval)¶ RateLimiter context factory.
Parameters: - backend_factory – An instance of
baseplate.context.ContextFactory. The context factory must return an instance ofbaseplate.ratelimit.backends.RateLimitBackend - allowance (int) – The maximum allowance allowed per key.
- interval (int) – The interval (in seconds) to reset allowances.
-
make_object_for_context(name, span)¶ Return an object that can be added to the context object.
- backend_factory – An instance of
-
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:
-
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.
- memcache_pool – An instance of
-
class
baseplate.ratelimit.backends.memcache.MemcacheRateLimitBackend(memcache, prefix='rl:')¶ A Memcache backend for rate limiting.
Parameters: - memcache – An instance of
baseplate.context.memcache.MonitoredMemcacheConnection. - 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)¶ 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:
- memcache – An instance of
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.
- redis_pool – An instance of
-
class
baseplate.ratelimit.backends.redis.RedisRateLimitBackend(redis, prefix='rl:')¶ A Redis backend for rate limiting.
Parameters: - redis – An instance of
baseplate.context.redis.MonitoredRedisConnection. - 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)¶ 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:
- redis – An instance of