Skip to content

call_next method takes longer than the actual time required #4931

@juyunsang

Description

@juyunsang

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import APIRouter, Request
from fastapi import FastAPI
from fastapi_utils.cbv import cbv

app = FastAPI()

@app.middleware('http')
async def log_middleware(request: Request, call_next):
    request.state.timer = Timer()
    request.state.timer.start('callNextTime')
    response = await call_next(request)
    request.state.timer.end('callNextTime')

router = APIRouter()
@cbv(router)
class TestRouter:
    @router.post('/')
    async def post(self, request:Request):
        request.state.timer.start('routerTime')
        # Do something
        request.state.timer.end('routerTime')
        return response

Description

When we run the stress test, the callNextTime takes much longer than the routerTime
I'd like to ask why this phenomenon occurs
image

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.65.2

Python Version

3.8.5

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions