Skip to content

Commit 33f3c9d

Browse files
committed
Set TCP_NODELAY to avoid Nagle's algorithm
Should probably buffer up messages instead, but this solves the immediate problem.
1 parent c21d3f9 commit 33f3c9d

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

unix/network.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ let addr_of_host host =
7171
else
7272
Eio_unix.Net.Ipaddr.of_unix addr.Unix.h_addr_list.(0)
7373

74+
let try_set_nodelay socket =
75+
try Unix.setsockopt socket Unix.TCP_NODELAY true
76+
with Unix.Unix_error (EOPNOTSUPP, _, _) -> () (* Probably a Unix-domain socket *)
77+
7478
let connect net ~sw ~secret_key (addr, auth) =
7579
let eio_addr =
7680
match addr with
@@ -89,6 +93,7 @@ let connect net ~sw ~secret_key (addr, auth) =
8993
let socket = Eio_unix.Resource.fd_opt socket |> Option.get in
9094
Eio_unix.Fd.use_exn "keep-alive" socket @@ fun socket ->
9195
Unix.setsockopt socket Unix.SO_KEEPALIVE true;
96+
try_set_nodelay socket;
9297
Keepalive.try_set_idle socket 60
9398
end;
9499
Tls_wrapper.connect_as_client socket secret_key auth
@@ -97,6 +102,8 @@ let connect net ~sw ~secret_key (addr, auth) =
97102
error "@[<v2>Network connection for %a failed:@,%a@]" Location.pp addr Fmt.exn ex
98103

99104
let accept_connection ~secret_key flow =
105+
Eio_unix.Resource.fd_opt flow
106+
|> Option.iter (fun fd -> Eio_unix.Fd.use_exn "TCP_NODELAY" fd try_set_nodelay);
100107
Tls_wrapper.connect_as_server flow secret_key
101108

102109
let v t = (t :> [`Generic] Eio.Net.ty r)

0 commit comments

Comments
 (0)