httpcore2 2.7.0__tar.gz → 2.8.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {httpcore2-2.7.0 → httpcore2-2.8.0}/PKG-INFO +1 -1
  2. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_async/connection.py +3 -0
  3. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_async/connection_pool.py +37 -11
  4. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_async/http2.py +3 -0
  5. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_async/interfaces.py +10 -0
  6. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_sync/connection.py +3 -0
  7. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_sync/connection_pool.py +37 -11
  8. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_sync/http2.py +3 -0
  9. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_sync/interfaces.py +10 -0
  10. {httpcore2-2.7.0 → httpcore2-2.8.0}/.gitignore +0 -0
  11. {httpcore2-2.7.0 → httpcore2-2.8.0}/CHANGELOG.md +0 -0
  12. {httpcore2-2.7.0 → httpcore2-2.8.0}/LICENSE.md +0 -0
  13. {httpcore2-2.7.0 → httpcore2-2.8.0}/README.md +0 -0
  14. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/__init__.py +0 -0
  15. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_api.py +0 -0
  16. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_async/__init__.py +0 -0
  17. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_async/http11.py +0 -0
  18. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_async/http_proxy.py +0 -0
  19. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_async/socks_proxy.py +0 -0
  20. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_backends/__init__.py +0 -0
  21. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_backends/anyio.py +0 -0
  22. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_backends/auto.py +0 -0
  23. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_backends/base.py +0 -0
  24. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_backends/mock.py +0 -0
  25. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_backends/sync.py +0 -0
  26. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_backends/trio.py +0 -0
  27. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_exceptions.py +0 -0
  28. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_models.py +0 -0
  29. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_ssl.py +0 -0
  30. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_sync/__init__.py +0 -0
  31. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_sync/http11.py +0 -0
  32. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_sync/http_proxy.py +0 -0
  33. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_sync/socks_proxy.py +0 -0
  34. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_synchronization.py +0 -0
  35. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_trace.py +0 -0
  36. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/_utils.py +0 -0
  37. {httpcore2-2.7.0 → httpcore2-2.8.0}/httpcore2/py.typed +0 -0
  38. {httpcore2-2.7.0 → httpcore2-2.8.0}/pyproject.toml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpcore2
3
- Version: 2.7.0
3
+ Version: 2.8.0
4
4
  Summary: A minimal low-level HTTP client.
5
5
  Project-URL: Changelog, https://github.com/pydantic/httpx2/blob/main/src/httpcore2/CHANGELOG.md
6
6
  Project-URL: Homepage, https://github.com/pydantic/httpx2
@@ -177,6 +177,9 @@ class AsyncHTTPConnection(AsyncConnectionInterface):
177
177
  return self._connect_failed
178
178
  return self._connection.is_idle()
179
179
 
180
+ def can_multiplex(self) -> bool:
181
+ return self._connection is not None and self._connection.can_multiplex()
182
+
180
183
  def is_closed(self) -> bool:
181
184
  if self._connection is None:
182
185
  return self._connect_failed
@@ -264,34 +264,40 @@ class AsyncConnectionPool(AsyncRequestInterface):
264
264
  closing_connections: list[AsyncConnectionInterface] = []
265
265
  retained_connections: list[AsyncConnectionInterface] = []
266
266
 
267
- # Connections currently referenced by an active request (including
268
- # connections that are in the process of being established).
267
+ # Connections currently referenced by an in-flight request, including
268
+ # connections that are in the process of being established and idle
269
+ # connections reserved by an assigned-but-not-yet-sent request.
269
270
  request_connections = {r.connection for r in self._requests}
270
271
 
271
272
  # First we handle cleaning up any connections that are closed
272
- # or have expired their keep-alive, in a single pass.
273
+ # or have expired their keep-alive, in a single pass. Reserved
274
+ # connections skip the expiry check: they were checked when assigned,
275
+ # and `has_expired()` on an idle connection probes the socket.
273
276
  for connection in self._connections:
277
+ reserved = connection in request_connections
274
278
  if connection.is_closed():
275
279
  continue
276
- elif not (connection.is_connected() or connection in request_connections):
280
+ elif not (connection.is_connected() or reserved):
277
281
  # Garbage: a NEW-state connection whose request was cancelled
278
282
  # before the TCP handshake completed. Drop it without closing
279
283
  # (there is no socket to close yet).
280
284
  continue
281
- elif connection.has_expired():
285
+ elif not reserved and connection.has_expired():
282
286
  closing_connections.append(connection)
283
287
  else:
284
288
  retained_connections.append(connection)
285
289
 
286
290
  # Then we close any surplus idle connections, to enforce the
287
- # max_keepalive_connections setting.
291
+ # max_keepalive_connections setting. Reserved connections are not
292
+ # surplus: a request is about to be sent on them.
288
293
  idle_surplus = (
289
- sum(connection.is_idle() for connection in retained_connections) - self._max_keepalive_connections
294
+ sum(connection.is_idle() and connection not in request_connections for connection in retained_connections)
295
+ - self._max_keepalive_connections
290
296
  )
