TCPImagePuller: add OS-level TCP keep-alive

This commit is contained in:
2026-06-25 20:44:42 +02:00
parent 4462f0998d
commit 884e831359
+14
View File
@@ -6,6 +6,7 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <cerrno>
#include <limits>
@@ -227,6 +228,19 @@ bool TCPImagePuller::EnsureConnected() {
if (receive_buffer_size)
setsockopt(new_fd, SOL_SOCKET, SO_RCVBUF, &receive_buffer_size.value(), sizeof(int32_t));
// OS-level TCP keep-alive: detect a silently-dead pusher (no RST, e.g. crash or
// network partition) even while recv() is blocked in a long inter-image gap. Mirror
// the pusher's settings so detection is symmetric (~60s) and stays well clear of the
// 250 ms app-level BUSY heartbeat.
int one = 1;
setsockopt(new_fd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
int idle = 30;
int intvl = 10;
int cnt = 3;
setsockopt(new_fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
setsockopt(new_fd, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl));
setsockopt(new_fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
if (connect(new_fd, ai->ai_addr, ai->ai_addrlen) == 0)
break;