You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -162,13 +166,14 @@ Multiple middlewares are applied in the order they are listed::
162
166
163
167
# Middlewares are applied in order: logging -> auth -> request
164
168
async with ClientSession(middlewares=(logging_middleware, auth_middleware)) as session:
165
-
resp = await session.get('http://example.com')
169
+
async with session.get("http://example.com") as resp:
170
+
...
166
171
167
-
A key aspect to understand about the flat middleware structure is that the execution flow follows this pattern:
172
+
A key aspect to understand about the middleware sequence is that the execution flow follows this pattern:
168
173
169
174
1. The first middleware in the list is called first and executes its code before calling the handler
170
-
2. The handler is the next middleware in the chain (or the actual request handler if there are no more middleware)
171
-
3. When the handler returns a response, execution continues in the first middleware after the handler call
175
+
2. The handler is the next middleware in the chain (or the request handler if there are no more middlewares)
176
+
3. When the handler returns a response, execution continues from the last middleware right after the handler call
172
177
4. This creates a nested "onion-like" pattern for execution
173
178
174
179
For example, with ``middlewares=(middleware1, middleware2)``, the execution order would be:
@@ -179,7 +184,12 @@ For example, with ``middlewares=(middleware1, middleware2)``, the execution orde
179
184
4. Exit ``middleware2`` (post-response code)
180
185
5. Exit ``middleware1`` (post-response code)
181
186
182
-
This flat structure means that middleware is applied on each retry attempt inside the client's retry loop, not just once before all retries. This allows middleware to modify requests freshly on each retry attempt.
187
+
This flat structure means that a middleware is applied on each retry attempt inside the client's retry loop,
188
+
not just once before all retries. This allows middleware to modify requests freshly on each retry attempt.
189
+
190
+
For example, if we had a retry middleware and a logging middleware, and we want every retried request to be
191
+
logged separately, then we'd need to specify ``middlewares=(retry_mw, logging_mw)``. If we reversed the order
192
+
to ``middlewares=(logging_mw, retry_mw)``, then we'd only log once regardless of how many retries are done.
183
193
184
194
.. note::
185
195
@@ -188,157 +198,6 @@ This flat structure means that middleware is applied on each retry attempt insid
188
198
like adding static headers, you can often use request parameters
189
199
(e.g., ``headers``) or session configuration instead.
190
200
191
-
Common Middleware Patterns
192
-
^^^^^^^^^^^^^^^^^^^^^^^^^^
193
-
194
-
.. _client-middleware-retry:
195
-
196
-
Authentication and Retry
197
-
""""""""""""""""""""""""
198
-
199
-
There are two recommended approaches for implementing retry logic:
200
-
201
-
1. **For Loop Pattern (Simple Cases)**
202
-
203
-
Use a bounded ``for`` loop when the number of retry attempts is known and fixed::
204
-
205
-
import hashlib
206
-
from aiohttp import ClientSession, ClientRequest, ClientResponse, ClientHandlerType
0 commit comments