Commit 8d7f402e authored by Sterling Hughes's avatar Sterling Hughes
Browse files

Make cach'ing work with threads now, there are now three cases:

    - Use a global dns cache (via setting the tentatively named,
    CURLOPT_DNS_USE_GLOBAL_CACHE option to true)
    - Use a per-handle dns cache, by default
    - Use a pooled dns cache when in the "multi" interface
parent d3299bee
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -488,6 +488,9 @@ typedef enum {
   */
  CINIT(SSLENGINE_DEFAULT, LONG, 90),

  /* Non-zero value means to use the global dns cache */
  CINIT(DNS_USE_GLOBAL_CACHE, LONG, 91),

  CURLOPT_LASTENTRY /* the last unusued */
} CURLoption;

+14 −4
Original line number Diff line number Diff line
@@ -141,8 +141,6 @@ CURLcode curl_global_init(long flags)
  if (initialized)
    return CURLE_OK;

  Curl_host_cache_init();
  
  if (flags & CURL_GLOBAL_SSL)
    Curl_SSL_init();

@@ -165,7 +163,7 @@ void curl_global_cleanup(void)
  if (!initialized)
    return;

  Curl_host_cache_dtor();
  Curl_global_host_cache_dtor();

  if (init_flags & CURL_GLOBAL_SSL)
    Curl_SSL_cleanup();
@@ -235,12 +233,24 @@ CURLcode curl_easy_perform(CURL *curl)
{
  struct SessionHandle *data = (struct SessionHandle *)curl;

  if (!data->hostcache) {
    if (Curl_global_host_cache_use(data)) {
      data->hostcache = Curl_global_host_cache_get();
    }
    else {
      data->hostcache = curl_hash_alloc(7, Curl_freeaddrinfo);
    }
  }

  return Curl_perform(data);
}

void curl_easy_cleanup(CURL *curl)
{
  struct SessionHandle *data = (struct SessionHandle *)curl;
  if (!Curl_global_host_cache_use(data)) {
    curl_hash_destroy(data->hostcache);
  }
  Curl_close(data);
}

+11 −0
Original line number Diff line number Diff line
@@ -255,11 +255,22 @@ curl_hash_clean(curl_hash *h)
  h->table = NULL;
}

size_t 
curl_hash_count(curl_hash *h)
{
  return h->size;
}

void 
curl_hash_destroy(curl_hash *h)
{
  if (!h) {
    return;
  }

  curl_hash_clean(h);
  free(h);
  h = NULL;
}

/*
+1 −0
Original line number Diff line number Diff line
@@ -71,6 +71,7 @@ int curl_hash_extended_delete(curl_hash *h, char *str_key, unsigned int str_key_
int curl_hash_extended_find(curl_hash *h, char *str_key, unsigned int str_key_len, 
			     unsigned long num_key, void **p);
void curl_hash_apply(curl_hash *h, void *user, void (*cb)(void *, curl_hash_element *));
size_t curl_hash_count(curl_hash *h);
void curl_hash_clean(curl_hash *h);
void curl_hash_destroy(curl_hash *h);

+19 −7
Original line number Diff line number Diff line
@@ -70,17 +70,27 @@
#endif

static curl_hash hostname_cache;
static int host_cache_initialized;

void Curl_host_cache_init(void)
void Curl_global_host_cache_init(void)
{
  if (!host_cache_initialized) {
    curl_hash_init(&hostname_cache, 7, Curl_freeaddrinfo);
    host_cache_initialized = 1;
  }
}

void Curl_host_cache_dtor(void)
curl_hash *Curl_global_host_cache_get(void)
{
  curl_hash_clean(&hostname_cache);
  return &hostname_cache;
}

void Curl_global_host_cache_dtor(void)
{
  if (host_cache_initialized) {
    curl_hash_clean(&hostname_cache);
  }
}

Curl_addrinfo *Curl_resolv(struct SessionHandle *data,
                           char *hostname,
@@ -90,14 +100,15 @@ Curl_addrinfo *Curl_resolv(struct SessionHandle *data,
  Curl_addrinfo *addr = NULL;
  size_t hostname_len = strlen(hostname)+1;

  if (curl_hash_find(&hostname_cache, hostname, hostname_len, (void **) &addr))
  if (curl_hash_find(data->hostcache, hostname, hostname_len, (void **) &addr)) {
    return addr;
  }
  
  addr = Curl_getaddrinfo(data, hostname, port, bufp);
  if (!addr)
    return NULL;

  curl_hash_add(&hostname_cache, hostname, hostname_len, (const void *) addr);
  curl_hash_add(data->hostcache, hostname, hostname_len, (const void *) addr);
  return addr;
}

@@ -405,3 +416,4 @@ Curl_addrinfo *Curl_getaddrinfo(struct SessionHandle *data,
 * vim600: fdm=marker
 * vim: et sw=2 ts=2 sts=2 tw=78
 */
 
Loading