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...
| defconnect_tcp( |
| self, |
| host: str, |
| port: int, |
| timeout: typing.Optional[float] =None, |
| local_address: typing.Optional[str] =None, |
| ) ->NetworkStream: |
| address= (host, port) |
| source_address=Noneiflocal_addressisNoneelse (local_address, 0) |
| exc_map: ExceptionMapping= { |
| socket.timeout: ConnectTimeout, |
| OSError: ConnectError, |
| } |
| withmap_exceptions(exc_map): |
| sock=socket.create_connection( |
| address, timeout, source_address=source_address |
| ) |
| returnSyncStream(sock) |
...which we would adapt to something like this...
defconnect_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=Noneiflocal_addressisNoneelse (local_address, 0) exc_map: ExceptionMapping= { socket.timeout: ConnectTimeout, OSError: ConnectError, }
withmap_exceptions(exc_map): sock=socket.create_connection( address, timeout, source_address=source_address )
# See https://docs.python.org/3/library/socket.html#socket.socket.setsockoptforoptioninsocket_options:
sock.setsockopt(*option)
returnSyncStream(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.
We should add a
socket_optionsconfiguration to theConnectionPoolandHTTPProxyclasses.This would need to be passed through to the network backends
connect_tcp(...)andconnect_unix_socket()methods.For example the sync case, here...
httpcore/httpcore/backends/sync.py
Lines 80 to 97 in 21450a7
...which we would adapt to something like this...
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.