rate_limit¶
- class litestar.middleware.rate_limit.RateLimitConfig¶
基类:
objectRateLimitMiddleware的配置- 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 | list[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.
- set_rate_limit_headers: bool = True¶
Boolean dictating whether to set the rate limit headers on the response.
- rate_limit_policy_header_key: str = 'RateLimit-Policy'¶
Key to use for the rate limit policy header.
- __init__(rate_limit: tuple[DurationUnit, int], exclude: str | list[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_remaining_header_key: str = 'RateLimit-Remaining'¶
Key to use for the rate limit remaining header.
- property middleware: DefineMiddleware¶
使用此属性将配置插入到应用层之一的中间件列表中。 示例: .. code-block:: python from litestar import Litestar, Request, get from litestar.middleware.rate_limit import RateLimitConfig # 限制为每分钟 10 次请求,排除 schema 路径 throttle_config = RateLimitConfig(rate_limit=("minute", 10), exclude=["/schema"]) @get("/") def my_handler(request: Request) -> None: ... app = Litestar(route_handlers=[my_handler], middleware=[throttle_config.middleware]) Returns: 一个
DefineMiddleware实例,其中self作为配置关键字参数值包含在内。
- class litestar.middleware.rate_limit.RateLimitMiddleware¶
-
速率限制中间件。
- __init__(app: ASGIApp, config: RateLimitConfig) None¶
初始化
RateLimitMiddleware。 Args: app: 要调用的下一个nextASGI 应用。 config:RateLimitConfig的实例。
- create_send_wrapper(send: Send, cache_object: CacheObject) Send¶
创建一个
send函数,用于包装原始的send以注入响应头。 Args: send: ASGIsend函数。 cache_object: 一个StorageObject实例。 Returns:send包装后的可调用对象。
- async retrieve_cached_history(key: str, store: Store) CacheObject¶
获取给定持续时间单位的时戳列表。 Args: key: 缓存键。 store: 一个
Store实例。 Returns: 一个CacheObject实例。
- async set_cached_history(key: str, cache_object: CacheObject, store: Store) None¶
在缓存中存储带有当前时间戳的历史记录。 Args: key: 缓存键。 cache_object: 一个
CacheObject。 store: 一个StoreReturns: None
- async should_check_request(request: Request[Any, Any, Any]) bool¶
返回一个布尔值,指示是否应对请求进行速率限制检查。 Args: request: 一个
Request实例。 Returns: 布尔值,规定是否应对请求进行速率限制检查。
- create_response_headers(cache_object: CacheObject) dict[str, str]¶
创建速率限制响应头。 Notes: * 参见 IETF RateLimit draft Args: cache_object: 一个
CacheObject。 Returns: 一个包含 HTTP 头的字典。