From dc1a3fff5621e9e968f64a7795e56c7087927567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Thir=C3=A9?= Date: Tue, 18 Feb 2025 15:26:11 +0100 Subject: [PATCH] Octez/P2P: Add socket option `KEEP_ALIVE` for outgoing connections --- src/lib_p2p/p2p_fd.ml | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/lib_p2p/p2p_fd.ml b/src/lib_p2p/p2p_fd.ml index 56f39eadff06..bf8e078c0ef0 100644 --- a/src/lib_p2p/p2p_fd.ml +++ b/src/lib_p2p/p2p_fd.ml @@ -158,7 +158,16 @@ let string_of_sockaddr addr = let id t = t.id -let raw_socket () = Lwt_unix.socket ~cloexec:true PF_INET6 SOCK_STREAM 0 +let raw_socket () = + let sock = Lwt_unix.socket ~cloexec:true PF_INET6 SOCK_STREAM 0 in + (* By setting [SO_KEEPALIVE] to [true], the socket is configured to send + periodic keep-alive probes to verify that the connection is still + active. + + It reset (send TCP RST message and close) if the peer is + unresponsive. *) + Lwt_unix.(setsockopt sock SO_KEEPALIVE true) ; + sock let socket () = create (raw_socket ()) @@ -170,14 +179,6 @@ let create_listening_socket ?(reuse_port = false) ~backlog let sock = raw_socket () in (if reuse_port then Lwt_unix.(setsockopt sock SO_REUSEPORT true)) ; Lwt_unix.(setsockopt sock SO_REUSEADDR true) ; - - (* By setting [SO_KEEPALIVE] to [true], the socket is configured to send - periodic keep-alive probes to verify that the connection is still - active. - - It reset (send TCP RST message and close) if the peer is - unresponsive. *) - Lwt_unix.(setsockopt sock SO_KEEPALIVE true) ; let*! () = Lwt_unix.bind sock -- GitLab