Skip to content

Commit

Permalink
Move reversing slice of middleware apps into the cache
Browse files Browse the repository at this point in the history
In post merge self re-review of #9158, I noticed I
should have moved the reversing into the cache
  • Loading branch information
bdraco committed Sep 18, 2024
1 parent bf022b3 commit 6633b89
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions aiohttp/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ def _build_middlewares(
handler: Handler, apps: Tuple["Application", ...]
) -> Callable[[Request], Awaitable[StreamResponse]]:
"""Apply middlewares to handler."""
for app in apps:
# The slice is to reverse the order of the apps
# so they are applied in the order they were added
for app in apps[::-1]:
assert app.pre_frozen, "middleware handlers are not ready"
for m in app._middlewares_handlers:
handler = update_wrapper(partial(m, handler=handler), handler)
Expand Down Expand Up @@ -396,7 +398,7 @@ async def _handle(self, request: Request) -> StreamResponse:
handler = match_info.handler

if self._run_middlewares:
handler = _build_middlewares(handler, match_info.apps[::-1])
handler = _build_middlewares(handler, match_info.apps)

resp = await handler(request)

Expand Down

0 comments on commit 6633b89

Please sign in to comment.