Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve middleware performance #9200

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES/9200.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improved middleware performance -- by :user:`bdraco`.

The ``set_current_app`` method was removed from ``UrlMappingMatchInfo`` because it is no longer used, and it was unlikely external caller would ever use it.
7 changes: 6 additions & 1 deletion aiohttp/web_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ async def impl(request: Request, handler: Handler) -> StreamResponse:

def _fix_request_current_app(app: "Application") -> Middleware:
async def impl(request: Request, handler: Handler) -> StreamResponse:
with request.match_info.set_current_app(app):
match_info = request.match_info
prev = match_info.current_app
match_info.current_app = app
try:
return await handler(request)
finally:
match_info.current_app = prev

return impl
10 changes: 2 additions & 8 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import os
import re
import sys
from contextlib import contextmanager
from pathlib import Path
from types import MappingProxyType
from typing import (
Expand Down Expand Up @@ -271,21 +270,16 @@ def current_app(self) -> "Application":
assert app is not None
return app

@contextmanager
def set_current_app(self, app: "Application") -> Generator[None, None, None]:
@current_app.setter
def current_app(self, app: "Application") -> None:
if DEBUG: # pragma: no cover
if app not in self._apps:
raise RuntimeError(
"Expected one of the following apps {!r}, got {!r}".format(
self._apps, app
)
)
prev = self._current_app
self._current_app = app
try:
yield
finally:
self._current_app = prev

def freeze(self) -> None:
self._frozen = True
Expand Down
Loading