Skip to content

Commit

Permalink
[PR #9170/eacf2e0 backport][3.11] Move reversing slice of middleware …
Browse files Browse the repository at this point in the history
…apps into the cache (#9187)
  • Loading branch information
bdraco authored Sep 18, 2024
1 parent eb68556 commit 8e4678a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/9170.misc.rst
8 changes: 4 additions & 4 deletions aiohttp/web_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import logging
import warnings
from functools import lru_cache, partial, update_wrapper
from functools import cache, partial, update_wrapper
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -79,12 +79,12 @@
_Resource = TypeVar("_Resource", bound=AbstractResource)


@lru_cache(None)
@cache
def _build_middlewares(
handler: Handler, apps: Tuple["Application", ...]
) -> Callable[[Request], Awaitable[StreamResponse]]:
"""Apply middlewares to handler."""
for app in apps:
for app in apps[::-1]:
for m, _ in app._middlewares_handlers: # type: ignore[union-attr]
handler = update_wrapper(partial(m, handler=handler), handler) # type: ignore[misc]
return handler
Expand Down Expand Up @@ -545,7 +545,7 @@ async def _handle(self, request: Request) -> StreamResponse:

if self._run_middlewares:
if not self._has_legacy_middlewares:
handler = _build_middlewares(handler, match_info.apps[::-1])
handler = _build_middlewares(handler, match_info.apps)
else:
for app in match_info.apps[::-1]:
for m, new_style in app._middlewares_handlers: # type: ignore[union-attr]
Expand Down

0 comments on commit 8e4678a

Please sign in to comment.