Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/unix/lwt_io.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1553,7 +1553,18 @@ let close_socket fd =
(fun () ->
Lwt_unix.close fd)

let open_connection ?fd ?(set_tcp_nodelay=true) ?(prepare_fd=ignore) ?in_buffer ?out_buffer sockaddr =
let optionally_set_tcp_nodelay set_tcp_nodelay fd =
match set_tcp_nodelay with
| Some false -> ()
| Some true -> Lwt_unix.setsockopt fd Unix.TCP_NODELAY true
| None ->
(* if not explicitly requested, we attempt to set nodelay but we don't fail in case of error *)
try
Lwt_unix.setsockopt fd Unix.TCP_NODELAY true
with
Unix.Unix_error (Unix.EOPNOTSUPP, _, _) -> ()

let open_connection ?fd ?set_tcp_nodelay ?(prepare_fd=ignore) ?in_buffer ?out_buffer sockaddr =
let fd =
match fd with
| None ->
Expand All @@ -1562,7 +1573,7 @@ let open_connection ?fd ?(set_tcp_nodelay=true) ?(prepare_fd=ignore) ?in_buffer
fd
in

if set_tcp_nodelay then Lwt_unix.setsockopt fd Unix.TCP_NODELAY true;
optionally_set_tcp_nodelay set_tcp_nodelay fd;
prepare_fd fd;

let close = lazy (close_socket fd) in
Expand Down Expand Up @@ -1609,7 +1620,7 @@ let shutdown_server_deprecated server =
let establish_server_generic
bind_function
?fd:preexisting_socket_for_listening
?(set_tcp_nodelay=true)
?set_tcp_nodelay
?(prepare_listening_fd=ignore)
?(prepare_client_fd=ignore)
?(backlog = Lwt_unix.somaxconn () [@ocaml.warning "-3"])
Expand Down Expand Up @@ -1658,7 +1669,7 @@ let establish_server_generic
with Invalid_argument _ -> ()
end;

if set_tcp_nodelay then Lwt_unix.setsockopt client_socket Unix.TCP_NODELAY true;
optionally_set_tcp_nodelay set_tcp_nodelay client_socket;
prepare_client_fd client_socket;
connection_handler_callback client_address client_socket;

Expand Down
5 changes: 4 additions & 1 deletion src/unix/lwt_io.mli
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ val open_connection :
Nagle's algorithm (https://en.wikipedia.org/wiki/Nagle%27s_algorithm).
See for example https://brooker.co.za/blog/2024/05/09/nagle.html for why.

The default value is [true].
The default value is to attempt to set [TCP_NODELAY] but ignore [EOPNOTSUPP].

@param prepare_fd is a custom callback that can be used to modify the socket FD
before it is turned into high level channels.
Expand Down Expand Up @@ -695,6 +695,9 @@ val establish_server_with_client_address :
The channels are closed automatically when the promise returned by [f]
resolves. To avoid this behavior, pass [~no_close:true].

See {!open_connection} for more details about [set_tcp_nodelay]
and [prepare_*_fd].

@since 3.1.0 *)

val shutdown_server : server -> unit Lwt.t
Expand Down
Loading