291
297
  if idle_surplus > 0:
292
298
  kept: list[AsyncConnectionInterface] = []
293
299
  for connection in retained_connections:
294
- if idle_surplus > 0 and connection.is_idle():
300
+ if idle_surplus > 0 and connection.is_idle() and connection not in request_connections:
295
301
  closing_connections.append(connection)
296
302
  idle_surplus -= 1
297
303
  else:
@@ -303,11 +309,27 @@ class AsyncConnectionPool(AsyncRequestInterface):
303
309
  # Snapshot the set of reusable connections once, rather than rebuilding
304
310
  # it per queued request — this is what brings the loop from O(N*M) to
305
311
  # O(N+M) in the common case.
306
- available_connections = [connection for connection in self._connections if connection.is_available()]
312
+ #
313
+ # An idle connection already assigned to an in-flight request is
314
+ # reserved: it stays IDLE until the winning task sends on it, so
315
+ # without this exclusion the next pass would assign it again and the
316
+ # loser would churn through `ConnectionNotAvailable`. Multiplexing
317
+ # connections are exempt: they can take further requests while idle.
318
+ available_connections = [
319
+ connection
320
+ for connection in self._connections
321
+ if connection.is_available()
322
+ and not (connection.is_idle() and connection in request_connections and not connection.can_multiplex())
323
+ ]
307
324
  new_connection_budget = self._max_connections - len(self._connections)
308
325
 
309
- # Assign queued requests to connections.
326
+ # Assign queued requests to connections. Once no connection is
327
+ # available and no new connection may be created, no queued request
328
+ # can be assigned, so the scan stops early: this keeps a pass on a
329
+ # saturated pool O(connections) rather than O(in-flight requests).
310
330
  for pool_request in self._requests:
331
+ if not available_connections and new_connection_budget <= 0:
332
+ break
311
333
  if not pool_request.is_queued():
312
334
  continue
313
335
  origin = pool_request.request.url.origin
@@ -318,9 +340,13 @@ class AsyncConnectionPool(AsyncRequestInterface):
318
340
  # 2. We can create a new connection to handle the request.
319
341
  # 3. We can close an idle connection and then create a new connection
320
342
  # to handle the request.
321
- for connection in available_connections:
343
+ for idx, connection in enumerate(available_connections):
322
344
  if connection.can_handle_request(origin):
323
345
  pool_request.assign_to_connection(connection)
346
+ if connection.is_idle() and not connection.can_multiplex():
347
+ # An idle HTTP/1.1 connection can only take this
348
+ # single request until it is released.
349
+ del available_connections[idx]
324
350
  break
325
351
  else:
326
352
  if new_connection_budget > 0:
@@ -495,6 +495,9 @@ class AsyncHTTP2Connection(AsyncConnectionInterface):
495
495
  def is_idle(self) -> bool:
496
496
  return self._state == HTTPConnectionState.IDLE
497
497
 
498
+ def can_multiplex(self) -> bool:
499
+ return True
500
+
498
501
  def is_closed(self) -> bool:
499
502
  return self._state == HTTPConnectionState.CLOSED
500
503
 
@@ -140,6 +140,16 @@ class AsyncConnectionInterface(AsyncRequestInterface):
140
140
  """
141
141
  raise NotImplementedError() # pragma: no cover
142
142
 
143
+ def can_multiplex(self) -> bool:
144
+ """
145
+ Return `True` if the connection can serve multiple requests
146
+ concurrently, such as an established HTTP/2 connection.
147
+
148
+ The default covers HTTP/1.1-style implementations, which serve a
149
+ single request at a time.
150
+ """
151
+ return False
152
+
143
153
  def is_closed(self) -> bool:
144
154
  """
145
155
  Return `True` if the connection has been closed.
@@ -177,6 +177,9 @@ class HTTPConnection(ConnectionInterface):
177
177
  return self._connect_failed
178
178
  return self._connection.is_idle()
179
179
 
180
+ def can_multiplex(self) -> bool:
181
+ return self._connection is not None and self._connection.can_multiplex()
182
+
180
183
  def is_closed(self) -> bool:
181
184
  if self._connection is None:
182
185
  return self._connect_failed
@@ -264,34 +264,40 @@ class ConnectionPool(RequestInterface):
264
264
  closing_connections: list[ConnectionInterface] = []
265
265
  retained_connections: list[ConnectionInterface] = []
266
266
 
267
- # Connections currently referenced by an active request (including
268
- # connections that are in the process of being established).
267
+ # Connections currently referenced by an in-flight request, including
268
+ # connections that are in the process of being established and idle
269
+ # connections reserved by an assigned-but-not-yet-sent request.
269
270
  request_connections = {r.connection for r in self._requests}
270
271
 
271
272
  # First we handle cleaning up any connections that are closed
