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

Use TCP_NODELAY on the server socket #3008

Merged
merged 3 commits into from May 16, 2024

Conversation

stefantalpalaru
Copy link
Contributor

@mathiascode
Copy link
Member

Sure, why not. We already have our own buffer, so Nagle's algorithm doesn't really benefit us anyway.

Could you set TCP_NODELAY on the peer sockets as well?

@@ -1214,6 +1214,9 @@ def _init_server_conn(self, msg_obj):
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, self.SOCKET_READ_BUFFER_SIZE)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, self.SOCKET_WRITE_BUFFER_SIZE)

if hasattr(socket, "TCP_NODELAY"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe TCP_NODELAY should be available on every OS we support. Do you know one that doesn't have it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not aware of any without it. I was just copying the safety checks in

if hasattr(socket, "SO_KEEPALIVE"):
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # pylint: disable=no-member
if hasattr(socket, "TCP_KEEPINTVL"):
server_socket.setsockopt(socket.IPPROTO_TCP,
socket.TCP_KEEPINTVL, interval) # pylint: disable=no-member
if hasattr(socket, "TCP_KEEPCNT"):
server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, count) # pylint: disable=no-member
if hasattr(socket, "TCP_KEEPIDLE"):
server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, idle) # pylint: disable=no-member
elif hasattr(socket, "TCP_KEEPALIVE"):
# macOS fallback
server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPALIVE, idle) # pylint: disable=no-member
elif hasattr(socket, "SIO_KEEPALIVE_VALS"):
# Windows fallback
# Probe count is set to 10 on a system level, and can't be modified.
# https://docs.microsoft.com/en-us/windows/win32/winsock/so-keepalive
server_socket.ioctl(
socket.SIO_KEEPALIVE_VALS, # pylint: disable=no-member
(
1,
idle * 1000,
interval * 1000
)
)
if hasattr(socket, "TCP_USER_TIMEOUT"):
server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_USER_TIMEOUT, timeout_seconds * 1000)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave the check out for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@slook slook May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think we should leave all the if hasattr(socket, "TCP_NODELAY"): checks out for now, because it is probably not required.

It can be added again if any testers report a Critical Error thown by the exception handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Check removed.

@stefantalpalaru
Copy link
Contributor Author

Could you set TCP_NODELAY on the peer sockets as well?

No. Those are UDP.

@mathiascode
Copy link
Member

Could you set TCP_NODELAY on the peer sockets as well?

No. Those are UDP.

The Soulseek protocol uses TCP, not UDP. Peer sockets are created here:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

and here:
incoming_sock, incoming_addr = sock.accept()

@stefantalpalaru
Copy link
Contributor Author

I stand corrected. Also added for peer sockets.

@mathiascode mathiascode merged commit 2c6ca87 into nicotine-plus:master May 16, 2024
18 of 28 checks passed
@stefantalpalaru stefantalpalaru deleted the tcpnodelay branch May 16, 2024 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants