Skip to content

Commit

Permalink
move Curl_share_lock and ref counting into Curl_fetch_addr
Browse files Browse the repository at this point in the history
  • Loading branch information
tvbuehler authored and bagder committed Apr 3, 2015
1 parent 0db8319 commit b4be97f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
72 changes: 52 additions & 20 deletions lib/hostip.c
Expand Up @@ -311,19 +311,9 @@ remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
sigjmp_buf curl_jmpenv;
#endif

/*
* Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
*
* Curl_resolv() checks initially and multi_runsingle() checks each time
* it discovers the handle in the state WAITRESOLVE whether the hostname
* has already been resolved and the address has already been stored in
* the DNS cache. This short circuits waiting for a lot of pending
* lookups for the same hostname requested by different handles.
*
* Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
*/
struct Curl_dns_entry *
Curl_fetch_addr(struct connectdata *conn,
/* lookup address, returns entry if found and not stale */
static struct Curl_dns_entry *
fetch_addr(struct connectdata *conn,
const char *hostname,
int port)
{
Expand All @@ -344,15 +334,57 @@ Curl_fetch_addr(struct connectdata *conn,
/* See if its already in our dns cache */
dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);

if(dns && (data->set.dns_cache_timeout != -1)) {
/* See whether the returned entry is stale. Done before we release lock */
struct hostcache_prune_data user;

time(&user.now);
user.cache_timeout = data->set.dns_cache_timeout;

if(hostcache_timestamp_remove(&user, dns)) {
infof(data, "Hostname in DNS cache was stale, zapped\n");
dns = NULL; /* the memory deallocation is being handled by the hash */
Curl_hash_delete(data->dns.hostcache, entry_id, entry_len+1);
}
}

/* free the allocated entry_id again */
free(entry_id);

/* See whether the returned entry is stale. Done before we release lock */
stale = remove_entry_if_stale(data, dns);
if(stale) {
infof(data, "Hostname in DNS cache was stale, zapped\n");
dns = NULL; /* the memory deallocation is being handled by the hash */
}
return dns;
}

/*
* Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
*
* Curl_resolv() checks initially and multi_runsingle() checks each time
* it discovers the handle in the state WAITRESOLVE whether the hostname
* has already been resolved and the address has already been stored in
* the DNS cache. This short circuits waiting for a lot of pending
* lookups for the same hostname requested by different handles.
*
* Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
*
* The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
* use, or we'll leak memory!
*/
struct Curl_dns_entry *
Curl_fetch_addr(struct connectdata *conn,
const char *hostname,
int port)
{
struct SessionHandle *data = conn->data;
struct Curl_dns_entry *dns = NULL;

if(data->share)
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);

dns = fetch_addr(conn, hostname, port);

if(dns) dns->inuse++; /* we use it! */

if(data->share)
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);

return dns;
}
Expand Down Expand Up @@ -451,7 +483,7 @@ int Curl_resolv(struct connectdata *conn,
if(data->share)
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);

dns = Curl_fetch_addr(conn, hostname, port);
dns = fetch_addr(conn, hostname, port);

if(dns) {
infof(data, "Hostname %s was found in DNS cache\n", hostname);
Expand Down
3 changes: 3 additions & 0 deletions lib/hostip.h
Expand Up @@ -174,6 +174,9 @@ const char *Curl_printable_address(const Curl_addrinfo *ip,
* Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
*
* Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
*
* The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
* use, or we'll leak memory!
*/
struct Curl_dns_entry *
Curl_fetch_addr(struct connectdata *conn,
Expand Down
7 changes: 0 additions & 7 deletions lib/multi.c
Expand Up @@ -1112,13 +1112,9 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
struct connectdata *conn = data->easy_conn;

/* check if we have the name resolved by now */
if(data->share)
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);

dns = Curl_fetch_addr(conn, conn->host.name, (int)conn->port);

if(dns) {
dns->inuse++; /* we use it! */
#ifdef CURLRES_ASYNCH
conn->async.dns = dns;
conn->async.done = TRUE;
Expand All @@ -1127,9 +1123,6 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
infof(data, "Hostname was found in DNS cache\n");
}

if(data->share)
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);

if(!dns)
result = Curl_resolver_is_resolved(data->easy_conn, &dns);

Expand Down

0 comments on commit b4be97f

Please sign in to comment.