Skip to content

Commit 85878dd

Browse files
committed
Merge branch 'ew/keepalive'
* ew/keepalive: enable SO_KEEPALIVE for connected TCP sockets
2 parents 7f1068e + e47a858 commit 85878dd

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
@@ -151,6 +151,15 @@ static void get_host_and_port(char **host, const char **port)
151151
}
152152
}
153153

154+
static void enable_keepalive(int sockfd)
155+
{
156+
int ka = 1;
157+
158+
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka)) < 0)
159+
fprintf(stderr, "unable to set SO_KEEPALIVE on socket: %s\n",
160+
strerror(errno));
161+
}
162+
154163
#ifndef NO_IPV6
155164

156165
static const char *ai_name(const struct addrinfo *ai)
@@ -215,6 +224,8 @@ static int git_tcp_connect_sock(char *host, int flags)
215224
if (sockfd < 0)
216225
die("unable to connect to %s:\n%s", host, error_message.buf);
217226

227+
enable_keepalive(sockfd);
228+
218229
if (flags & CONNECT_VERBOSE)
219230
fprintf(stderr, "done.\n");
220231

@@ -288,6 +299,8 @@ static int git_tcp_connect_sock(char *host, int flags)
288299
if (sockfd < 0)
289300
die("unable to connect to %s:\n%s", host, error_message.buf);
290301

302+
enable_keepalive(sockfd);
303+
291304
if (flags & CONNECT_VERBOSE)
292305
fprintf(stderr, "done.\n");
293306

0 commit comments

Comments
 (0)