Skip to content
Myr

Release max_connections for keepalive connections when closing the connection pool (#721)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Florimond Manca · committed by Tom Christie · parent 0ed0314 · Browse files · 2 files+26 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+26 added−0 removed

  • tests:+25 added−0 removed
  • other:+1 added−0 removed

2 files+26 added−0 removed

modified:httpx/dispatch/connection_pool.py+1 added
204 unmodified lines
205 self.is_closed = True206 connections = list(self.keepalive_connections)207 self.keepalive_connections.clear()208 for connection in connections:209 self.max_connections.release()210 await connection.close()211212 def pop_connection(self, origin: Origin) -> typing.Optional[HTTPConnection]:213 connection = self.active_connections.pop_by_origin(origin, http2_only=True)
8 unmodified lines
modified:tests/dispatch/test_connection_pools.py+25 added
219 unmodified lines
220 await restart(server)221222 response = await http.request("GET", server.url)223 assert response.status_code == 200224225226@pytest.mark.usefixtures("async_environment")227async def test_connection_pool_closed_close_keepalive_and_free_semaphore(server):228 """229 Closing the connection pool should close remaining keepalive connections and230 release the max_connections semaphore.231 """232 http = ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1))233234 async with http:235 response = await http.request("GET", server.url)236 await response.aread()237 assert response.status_code == 200238 assert len(http.keepalive_connections) == 1239240 assert len(http.keepalive_connections) == 0241242 # Perform a second round of requests to make sure the max_connections semaphore243 # was released properly.244245 async with http:246 response = await http.request("GET", server.url)247 await response.aread()248 assert response.status_code == 200