rate_limit#
- class starlite.middleware.rate_limit.CacheObject#
Bases:
object
Representation of a cached object’s metadata.
- class starlite.middleware.rate_limit.RateLimitMiddleware#
Bases:
AbstractMiddleware
Rate-limiting middleware.
- __init__(app: ASGIApp, config: RateLimitConfig) None #
Initialize
RateLimitMiddleware
.- Parameters:
app – The
next
ASGI app to call.config – An instance of RateLimitConfig.
- create_send_wrapper(send: Send, cache_object: CacheObject) Send #
Create a
send
function that wraps the original send to inject response headers.- Parameters:
send – The ASGI send function.
cache_object – A CacheObject instance.
- Returns:
Send wrapper callable.
- cache_key_from_request(request: Request[Any, Any]) str #
Get a cache-key from a
Request
- Parameters:
request – A
Request
instance.- Returns:
A cache key.
- async retrieve_cached_history(key: str) CacheObject #
Retrieve a list of time stamps for the given duration unit.
- Parameters:
key – Cache key.
- Returns:
An instance of CacheObject.
- async set_cached_history(key: str, cache_object: CacheObject) None #
Store history extended with the current timestamp in cache.
- Parameters:
key – Cache key.
cache_object – An instance of CacheObject.
- Returns:
None
- async should_check_request(request: Request[Any, Any]) bool #
Return a boolean indicating if a request should be checked for rate limiting.
- Parameters:
request – A
Request
instance.- Returns:
Boolean dictating whether the request should be checked for rate-limiting.
- create_response_headers(cache_object: CacheObject) Dict[str, str] #
Create ratelimit response headers.
Notes
see the
IETF RateLimit draft
- Parameters:
cache_object – An instance of Cache Object.
- Returns:
A dict of http headers.
- class starlite.middleware.rate_limit.RateLimitConfig#
Bases:
BaseModel
Configuration for
RateLimitMiddleware
- rate_limit: Tuple[Literal['second', 'minute', 'hour', 'day'], int]#
A tuple containing a time unit (second, minute, hour, day) and quantity, e.g. (“day”, 1) or (“minute”, 5).
- exclude: Optional[Union[str, List[str]]]#
A pattern or list of patterns to skip in the rate limiting middleware.
- exclude_opt_key: Optional[str]#
An identifier to use on routes to disable rate limiting for a particular route.
- check_throttle_handler: Optional[Callable[[Request[Any, Any]], Union[bool, Awaitable[bool]]]]#
Handler callable that receives the request instance, returning a boolean dictating whether or not the request should be checked for rate limiting.
- middleware_class: Type[RateLimitMiddleware]#
The middleware class to use.
- set_rate_limit_headers: bool#
Boolean dictating whether to set the rate limit headers on the response.
- classmethod validate_check_throttle_handler(value: Callable) Callable #
Wrap
check_throttle_handler
in anAsyncCallable
- Parameters:
value – A callable.
- Returns:
An instance of AsyncCallable
- property middleware: DefineMiddleware#
Use this property to insert the config into a middleware list on one of the application layers.
Examples
- Returns:
An instance of DefineMiddleware including
self
as the config kwarg value.