mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-21 04:46:26 +00:00
style: 统一代码格式和字符串引号使用
refactor: 优化代码结构和可读性 feat: 添加http_limit模块实现请求限制功能 fix: 修复异步任务中使用time.sleep的问题 chore: 更新依赖项并添加pytest测试框架 docs: 更新项目描述信息 perf: 优化Redis序列化方式使用JSON替代pickle test: 添加测试相关配置和依赖
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
from math import ceil
|
||||
from typing import NoReturn
|
||||
|
||||
from fastapi import Request, Response
|
||||
from starlette.websockets import WebSocket
|
||||
|
||||
from app.core.exceptions import CustomException
|
||||
|
||||
|
||||
def http_limit_callback(request: Request, response: Response, expire: int) -> NoReturn:
|
||||
"""
|
||||
请求限制时的默认回调函数
|
||||
|
||||
:param request: FastAPI 请求对象
|
||||
:param response: FastAPI 响应对象
|
||||
:param expire: 剩余毫秒数
|
||||
:return:
|
||||
"""
|
||||
expires = ceil(expire / 30)
|
||||
raise CustomException(
|
||||
status_code=429,
|
||||
msg="请求过于频繁,请稍后重试!",
|
||||
data={"Retry-After": str(expires)},
|
||||
)
|
||||
|
||||
|
||||
async def ws_limit_callback(ws: WebSocket, expire: int) -> None:
|
||||
"""
|
||||
WebSocket请求限制时的默认回调函数
|
||||
|
||||
:param ws: WebSocket连接对象
|
||||
:param expire: 剩余毫秒数
|
||||
:return:
|
||||
"""
|
||||
expires = ceil(expire / 30)
|
||||
await ws.close(code=1008, reason=f"请求过于频繁,请稍后重试!{expires} 秒后重试")
|
||||
Reference in New Issue
Block a user