Skip to content

Commit 2cb1ff9

Browse files
committed
Merge branch 'ew/keepalive' into maint
* ew/keepalive: enable SO_KEEPALIVE for connected TCP sockets
2 parents 4742949 + e47a858 commit 2cb1ff9

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

connect.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ static void get_host_and_port(char **host, const char **port)
175175
}
176176
}
177177

178+
static void enable_keepalive(int sockfd)
179+
{
180+
int ka = 1;
181+
182+
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
183+
fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
184+
strerror(errno));
185+
}
186+
178187
#ifndef NO_IPV6
179188

180189
static const char *ai_name(const struct addrinfo *ai)
@@ -239,6 +248,8 @@ static int git_tcp_connect_sock(char *host, int flags)
239248
if (sockfd < 0)
240249
die("unable to connect to %s:\n%s", host, error_message.buf);
241250

251+
enable_keepalive(sockfd);
252+
242253
if (flags & CONNECT_VERBOSE)
243254
fprintf(stderr, "done.\n");
244255

@@ -312,6 +323,8 @@ static int git_tcp_connect_sock(char *host, int flags)
312323
if (sockfd < 0)
313324
die("unable to connect to %s:\n%s", host, error_message.buf);
314325

326+
enable_keepalive(sockfd);
327+
315328
if (flags & CONNECT_VERBOSE)
316329
fprintf(stderr, "done.\n");
317330

0 commit comments

Comments
 (0)