git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] http: enable keepalive on TCP sockets
@ 2013-10-12 22:29 Eric Wong
  2013-10-13  9:44 ` Daniel Stenberg
  0 siblings, 1 reply; 9+ messages in thread
From: Eric Wong @ 2013-10-12 22:29 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

This is a follow up to commit e47a8583a20256851e7fc882233e3bd5bf33dc6e
(enable SO_KEEPALIVE for connected TCP sockets).

Sockets may never receive notification of some link errors,
causing "git fetch" or similar processes to hang forever.
Enabling keepalive messages allows hung processes to error out
after a few minutes/hours depending on the keepalive settings of
the system.

I noticed this problem with some non-interactive cronjobs getting
hung when talking to HTTP servers.

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 http.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/http.c b/http.c
index f3e1439..5834c9b 100644
--- a/http.c
+++ b/http.c
@@ -260,6 +260,24 @@ static int has_cert_password(void)
 	return 1;
 }
 
+/* curl 7.25.0 has CURLOPT_TCP_KEEPALIVE, too, but we support older curl */
+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 CURL *get_curl_handle(void)
 {
 	CURL *result = curl_easy_init();
@@ -332,6 +350,10 @@ static CURL *get_curl_handle(void)
 		curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
 	}
 
+#if LIBCURL_VERSION_NUM >= 0x071000
+	curl_easy_setopt(result, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
+#endif
+
 	return result;
 }
 
-- 
Eric Wong

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-10-15  6:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-12 22:29 [PATCH] http: enable keepalive on TCP sockets Eric Wong
2013-10-13  9:44 ` Daniel Stenberg
2013-10-14  5:27   ` Eric Wong
2013-10-14 21:40     ` Jeff King
2013-10-14 23:38       ` Eric Wong
2013-10-15  0:06         ` [PATCH] http: use curl's tcp keepalive if available Jeff King
2013-10-15  4:58           ` Eric Wong
2013-10-15  5:00             ` Jeff King
2013-10-15  6:03               ` Eric Wong

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).