rate_limit

class litestar.middleware.rate_limit.CacheObject

Bases: object

Representation of a cached object’s metadata.

__init__(history: list[int], reset: int) None
class litestar.middleware.rate_limit.RateLimitConfig

Bases: object

Configuration for RateLimitMiddleware

rate_limit: tuple[DurationUnit, int]

A tuple containing a time unit (second, minute, hour, day) and quantity, e.g. (“day”, 1) or (“minute”, 5).

exclude: str | tuple[str, ...] | None = None

A pattern or list of patterns to skip in the rate limiting middleware.

exclude_opt_key: str | None = None

An identifier to use on routes to disable rate limiting for a particular route.

check_throttle_handler: Callable[[Request[Any, Any, Any]], SyncOrAsyncUnion[bool]] | None = None

Handler callable that receives the request instance, returning a boolean dictating whether or not the request should be checked for rate limiting.

middleware_class

The middleware class to use.

alias of RateLimitMiddleware

set_rate_limit_headers: bool = True

Boolean dictating whether to set the rate limit headers on the response.

__init__(rate_limit: tuple[DurationUnit, int], exclude: str | tuple[str, ...] | None = None, exclude_opt_key: str | None = None, check_throttle_handler: Callable[[Request[Any, Any, Any]], SyncOrAsyncUnion[bool]] | None = None, middleware_class: type[RateLimitMiddleware] = <class 'litestar.middleware.rate_limit.RateLimitMiddleware'>, set_rate_limit_headers: bool = True, rate_limit_policy_header_key: str = 'RateLimit-Policy', rate_limit_remaining_header_key: str = 'RateLimit-Remaining', rate_limit_reset_header_key: str = 'RateLimit-Reset', rate_limit_limit_header_key: str = 'RateLimit-Limit', store: str = 'rate_limit') None
rate_limit_policy_header_key: str = 'RateLimit-Policy'

Key to use for the rate limit policy header.

rate_limit_remaining_header_key: str = 'RateLimit-Remaining'

Key to use for the rate limit remaining header.

rate_limit_reset_header_key: str = 'RateLimit-Reset'

Key to use for the rate limit reset header.

rate_limit_limit_header_key: str = 'RateLimit-Limit'

Key to use for the rate limit limit header.

store: str = 'rate_limit'

Name of the Store to use

get_store_from_app(app: Litestar) Store

Get the store defined in store from an Litestar instance.

class litestar.middleware.rate_limit.RateLimitMiddleware

Bases: ASGIMiddleware

Rate-limiting middleware.

__init__(config: RateLimitConfig) None

Initialize RateLimitMiddleware.

Parameters:

config – An instance of RateLimitConfig.

async handle(scope: Scope, receive: Receive, send: Send, next_app: ASGIApp) None

Handle ASGI call.

Parameters:
  • scope – The ASGI connection scope.

  • receive – The ASGI receive function.

  • send – The ASGI send function

  • next_app – The next ASGI application in the middleware stack to call

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 StorageObject instance.

Returns:

Send wrapper callable.

cache_key_from_request(request: Request[Any, 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, store: Store) CacheObject

Retrieve a list of time stamps for the given duration unit.

Parameters:
Returns:

An CacheObject.

async set_cached_history(key: str, cache_object: CacheObject, store: Store) None

Store history extended with the current timestamp in cache.

Parameters:
Returns:

None

async should_check_request(request: Request[Any, 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

Parameters:

cache_object – A CacheObject.

Returns:

A dict of http headers.

litestar.middleware.rate_limit.DurationUnit

alias of Literal[‘second’, ‘minute’, ‘hour’, ‘day’]