|
5 | 5 |
|
6 | 6 | from .._backends.auto import AutoBackend |
7 | 7 | from .._backends.base import SOCKET_OPTION, AsyncNetworkBackend |
8 | | -from .._exceptions import ConnectionNotAvailable, UnsupportedProtocol |
| 8 | +from .._exceptions import ConnectionNotAvailable, PoolTimeout, UnsupportedProtocol |
9 | 9 | from .._models import Origin, Request, Response |
10 | | -from .._synchronization import AsyncEvent, AsyncLock, AsyncShieldCancellation |
| 10 | +from .._synchronization import ( |
| 11 | + AsyncEvent, |
| 12 | + AsyncLock, |
| 13 | + AsyncShieldCancellation, |
| 14 | + sync_current_time, |
| 15 | +) |
11 | 16 | from .connection import AsyncHTTPConnection |
12 | 17 | from .interfaces import AsyncConnectionInterface, AsyncRequestInterface |
13 | 18 |
|
@@ -220,15 +225,20 @@ async def handle_async_request(self, request: Request) -> Response: |
220 | 225 | ) |
221 | 226 |
|
222 | 227 | status = RequestStatus(request) |
| 228 | + timeouts = request.extensions.get("timeout", {}) |
| 229 | + timeout = timeouts.get("pool", None) |
| 230 | + |
| 231 | + if timeout is not None: |
| 232 | + deadline = sync_current_time() + timeout |
| 233 | + else: |
| 234 | + deadline = float("inf") |
223 | 235 |
|
224 | 236 | async with self._pool_lock: |
225 | 237 | self._requests.append(status) |
226 | 238 | await self._close_expired_connections() |
227 | 239 | await self._attempt_to_acquire_connection(status) |
228 | 240 |
|
229 | 241 | while True: |
230 | | - timeouts = request.extensions.get("timeout", {}) |
231 | | - timeout = timeouts.get("pool", None) |
232 | 242 | try: |
233 | 243 | connection = await status.wait_for_connection(timeout=timeout) |
234 | 244 | except BaseException as exc: |
@@ -263,6 +273,10 @@ async def handle_async_request(self, request: Request) -> Response: |
263 | 273 | else: |
264 | 274 | break |
265 | 275 |
|
| 276 | + timeout = deadline - sync_current_time() |
| 277 | + if timeout < 0: |
| 278 | + raise PoolTimeout |
| 279 | + |
266 | 280 | # When we return the response, we wrap the stream in a special class |
267 | 281 | # that handles notifying the connection pool once the response |
268 | 282 | # has been released. |
|
0 commit comments