From 31c0753dc77806585029c504d349df791211bf63 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 18 Sep 2024 16:26:42 +0200 Subject: [PATCH] [PR #9170/eacf2e0 backport][3.10] Move reversing slice of middleware apps into the cache (#9186) --- CHANGES/9170.misc.rst | 1 + aiohttp/web_app.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 120000 CHANGES/9170.misc.rst diff --git a/CHANGES/9170.misc.rst b/CHANGES/9170.misc.rst new file mode 120000 index 00000000000..e41cbad0125 --- /dev/null +++ b/CHANGES/9170.misc.rst @@ -0,0 +1 @@ +9158.misc.rst \ No newline at end of file diff --git a/aiohttp/web_app.py b/aiohttp/web_app.py index b8768064507..c4199b12271 100644 --- a/aiohttp/web_app.py +++ b/aiohttp/web_app.py @@ -84,7 +84,7 @@ 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 @@ -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]