Repository navigation
Allow socket options to be configured. #662
Copy link
Copy link
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
We should add a socket_options configuration to the ConnectionPool and HTTPProxy classes.
This would need to be passed through to the network backends connect_tcp(...) and connect_unix_socket() methods.
For example the sync case, here...
httpcore/httpcore/backends/sync.py
Lines 80 to 97 in 21450a7
| def connect_tcp( | |
| self, | |
| host: str, | |
| port: int, | |
| timeout: typing.Optional[float] = None, | |
| local_address: typing.Optional[str] = None, | |
| ) -> NetworkStream: | |
| address = (host, port) | |
| source_address = None if local_address is None else (local_address, 0) | |
| exc_map: ExceptionMapping = { | |
| socket.timeout: ConnectTimeout, | |
| OSError: ConnectError, | |
| } | |
| with map_exceptions(exc_map): | |
| sock = socket.create_connection( | |
| address, timeout, source_address=source_address | |
| ) | |
| return SyncStream(sock) |
...which we would adapt to something like this...
def connect_tcp(
self,
host: str,
port: int,
timeout: typing.Optional[float] = None,
local_address: typing.Optional[str] = None,
socket_options: typing.Sequence[typing.Tuple[int, int, typing.Union[int, bytes]]] = ()
) -> NetworkStream:
address = (host, port)
source_address = None if local_address is None else (local_address, 0)
exc_map: ExceptionMapping = {
socket.timeout: ConnectTimeout,
OSError: ConnectError,
}
with map_exceptions(exc_map):
sock = socket.create_connection(
address, timeout, source_address=source_address
)
# See https://docs.python.org/3/library/socket.html#socket.socket.setsockopt
for option in socket_options:
sock.setsockopt(*option)
return SyncStream(sock) This would resolve #651 and encode/httpx#2635
We could also consider changing our defaults for TCP_NODELAY, but we should consider that option as a follow-up.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Activity
lovelydinosaur commentedon Mar 28, 2023
We'd also want to then push this configuration through to
httpx.HTTPTransport.