slowapi icon indicating copy to clipboard operation
slowapi copied to clipboard

Consider avoiding BaseHTTPMiddleware as parent of SlowAPIMiddleware

Open KissPeter opened this issue 1 year ago • 2 comments

Describe the bug Thanks for your package, really useful. I made a collection of performance optimisations based on my real word examples. You can see them here: https://kisspeter.github.io/fastapi-performance-optimization/

One of the most impactful (negative on performance) item was the usage of BaseHTTPMiddleware the parent of your SlowAPIMiddleware. Therefore the global rate limiter impacts the app performance negatively.

To Reproduce

https://kisspeter.github.io/fastapi-performance-optimization/middleware

Expected behavior SlowAPIMiddleware with minimal or no overall performance impact

Screenshots If applicable, add screenshots to help explain your problem.

Your app (please complete the following information):

  • fastapi or starlette - irrelevant here
  • slowapi version - latest impacted

Additional context Add any other context about the problem here.

KissPeter avatar Apr 18 '24 20:04 KissPeter

Hi @KissPeter thanks for sharing this! Did you do any checks specifically on slowapi?

laurentS avatar Apr 24 '24 15:04 laurentS

@KissPeter did you have a look at the ASGI middleware that doesn't inherit from BaseHTTPMiddleware ? see docs here: https://github.com/laurentS/slowapi/blob/master/docs/examples.md#wsgi-vs-asgi-middleware

thentgesMindee avatar Apr 24 '24 16:04 thentgesMindee