Probe idle HTTP/1.1 sockets on reuse instead of on every pool pass
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 connections | main | This PR |
|---|---|---|
| 1 | 286 µs | 226 µs |
| 10 | 556 µs | 226 µs |
| 50 | 1633 µs | 268 µs |
| 100 | 2591 µs | 312 µs |
Behaviour changes
has_expired()on an HTTP/1.1 connection no longer returnsTruewhen 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 raisesConnectionNotAvailablewhen 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