Fastapi: @app.middleware("http")

Created on 25 Oct 2020  路  2Comments  路  Source: tiangolo/fastapi

@app.middleware("http")

I quote middleware to calculate the process time, while I find the requests per second (RPS) to be 60% of without middleware. That means 4000rps to around 2500rps. Is it right?
Could you please give some advice to improve the performance of middleware?

@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_timestamp = time.time()
    request.state.timestamp = start_timestamp
    response = await call_next(request)
    process_time = time.time() - start_timestamp
    response.headers["X-Process-Time"] = str(process_time)
    logger.info({"status_code": response.status_code,
                 "url": request.url,
                 "client": request.client.host,
                 "timestamp": start_timestamp,
                 "process_time": process_time})
    return response
question

Most helpful comment

It's a known issue on Starlette side.

When you create a middleware using the decorator @app.middleware('http') it creates a BaseHTTPMiddleware based middleware. Precisely the class that comes with the issue (https://github.com/encode/starlette/issues/919).

You should create a pure ASGI middleware. I'm going to recommend this middleware tho: TimingMiddleware.

The FastAPI @app.add_middleware is inherited from Starlette, so you should be able to do as: https://github.com/steinnes/timing-asgi#usage.

Note:
There's an issue on Starlette side to improve the documentation about middlewares. So, I think a PR will be gladly accepted by @tiangolo (those are my words, not his, jfyk) if someone wants to improve the documentation about it on Advanced Middleware section on FastAPI itself.

All 2 comments

It's a known issue on Starlette side.

When you create a middleware using the decorator @app.middleware('http') it creates a BaseHTTPMiddleware based middleware. Precisely the class that comes with the issue (https://github.com/encode/starlette/issues/919).

You should create a pure ASGI middleware. I'm going to recommend this middleware tho: TimingMiddleware.

The FastAPI @app.add_middleware is inherited from Starlette, so you should be able to do as: https://github.com/steinnes/timing-asgi#usage.

Note:
There's an issue on Starlette side to improve the documentation about middlewares. So, I think a PR will be gladly accepted by @tiangolo (those are my words, not his, jfyk) if someone wants to improve the documentation about it on Advanced Middleware section on FastAPI itself.

Thank you, Kludex锛丮aybe an wrapper will works well. Hope to hear good news soon.

Was this page helpful?
0 / 5 - 0 ratings