Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow socket options to be configured. #662

Closed
tomchristie opened this issue Mar 28, 2023 · 1 comment · Fixed by #668
Closed

Allow socket options to be configured. #662

tomchristie opened this issue Mar 28, 2023 · 1 comment · Fixed by #668
Labels
enhancement New feature or request

Comments

@tomchristie
Copy link
Member

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...

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.

@tomchristie tomchristie added the enhancement New feature or request label Mar 28, 2023
@tomchristie
Copy link
Member Author

We'd also want to then push this configuration through to httpx.HTTPTransport.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant