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.RateLimitExceededExceptionif the allowance for key is exhausted.
- class baseplate.lib.ratelimit.RateLimiterContextFactory(backend_factory, allowance, interval)[source]¶
RateLimiter context factory.
- Parameters:
backend_factory (
ContextFactory) – An instance ofbaseplate.clients.ContextFactory. The context factory must return an instance ofbaseplate.lib.ratelimit.backends.RateLimitBackendallowance (
int) – The maximum allowance allowed per key.interval (
int) – The interval (in seconds) to reset allowances.
Backends¶
- class baseplate.lib.ratelimit.backends.RateLimitBackend[source]¶
An interface for rate limit backends to implement.
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.
- 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.
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.
- class baseplate.lib.ratelimit.backends.redis.RedisRateLimitBackend(redis, prefix='rl:')[source]¶
A Redis backend for rate limiting.
- Parameters:
redis (
MonitoredRedisConnection) – An instance ofbaseplate.clients.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.