Skip to content

Commit c9106c7

Browse files
committed
Add get_available_stream_capacity to conn interface
1 parent 72d2eb6 commit c9106c7

10 files changed

Lines changed: 52 additions & 4 deletions

File tree

httpcore/_async/connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ def is_closed(self) -> bool:
199199
return self._connect_failed
200200
return self._connection.is_closed()
201201

202+
def get_available_stream_capacity(self) -> int:
203+
if self._connection is None:
204+
return 1
205+
return self._connection.get_available_stream_capacity()
206+
202207
def info(self) -> str:
203208
if self._connection is None:
204209
return "CONNECTION FAILED" if self._connect_failed else "CONNECTING"

httpcore/_async/connection_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
try:
1717
from .http2 import AsyncHTTP2Connection
18-
except ImportError:
18+
except ImportError: # pragma: nocover
1919
# ImportError happens when the user installed httpcore without the optional http2 dependency
2020
AsyncHTTP2Connection = None # type: ignore[assignment, misc]
2121

@@ -303,7 +303,7 @@ def _assign_requests_to_connections(self) -> list[AsyncConnectionInterface]:
303303
# Available connections
304304
available_conns.append(conn)
305305
# Track HTTP/2 connection capacity
306-
if self._http2 and isinstance(conn, AsyncHTTP2Connection):
306+
if self._http2:
307307
# Get the actual available stream count from the connection
308308
http2_conn_stream_capacity[conn] = (
309309
conn.get_available_stream_capacity()

httpcore/_async/http11.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ def is_idle(self) -> bool:
291291
def is_closed(self) -> bool:
292292
return self._state == HTTPConnectionState.CLOSED
293293

294+
def get_available_stream_capacity(self) -> int:
295+
"""
296+
For HTTP/1.1, return 1 if the connection is idle (can accept a request),
297+
0 otherwise (connection is busy).
298+
"""
299+
return 1 if self._state == HTTPConnectionState.IDLE else 0
300+
294301
def info(self) -> str:
295302
origin = str(self._origin)
296303
return (

httpcore/_async/http_proxy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,5 +363,8 @@ def is_idle(self) -> bool:
363363
def is_closed(self) -> bool:
364364
return self._connection.is_closed()
365365

366+
def get_available_stream_capacity(self) -> int:
367+
return self._connection.get_available_stream_capacity()
368+
366369
def __repr__(self) -> str:
367370
return f"<{self.__class__.__name__} [{self.info()}]>"

httpcore/_async/interfaces.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,12 @@ def is_closed(self) -> bool:
135135
returned to the connection pool or not.
136136
"""
137137
raise NotImplementedError() # pragma: nocover
138+
139+
def get_available_stream_capacity(self) -> int:
140+
"""
141+
Return the number of additional streams that can be handled by this connection.
142+
143+
For HTTP/1.1 connections, this is 1 if the connection is idle, 0 otherwise.
144+
For HTTP/2 connections, this is the number of available concurrent streams.
145+
"""
146+
raise NotImplementedError() # pragma: nocover

httpcore/_sync/connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ def is_closed(self) -> bool:
199199
return self._connect_failed
200200
return self._connection.is_closed()
201201

202+
def get_available_stream_capacity(self) -> int:
203+
if self._connection is None:
204+
return 1
205+
return self._connection.get_available_stream_capacity()
206+
202207
def info(self) -> str:
203208
if self._connection is None:
204209
return "CONNECTION FAILED" if self._connect_failed else "CONNECTING"

httpcore/_sync/connection_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
try:
1717
from .http2 import HTTP2Connection
18-
except ImportError:
18+
except ImportError: # pragma: nocover
1919
# ImportError happens when the user installed httpcore without the optional http2 dependency
2020
HTTP2Connection = None # type: ignore[assignment, misc]
2121

@@ -303,7 +303,7 @@ def _assign_requests_to_connections(self) -> list[ConnectionInterface]:
303303
# Available connections
304304
available_conns.append(conn)
305305
# Track HTTP/2 connection capacity
306-
if self._http2 and isinstance(conn, HTTP2Connection):
306+
if self._http2:
307307
# Get the actual available stream count from the connection
308308
http2_conn_stream_capacity[conn] = (
309309
conn.get_available_stream_capacity()

httpcore/_sync/http11.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ def is_idle(self) -> bool:
291291
def is_closed(self) -> bool:
292292
return self._state == HTTPConnectionState.CLOSED
293293

294+
def get_available_stream_capacity(self) -> int:
295+
"""
296+
For HTTP/1.1, return 1 if the connection is idle (can accept a request),
297+
0 otherwise (connection is busy).
298+
"""
299+
return 1 if self._state == HTTPConnectionState.IDLE else 0
300+
294301
def info(self) -> str:
295302
origin = str(self._origin)
296303
return (

httpcore/_sync/http_proxy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,5 +363,8 @@ def is_idle(self) -> bool:
363363
def is_closed(self) -> bool:
364364
return self._connection.is_closed()
365365

366+
def get_available_stream_capacity(self) -> int:
367+
return self._connection.get_available_stream_capacity()
368+
366369
def __repr__(self) -> str:
367370
return f"<{self.__class__.__name__} [{self.info()}]>"

httpcore/_sync/interfaces.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,12 @@ def is_closed(self) -> bool:
135135
returned to the connection pool or not.
136136
"""
137137
raise NotImplementedError() # pragma: nocover
138+
139+
def get_available_stream_capacity(self) -> int:
140+
"""
141+
Return the number of additional streams that can be handled by this connection.
142+
143+
For HTTP/1.1 connections, this is 1 if the connection is idle, 0 otherwise.
144+
For HTTP/2 connections, this is the number of available concurrent streams.
145+
"""
146+
raise NotImplementedError() # pragma: nocover

0 commit comments

Comments
 (0)