From: Azat Khuzhin Date: Wed, 12 Nov 2014 17:23:46 +0000 (+0300) Subject: http: use IP address that we got before (if any) during retrying X-Git-Tag: release-2.1.6-beta~90^2~70^2~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=54c887d8231b5778b2b76ddd6dce263422303ae3;p=libevent http: use IP address that we got before (if any) during retrying Before this patch every time we are retrying our request we resolve domain, but we could optimize this (since UDP is slow) by using cached conn_address value, so do this. --- diff --git a/http.c b/http.c index ad5e7612..500e3e2a 100644 --- a/http.c +++ b/http.c @@ -2418,6 +2418,9 @@ int evhttp_connection_connect_(struct evhttp_connection *evcon) { int old_state = evcon->state; + const char *address = evcon->address; + const struct sockaddr *sa = evhttp_connection_get_addr(evcon); + int ret; if (evcon->state == EVCON_CONNECTING) return (0); @@ -2458,8 +2461,20 @@ evhttp_connection_connect_(struct evhttp_connection *evcon) evcon->state = EVCON_CONNECTING; - if (bufferevent_socket_connect_hostname(evcon->bufev, evcon->dns_base, - evcon->ai_family, evcon->address, evcon->port) < 0) { + if (sa && (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)) { + int socklen; + if (sa->sa_family == AF_INET) { + socklen = sizeof(struct sockaddr_in); + } else if (sa->sa_family == AF_INET6) { + socklen = sizeof(struct sockaddr_in6); + } + ret = bufferevent_socket_connect(evcon->bufev, sa, socklen); + } else { + ret = bufferevent_socket_connect_hostname(evcon->bufev, + evcon->dns_base, evcon->ai_family, address, evcon->port); + } + + if (ret < 0) { evcon->state = old_state; event_sock_warn(evcon->fd, "%s: connection to \"%s\" failed", __func__, evcon->address);