272
- # or have expired their keep-alive, in a single pass.
273
+ # or have expired their keep-alive, in a single pass. Reserved
274
+ # connections skip the expiry check: they were checked when assigned,
275
+ # and `has_expired()` on an idle connection probes the socket.
273
276
  for connection in self._connections:
277
+ reserved = connection in request_connections
274
278
  if connection.is_closed():
275
279
  continue
276
- elif not (connection.is_connected() or connection in request_connections):
280
+ elif not (connection.is_connected() or reserved):
277
281
  # Garbage: a NEW-state connection whose request was cancelled
278
282
  # before the TCP handshake completed. Drop it without closing
279
283
  # (there is no socket to close yet).
280
284
  continue
281
- elif connection.has_expired():
285
+ elif not reserved and connection.has_expired():
282
286
  closing_connections.append(connection)
283
287
  else:
284
288
  retained_connections.append(connection)
285
289
 
286
290
  # Then we close any surplus idle connections, to enforce the
287
- # max_keepalive_connections setting.
291
+ # max_keepalive_connections setting. Reserved connections are not
292
+ # surplus: a request is about to be sent on them.
288
293
  idle_surplus = (
289
- sum(connection.is_idle() for connection in retained_connections) - self._max_keepalive_connections
294
+ sum(connection.is_idle() and connection not in request_connections for connection in retained_connections)
295
+ - self._max_keepalive_connections
290
296
  )
291
297
  if idle_surplus > 0:
292
298
  kept: list[ConnectionInterface] = []
293
299
  for connection in retained_connections:
294
- if idle_surplus > 0 and connection.is_idle():
300
+ if idle_surplus > 0 and connection.is_idle() and connection not in request_connections:
295
301
  closing_connections.append(connection)
296
302
  idle_surplus -= 1
297
303
  else:
@@ -303,11 +309,27 @@ class ConnectionPool(RequestInterface):
303
309
  # Snapshot the set of reusable connections once, rather than rebuilding
304
310
  # it per queued request — this is what brings the loop from O(N*M) to
305
311
  # O(N+M) in the common case.
306
- available_connections = [connection for connection in self._connections if connection.is_available()]
312
+ #
313
+ # An idle connection already assigned to an in-flight request is
314
+ # reserved: it stays IDLE until the winning task sends on it, so
315
+ # without this exclusion the next pass would assign it again and the
316
+ # loser would churn through `ConnectionNotAvailable`. Multiplexing
317
+ # connections are exempt: they can take further requests while idle.
318
+ available_connections = [
319
+ connection
320
+ for connection in self._connections
321
+ if connection.is_available()
322
+ and not (connection.is_idle() and connection in request_connections and not connection.can_multiplex())
323
+ ]
307
324
  new_connection_budget = self._max_connections - len(self._connections)
308
325
 
309
- # Assign queued requests to connections.
326
+ # Assign queued requests to connections. Once no connection is
327
+ # available and no new connection may be created, no queued request
328
+ # can be assigned, so the scan stops early: this keeps a pass on a
329
+ # saturated pool O(connections) rather than O(in-flight requests).
310
330
  for pool_request in self._requests:
331
+ if not available_connections and new_connection_budget <= 0:
332
+ break
311
333
  if not pool_request.is_queued():
312
334
  continue
313
335
  origin = pool_request.request.url.origin
@@ -318,9 +340,13 @@ class ConnectionPool(RequestInterface):
318
340
  # 2. We can create a new connection to handle the request.
319
341
  # 3. We can close an idle connection and then create a new connection
320
342
  # to handle the request.
321
- for connection in available_connections:
343
+ for idx, connection in enumerate(available_connections):
322
344
  if connection.can_handle_request(origin):
323
345
  pool_request.assign_to_connection(connection)
346
+ if connection.is_idle() and not connection.can_multiplex():
347
+ # An idle HTTP/1.1 connection can only take this
348
+ # single request until it is released.
349
+ del available_connections[idx]
324
350
  break
325
351
  else:
326
352
  if new_connection_budget > 0:
@@ -495,6 +495,9 @@ class HTTP2Connection(ConnectionInterface):
495
495
  def is_idle(self) -> bool:
496
496
  return self._state == HTTPConnectionState.IDLE
497
497
 
498
+ def can_multiplex(self) -> bool:
499
+ return True
500
+
498
501
  def is_closed(self) -> bool:
499
502
  return self._state == HTTPConnectionState.CLOSED
500
503
 
@@ -140,6 +140,16 @@ class ConnectionInterface(RequestInterface):
140
140
  """
141
141
  raise NotImplementedError() # pragma: no cover
142
142
 
143
+ def can_multiplex(self) -> bool:
144
+ """
145
+ Return `True` if the connection can serve multiple requests
146
+ concurrently, such as an established HTTP/2 connection.
147
+
148
+ The default covers HTTP/1.1-style implementations, which serve a
149
+ single request at a time.
150
+ """
151
+ return False
152
+
143
153
  def is_closed(self) -> bool:
144
154
  """
145
155
  Return `True` if the connection has been closed.
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes