Skip to content

Lwt_io: options for network functions, client and server - #1061

Merged
raphael-proust merged 6 commits into
ocsigen:masterfrom
c-cube:simon/lwt-io-socket-options
Jul 22, 2025
Merged

raphael-proust merged 6 commits into
ocsigen:masterfrom
c-cube:simon/lwt-io-socket-options

Conversation

@c-cube

@c-cube c-cube commented Jul 10, 2025

Copy link
Copy Markdown
Collaborator

The idea is that we should be able to use the high level API and still have
basic functionality such as TCP_NODELAY being set automatically. For more
advanced cases, optional callbacks can be used to customize the socket FDs.

c-cube added 3 commits July 9, 2025 21:46
this way it's possible to use the high level API and still
control some specific parameters for the socket, such as timeout or
buffer size. TCP_NODELAY should be the default.
we should have TCP_NODELAY true by default on client sockets,
and we can now set options on both the listening socket
and each client socket.
this is more backward compatible
@c-cube

c-cube commented Jul 11, 2025

Copy link
Copy Markdown
Collaborator Author

I'm not sure why the tests fail on freeBSD.

@c-cube
c-cube marked this pull request as ready for review July 11, 2025 04:33
@c-cube

c-cube commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator Author

After some thoughts, I think TCP_NODELAY should still be set by default. In the super rare case where it must be unset, people can either specify the option or go to Lwt_unix; for the rest this will save perplexity over these mysterious 40ms that keep popping up.

@raphael-proust raphael-proust left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'll look at the freebsd CI failures in more details, but these are generally due to stricter process isolation on bsd than on linux.

Comment thread src/unix/lwt_io.mli Resolved
Comment thread src/unix/lwt_io.mli Resolved
c-cube and others added 2 commits July 16, 2025 22:07
@raphael-proust

Copy link
Copy Markdown
Collaborator

Nice, thanks for the contribution!

@raphael-proust
raphael-proust merged commit 968123e into ocsigen:master Jul 22, 2025
@dinosaure

Copy link
Copy Markdown
Member

Sorry for arriving late (and after the merge), but it is generally advisable to take a conservative approach when creating a socket and, perhaps, document such an option, which is an optimisation in itself.

The apparent optimisation provided by TCP_NODELAY is often quite specific to the fact that the tests/benchmarks are done on a local network. But when it comes to communicating with the Internet (where there are a variety of implementations of the TCP/IP stack), TCP_NODELAY can be counterproductive (especially with BSD systems that can send delayed ACKs for small packets). What is certain is that TCP_NODELAY changes the behaviour of the sender but not the reception (on the peer side, which may have a different implementation of the TCP/IP stack).

What is certain is that this type of optimisation is quite relative to the network you are on. It is also specific to what we want to do: some would recommend UDP rather than TCP+TCP_NODELAY, for example. This does not systematically invalidate the use of TCP_NODELAY, but neither does it systematically confirm the use of TCP_NODELAY. This is why the option still exists and why the Nagle algorithm is still applied by default.

I am not against TCP_NODELAY, but it would be more prudent to continue offering a socket that behaves like the one we could create with the Unix module and to offer, via the documentation and the option, a possible optimisation explaining the implications.

On another note, there is much more documentation on the Nagle algorithm as a bottleneck when writing than on congestion problems because TCP_NODELAY has been enabled. For a user, it might be more interesting to encounter one rather than the other (and discover that lwt has a subtly different way of creating sockets).

@c-cube

c-cube commented Jul 22, 2025 via email

Copy link
Copy Markdown
Collaborator Author

@dinosaure

dinosaure commented Jul 22, 2025 •

Copy link
Copy Markdown
Member

I disagree. Nagle is the wrong default in 99% of use cases,

I'm sorry, but it would be better to argue this assertion rather than say that I'm wrong...

And the problem is not whether TCP_NODELAY is better or worse, but rather to allow the user to identify what may be causing latency and/or congestion issues at the TCP/IP stack level. I reiterate that the online documentation is more geared towards: if you observe latency, you may need to use TCP_NODELAY rather than if you observe congestion, which may be because a library is adding the TCP_NODELAY flag.

The second point may be particularly difficult to understand compared to the first, hence the more conservative approach.

EDIT: And, at the very least, you should understand that the documentation as provided in this PR may not be sufficient to enable users to understand the behaviour of the TCP/IP stack when using lwt. This is perhaps more thoughtful than simply saying that I am wrong.

@hannesm

hannesm commented Jul 22, 2025 •

Copy link
Copy Markdown
Contributor

I understand that enabling or not TCP_NODELAY is controversial, as it is as well controversially discussed on the Internet. In the end, some programming languages (such as Go) enable TCP_NODELAY by default, others don't. I haven't seen an OS that enables TCP_NODELAY by default.

For what it is worth, I'd at least expect the default in Lwt_io being documented. At the moment, the docstring is:

      @param set_tcp_nodelay if true, [TCP_NODELAY] is set on the socket FD. This
        avoids a surprising 40ms delay in some situations.
        See for example https://brooker.co.za/blog/2024/05/09/nagle.html for why.

It may be worth to refer to (a) the default, (b) maybe Nagle's algorithm (and the nice wikipedia article https://en.wikipedia.org/wiki/Nagle%27s_algorithm -- instead of a random rant on the Internet.

Also, this should then be highlighted in the Changes of a potential new lwt release.

@c-cube

c-cube commented Jul 22, 2025 via email

Copy link
Copy Markdown
Collaborator Author

@c-cube

c-cube commented Jul 22, 2025

Copy link
Copy Markdown
Collaborator Author

It may be worth to refer to (a) the default, (b) maybe Nagle's algorithm (and the nice wikipedia article https://en.wikipedia.org/wiki/Nagle%27s_algorithm -- instead of a random rant on the Internet.

Started in #1065 , good point!

@c-cube
c-cube deleted the simon/lwt-io-socket-options branch July 22, 2025 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants