Connections
TODO
Reference
httpcorexyz.HTTPConnection
get_available_stream_capacity(self)
Return the number of additional streams that can be handled by this connection.
For HTTP/1.1 connections, this is 1 if the connection is idle, 0 otherwise. For HTTP/2 connections, this is the number of available concurrent streams.
Source code in httpcorexyz/__init__.py
def get_available_stream_capacity(self) -> int:
if self._connection is None:
return 1
return self._connection.get_available_stream_capacity()
has_expired(self)
Return True if the connection is in a state where it should be closed.
This either means that the connection is idle and it has passed the expiry time on its keep-alive, or that server has sent an EOF.
Source code in httpcorexyz/__init__.py
def has_expired(self) -> bool:
if self._connection is None:
return self._connect_failed
return self._connection.has_expired()
is_available(self)
Return True if the connection is currently able to accept an
outgoing request.
An HTTP/1.1 connection will only be available if it is currently idle.
An HTTP/2 connection will be available so long as the stream ID space is not yet exhausted, and the connection is not in an error state.
While the connection is being established we may not yet know if it is going
to result in an HTTP/1.1 or HTTP/2 connection. The connection should be
treated as being available, but might ultimately raise NewConnectionRequired
required exceptions if multiple requests are attempted over a connection
that ends up being established as HTTP/1.1.
Source code in httpcorexyz/__init__.py
def is_available(self) -> bool:
if self._connection is None:
# If HTTP/2 support is enabled, and the resulting connection could
# end up as HTTP/2 then we should indicate the connection as being
# available to service multiple requests.
return (
self._http2
and (self._origin.scheme == b"https" or not self._http1)
and not self._connect_failed
)
return self._connection.is_available()
is_closed(self)
Return True if the connection has been closed.
Used when a response is closed to determine if the connection may be returned to the connection pool or not.
Source code in httpcorexyz/__init__.py
def is_closed(self) -> bool:
if self._connection is None:
return self._connect_failed
return self._connection.is_closed()
is_idle(self)
Return True if the connection is currently idle.
Source code in httpcorexyz/__init__.py
def is_idle(self) -> bool:
if self._connection is None:
return self._connect_failed
return self._connection.is_idle()
httpcorexyz.HTTP11Connection
get_available_stream_capacity(self)
Return the number of additional streams that can be handled by this connection.
For HTTP/1.1 connections, this is 1 if the connection is idle, 0 otherwise. For HTTP/2 connections, this is the number of available concurrent streams.
Source code in httpcorexyz/__init__.py
def get_available_stream_capacity(self) -> int:
return 1 if self._state == HTTPConnectionState.IDLE else 0
has_expired(self)
Return True if the connection is in a state where it should be closed.
This either means that the connection is idle and it has passed the expiry time on its keep-alive, or that server has sent an EOF.
Source code in httpcorexyz/__init__.py
def has_expired(self) -> bool:
now = time.monotonic()
keepalive_expired = self._expire_at is not None and now > self._expire_at
# If the HTTP connection is idle but the socket is readable, then the
# only valid state is that the socket is about to return b"", indicating
# a server-initiated disconnect.
server_disconnected = (
self._state == HTTPConnectionState.IDLE
and self._network_stream.get_extra_info("is_readable")
)
return keepalive_expired or server_disconnected
is_available(self)
Return True if the connection is currently able to accept an
outgoing request.
An HTTP/1.1 connection will only be available if it is currently idle.
An HTTP/2 connection will be available so long as the stream ID space is not yet exhausted, and the connection is not in an error state.
While the connection is being established we may not yet know if it is going
to result in an HTTP/1.1 or HTTP/2 connection. The connection should be
treated as being available, but might ultimately raise NewConnectionRequired
required exceptions if multiple requests are attempted over a connection
that ends up being established as HTTP/1.1.
Source code in httpcorexyz/__init__.py
def is_available(self) -> bool:
# Note that HTTP/1.1 connections in the "NEW" state are not treated as
# being "available". The control flow which created the connection will
# be able to send an outgoing request, but the connection will not be
# acquired from the connection pool for any other request.
return self._state == HTTPConnectionState.IDLE
is_closed(self)
Return True if the connection has been closed.
Used when a response is closed to determine if the connection may be returned to the connection pool or not.
Source code in httpcorexyz/__init__.py
def is_closed(self) -> bool:
return self._state == HTTPConnectionState.CLOSED
is_idle(self)
Return True if the connection is currently idle.
Source code in httpcorexyz/__init__.py
def is_idle(self) -> bool:
return self._state == HTTPConnectionState.IDLE
httpcorexyz.HTTP2Connection
get_available_stream_capacity(self)
Return the number of additional streams that can be handled by this connection.
For HTTP/1.1 connections, this is 1 if the connection is idle, 0 otherwise. For HTTP/2 connections, this is the number of available concurrent streams.
Source code in httpcorexyz/__init__.py
def get_available_stream_capacity(self) -> int:
return self._max_streams - self._concurrent_streams
has_expired(self)
Return True if the connection is in a state where it should be closed.
This either means that the connection is idle and it has passed the expiry time on its keep-alive, or that server has sent an EOF.
Source code in httpcorexyz/__init__.py
def has_expired(self) -> bool:
now = time.monotonic()
return self._expire_at is not None and now > self._expire_at
is_available(self)
Return True if the connection is currently able to accept an
outgoing request.
An HTTP/1.1 connection will only be available if it is currently idle.
An HTTP/2 connection will be available so long as the stream ID space is not yet exhausted, and the connection is not in an error state.
While the connection is being established we may not yet know if it is going
to result in an HTTP/1.1 or HTTP/2 connection. The connection should be
treated as being available, but might ultimately raise NewConnectionRequired
required exceptions if multiple requests are attempted over a connection
that ends up being established as HTTP/1.1.
Source code in httpcorexyz/__init__.py
def is_available(self) -> bool:
return (
self._state != HTTPConnectionState.CLOSED
and not self._connection_error
and not self._used_all_stream_ids
and not (
self._h2_state.state_machine.state
== h2.connection.ConnectionState.CLOSED
)
)
is_closed(self)
Return True if the connection has been closed.
Used when a response is closed to determine if the connection may be returned to the connection pool or not.
Source code in httpcorexyz/__init__.py
def is_closed(self) -> bool:
return self._state == HTTPConnectionState.CLOSED
is_idle(self)
Return True if the connection is currently idle.
Source code in httpcorexyz/__init__.py
def is_idle(self) -> bool:
return self._state == HTTPConnectionState.IDLE