Probe idle HTTP/1.1 sockets on reuse instead of on every pool pass

Merged
KL
2d ago
+143-32

Summary

Every request start and response close runs _assign_requests_to_connections(), which called has_expired() on each idle connection. For HTTP/1.1 that polled the socket to detect a server disconnect, so the cost of one request grew with the number of idle connections in the pool.

The socket is now checked in HTTP11Connection.handle_request(), when an idle connection is about to be reused. If the server has disconnected, the connection is closed and ConnectionNotAvailable is raised, which the pool already handles by assigning the request to another connection. has_expired() only checks the keep-alive expiry, as it already does for HTTP/2.

Time per request with the script from the issue (macOS, Python 3.14, AnyIO backend):

Idle connectionsmainThis PR
1286 µs226 µs
10556 µs226 µs
501633 µs268 µs
1002591 µs312 µs

Behaviour changes

  • has_expired() on an HTTP/1.1 connection no longer returns True when the server has sent an EOF.
  • An idle connection that the server closed stays in the pool until it is reused, its keep-alive expires, or it is evicted for capacity. Before, any pool activity removed it.
  • HTTPConnection.handle_request() used without a pool raises ConnectionNotAvailable when the server has closed the idle connection, instead of failing later while reading the response.

Closes #1288.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

Session: pi --session 01a1e3ea-ab1f-71fe-9b2c-85e666e73d75