节流
约 148 字小于 1 分钟
fba 内部使用 fastapi-limit 实现后端接口节流
使用方法如下:
@app.get("/", dependencies=[Depends(RateLimiter(times=2, seconds=5))])
async def index_get():
return {"msg": "Hello World"}
@app.post("/", dependencies=[Depends(RateLimiter(times=1, seconds=5))])
async def index_post():
return {"msg": "Hello World"}
@app.get(
"/multiple",
dependencies=[
Depends(RateLimiter(times=1, seconds=5)),
Depends(RateLimiter(times=2, seconds=15)),
],
)
async def multiple():
return {"msg": "Hello World"}
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
ratelimit = WebSocketRateLimiter(times=1, seconds=5)
while True:
try:
data = await websocket.receive_text()
await ratelimit(websocket, context_key=data)
await websocket.send_text("Hello, world")
except HTTPException:
await websocket.send_text("Hello again")