Skip to content

Commit 4e711ac

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 c95f619 commit 4e711ac

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
@@ -58,6 +58,10 @@ let error fmt =
5858

5959
let parse_third_party_cap_id _ = `Two_party_only
6060

61+
let try_set_nodelay socket =
62+
try Unix.setsockopt socket Unix.TCP_NODELAY true
63+
with Unix.Unix_error (EOPNOTSUPP, _, _) -> () (* Probably a Unix-domain socket *)
64+
6165
let connect net ~sw ~secret_key (addr, auth) =
6266
let eio_addr =
6367
match addr with
@@ -76,6 +80,7 @@ let connect net ~sw ~secret_key (addr, auth) =
7680
let socket = Eio_unix.Resource.fd_opt socket |> Option.get in
7781
Eio_unix.Fd.use_exn "keep-alive" socket @@ fun socket ->
7882
Unix.setsockopt socket Unix.SO_KEEPALIVE true;
83+
try_set_nodelay socket;
7984
Keepalive.try_set_idle socket 60
8085
end;
8186
Tls_wrapper.connect_as_client socket secret_key auth
@@ -84,6 +89,8 @@ let connect net ~sw ~secret_key (addr, auth) =
8489
error "@[<v2>Network connection for %a failed:@,%a@]" Location.pp addr Fmt.exn ex
8590

8691
let accept_connection ~secret_key flow =
92+
Eio_unix.Resource.fd_opt flow
93+
|> Option.iter (fun fd -> Eio_unix.Fd.use_exn "TCP_NODELAY" fd try_set_nodelay);
8794
Tls_wrapper.connect_as_server flow secret_key
8895

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

0 commit comments

Comments
 (0)