Merge branch 'ew/keepalive'
authorJunio C Hamano <gitster@pobox.com>
Mon, 28 Oct 2013 17:43:24 +0000 (10:43 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 28 Oct 2013 17:43:24 +0000 (10:43 -0700)
* ew/keepalive:
http: use curl's tcp keepalive if available
http: enable keepalive on TCP sockets

http.c
diff --git a/http.c b/http.c
index f3e1439d58a00328ece38a6eead63309fe7f58ca..0ddb164a6f239789664c77f1dd97a31086fc71e3 100644 (file)
--- a/http.c
+++ b/http.c
@@ -260,6 +260,42 @@ static int has_cert_password(void)
        return 1;
 }
 
+#if LIBCURL_VERSION_NUM >= 0x071900
+static void set_curl_keepalive(CURL *c)
+{
+       curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
+}
+
+#elif LIBCURL_VERSION_NUM >= 0x071000
+static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
+{
+       int ka = 1;
+       int rc;
+       socklen_t len = (socklen_t)sizeof(ka);
+
+       if (type != CURLSOCKTYPE_IPCXN)
+               return 0;
+
+       rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
+       if (rc < 0)
+               warning("unable to set SO_KEEPALIVE on socket %s",
+                       strerror(errno));
+
+       return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
+}
+
+static void set_curl_keepalive(CURL *c)
+{
+       curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
+}
+
+#else
+static void set_curl_keepalive(CURL *c)
+{
+       /* not supported on older curl versions */
+}
+#endif
+
 static CURL *get_curl_handle(void)
 {
        CURL *result = curl_easy_init();
@@ -332,6 +368,8 @@ static CURL *get_curl_handle(void)
                curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
        }
 
+       set_curl_keepalive(result);
+
        return result;
 }