Fastapi 中的速率限制

Him*_*rya -3 python rate-limiting fastapi

如何在 Fastapi 应用程序中对 API 端点请求进行速率限制?我需要为每个用户每秒限制 API 调用 5 个请求,并且超过该限制会阻止该特定用户 60 秒。

在 main.py

def get_application() -> FastAPI:
     application = FastAPI(title=PROJECT_NAME, debug=DEBUG, version=VERSION)
     application.add_event_handler(
        "startup", create_start_app_handler(application))
     application.add_event_handler(
        "shutdown", create_stop_app_handler(application))
     return application
app = get_application()
Run Code Online (Sandbox Code Playgroud)

在事件.py

def create_start_app_handler(app: FastAPI) -> Callable:  
    async def start_app() -> None:           

        redis = await aioredis.create_redis_pool("redis://localhost:8080")
        FastAPILimiter.init(redis)
    return start_app
Run Code Online (Sandbox Code Playgroud)

在端点

@router.post('/user',
             tags=["user"],
             name="user:user", dependencies=[Depends(RateLimiter(times=5, seconds=60))])
***code****
Run Code Online (Sandbox Code Playgroud)

从此文件 test.py 运行。

import uvicorn

from app.main import app

if __name__ == "__main__":
    uvicorn.run("test:app", host="0.0.0.0", port=8000, reload=True)
Run Code Online (Sandbox Code Playgroud)

我如上编辑,但出现以下错误。

File "****ite-packages\starlette\routing.py", line 526, in lifespan
    async for item in self.lifespan_context(app):
  File "****site-packages\starlette\routing.py", line 467, in default_lifespan
    await self.startup()
  File "****site-packages\starlette\routing.py", line 502, in startup
    await handler()
  File "****app\core\services\events.py", line 15, in start_app
    redis = await aioredis.create_redis_pool("redis://localhost:8080")
  File "****\site-packages\aioredis\commands\__init__.py", line 188, in create_redis_pool
    pool = await create_pool(address, db=db,
  File "****site-packages\aioredis\pool.py", line 58, in create_pool
    await pool._fill_free(override_min=False)
  File "C****\site-packages\aioredis\pool.py", line 383, in _fill_free
    conn = await self._create_new_connection(self._address)
  File "****site-packages\aioredis\connection.py", line 111, in create_connection
    reader, writer = await asyncio.wait_for(open_connection(
  File "****\asyncio\tasks.py", line 455, in wait_for
    return await fut
  File "****\site-packages\aioredis\stream.py", line 23, in open_connection
    transport, _ = await get_event_loop().create_connection(
  File "****\asyncio\base_events.py", line 1033, in create_connection
    raise OSError('Multiple exceptions: {}'.format(
OSError: Multiple exceptions: [Errno 10061] Connect call failed ('::1', 8080, 0, 0), [Errno 10061] Connect call failed ('127.0.0.1', 8080)
Run Code Online (Sandbox Code Playgroud)

kri*_*men 11

FastAPI 本身并不支持这一点,但可以使用一些库(例如下面的库)实现这一点,但通常需要某种数据库支持(redis、memcached 等),尽管 Slowapi 在没有数据库的情况下有内存回退。

为了使用fastapi-limiter,如他们的文档中所示:

注意:您需要一个正在运行的Redis才能正常工作。

import aioredis
import uvicorn
from fastapi import Depends, FastAPI

from fastapi_limiter import FastAPILimiter
from fastapi_limiter.depends import RateLimiter

app = FastAPI()


@app.on_event("startup")
async def startup():
    redis = await aioredis.create_redis_pool("redis://localhost")
    FastAPILimiter.init(redis)


@app.get("/", dependencies=[Depends(RateLimiter(times=2, seconds=5))])
async def index():
    return {"msg": "Hello World"}


if __name__ == "__main__":
    uvicorn.run("main:app", debug=True, reload=True)
Run Code Online (Sandbox Code Playgroud)


小智 7

您可以使用https://github.com/abersheeran/asgi-ratelimit

相比https://pypi.org/project/fastapi-limiter/https://pypi.org/project/slowapi/,它可以更好地满足您的需求。

这是一个例子:超过每秒五次访问限制后,阻止特定用户 60 秒。

app.add_middleware(
    RateLimitMiddleware,
    authenticate=AUTH_FUNCTION,
    backend=RedisBackend(),
    config={
        r"^/user": [Rule(second=5, block_time=60)],
    },
)
Run Code Online (Sandbox Code Playgroud)


Yag*_*nci 5

最好的选择是使用库,因为 FastAPI 不提供此功能开箱即用。

slowapi很棒,而且易于使用。

你可以像这样使用 ut 。

from fastapi import FastAPI
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address


limiter = Limiter(key_func=get_remote_address)
app = FastAPI()
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

@app.get("/home")
@limiter.limit("5/minute")
async def homepage(request: Request):
    return PlainTextResponse("test")

@app.get("/mars")
@limiter.limit("5/minute")
async def homepage(request: Request, response: Response):
    return {"key": "value"}
Run Code Online (Sandbox Code Playgroud)