Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 47ce115

Browse files
peffgitster
authored andcommitted
http: use curl's tcp keepalive if available
Commit a15d069 taught git to use curl's SOCKOPTFUNCTION hook to turn on TCP keepalives. However, modern versions of curl have a TCP_KEEPALIVE option, which can do this for us. As an added bonus, the curl code knows how to turn on keepalive for a much wider variety of platforms. The only downside to using this option is that not everybody has a new enough curl. Let's split our keepalive options into three conditionals: 1. With curl 7.25.0 and newer, we rely on curl to do it right. 2. With older curl that still knows SOCKOPTFUNCTION, we use the code from a15d069. 3. Otherwise, we are out of luck, and the call is a no-op. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a15d069 commit 47ce115

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

http.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,13 @@ static int has_cert_password(void)
233233
return 0;
234234
}
235235

236-
/* curl 7.25.0 has CURLOPT_TCP_KEEPALIVE, too, but we support older curl */
236+
#if LIBCURL_VERSION_NUM >= 0x071900
237+
static void set_curl_keepalive(CURL *c)
238+
{
239+
curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
240+
}
241+
242+
#elif LIBCURL_VERSION_NUM >= 0x071000
237243
static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
238244
{
239245
int ka = 1;
@@ -251,6 +257,18 @@ static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
251257
return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
252258
}
253259

260+
static void set_curl_keepalive(CURL *c)
261+
{
262+
curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
263+
}
264+
265+
#else
266+
static void set_curl_keepalive(CURL *c)
267+
{
268+
/* not supported on older curl versions */
269+
}
270+
#endif
271+
254272
static CURL *get_curl_handle(void)
255273
{
256274
CURL *result = curl_easy_init();
@@ -316,9 +334,7 @@ static CURL *get_curl_handle(void)
316334
if (curl_http_proxy)
317335
curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
318336

319-
#if LIBCURL_VERSION_NUM >= 0x071000
320-
curl_easy_setopt(result, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
321-
#endif
337+
set_curl_keepalive(result);
322338

323339
return result;
324340
}

0 commit comments

Comments
 (0)