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

Commit bb2fd90

Browse files
committed
Merge branch 'ew/keepalive'
* ew/keepalive: http: use curl's tcp keepalive if available http: enable keepalive on TCP sockets
2 parents 2d99baa + 47ce115 commit bb2fd90

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

http.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,42 @@ static int has_cert_password(void)
260260
return 1;
261261
}
262262

263+
#if LIBCURL_VERSION_NUM >= 0x071900
264+
static void set_curl_keepalive(CURL *c)
265+
{
266+
curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
267+
}
268+
269+
#elif LIBCURL_VERSION_NUM >= 0x071000
270+
static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
271+
{
272+
int ka = 1;
273+
int rc;
274+
socklen_t len = (socklen_t)sizeof(ka);
275+
276+
if (type != CURLSOCKTYPE_IPCXN)
277+
return 0;
278+
279+
rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
280+
if (rc < 0)
281+
warning("unable to set SO_KEEPALIVE on socket %s",
282+
strerror(errno));
283+
284+
return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
285+
}
286+
287+
static void set_curl_keepalive(CURL *c)
288+
{
289+
curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
290+
}
291+
292+
#else
293+
static void set_curl_keepalive(CURL *c)
294+
{
295+
/* not supported on older curl versions */
296+
}
297+
#endif
298+
263299
static CURL *get_curl_handle(void)
264300
{
265301
CURL *result = curl_easy_init();
@@ -332,6 +368,8 @@ static CURL *get_curl_handle(void)
332368
curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
333369
}
334370

371+
set_curl_keepalive(result);
372+
335373
return result;
336374
}
337375

0 commit comments

Comments
 (0)