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

Expose setsockopt in public API #39

Closed
standy66 opened this issue Feb 2, 2019 · 7 comments
Closed

Expose setsockopt in public API #39

standy66 opened this issue Feb 2, 2019 · 7 comments
Labels
enhancement New feature or request

Comments

@standy66
Copy link
Contributor

standy66 commented Feb 2, 2019

This would be useful for setting up TCP keepalive and TCP_NODELAY flags. Right now the only option is to assume every SocketStream is anyio._networking.SocketStream and obtain raw socket via _socket._raw_socket

@agronholm agronholm added the enhancement New feature or request label Feb 4, 2019
@agronholm
Copy link
Owner

Are you intent on disabling TCP_NODELAY for your sockets?

@standy66
Copy link
Contributor Author

standy66 commented Feb 6, 2019

I want to be able to execute code similar to the following:

socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
if hasattr(socket, "TCP_KEEPIDLE"):
    socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 300)
elif is_darwin():
    # Darwin specific option
    # Value extracted from XNU source code
    # https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/bsd/netinet/tcp.h#L207
    TCP_KEEPALIVE = 16
    socket.setsockopt(socket.IPPROTO_TCP, TCP_KEEPALIVE, 300)
socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 30)
socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

@agronholm
Copy link
Owner

Yes, I get the use case for setsockopt(). I was merely wondering whether or not you knew that TCP_NODELAY is set for socket streams by default on anyio.

@agronholm
Copy link
Owner

I implemented getsockopt() and setsockopt() in SocketStream but now I keep wondering if I should expose the raw socket instead. After all, the socket object has a huge amount of low level functionality but I'm uncertain whether it should be exposed or not.

@standy66
Copy link
Contributor Author

Did you test it?
I'm getting TypeError: setsockopt() takes exactly 3 arguments (4 given) when calling socket_stream.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)

socket.SocketType.setsockopt is a weird built-in method, it does not seem to work when you manually pass optlen=None

@agronholm
Copy link
Owner

It seems that I only tested getsockopt() but forgot about setsockopt(). I will fix this tomorrow.

@agronholm
Copy link
Owner

Fixed in b383275.

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

No branches or pull requests

2 participants