dns_cache: fix issue of incorrect cache timeout when the process is restarted.
This commit is contained in:
@@ -268,7 +268,8 @@ static void _dns_cache_remove_by_domain(struct dns_cache_key *cache_key)
|
|||||||
pthread_mutex_unlock(&dns_cache_head.lock);
|
pthread_mutex_unlock(&dns_cache_head.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _dns_cache_insert(struct dns_cache_info *info, struct dns_cache_data *cache_data, struct list_head *head)
|
static int _dns_cache_insert(struct dns_cache_info *info, struct dns_cache_data *cache_data, struct list_head *head,
|
||||||
|
int timeout)
|
||||||
{
|
{
|
||||||
uint32_t key = 0;
|
uint32_t key = 0;
|
||||||
struct dns_cache *dns_cache = NULL;
|
struct dns_cache *dns_cache = NULL;
|
||||||
@@ -297,7 +298,7 @@ static int _dns_cache_insert(struct dns_cache_info *info, struct dns_cache_data
|
|||||||
dns_cache->cache_data = cache_data;
|
dns_cache->cache_data = cache_data;
|
||||||
dns_cache->timer.function = dns_cache_expired;
|
dns_cache->timer.function = dns_cache_expired;
|
||||||
dns_cache->timer.del_function = dns_cache_timer_release;
|
dns_cache->timer.del_function = dns_cache_timer_release;
|
||||||
dns_cache->timer.expires = info->timeout;
|
dns_cache->timer.expires = timeout;
|
||||||
dns_cache->timer.data = dns_cache;
|
dns_cache->timer.data = dns_cache;
|
||||||
pthread_mutex_lock(&dns_cache_head.lock);
|
pthread_mutex_lock(&dns_cache_head.lock);
|
||||||
hash_table_add(dns_cache_head.cache_hash, &dns_cache->node, key);
|
hash_table_add(dns_cache_head.cache_hash, &dns_cache->node, key);
|
||||||
@@ -359,7 +360,7 @@ int dns_cache_insert(struct dns_cache_key *cache_key, int rcode, int ttl, int sp
|
|||||||
time(&info.insert_time);
|
time(&info.insert_time);
|
||||||
time(&info.replace_time);
|
time(&info.replace_time);
|
||||||
|
|
||||||
return _dns_cache_insert(&info, cache_data, &dns_cache_head.cache_list);
|
return _dns_cache_insert(&info, cache_data, &dns_cache_head.cache_list, timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct dns_cache *dns_cache_lookup(struct dns_cache_key *cache_key)
|
struct dns_cache *dns_cache_lookup(struct dns_cache_key *cache_key)
|
||||||
@@ -509,19 +510,17 @@ static int _dns_cache_read_to_cache(struct dns_cache_record *cache_record, struc
|
|||||||
struct dns_cache_info *info = &cache_record->info;
|
struct dns_cache_info *info = &cache_record->info;
|
||||||
|
|
||||||
time_t now = time(NULL);
|
time_t now = time(NULL);
|
||||||
unsigned int seed_tmp = now;
|
|
||||||
int passed_time = now - info->replace_time;
|
int passed_time = now - info->replace_time;
|
||||||
int timeout = info->timeout - passed_time;
|
int timeout = info->timeout - passed_time;
|
||||||
|
if ((timeout > dns_conf_serve_expired_ttl + info->ttl) && dns_conf_serve_expired_ttl >= 0) {
|
||||||
|
timeout = dns_conf_serve_expired_ttl + info->ttl;
|
||||||
|
}
|
||||||
|
|
||||||
if (timeout < DNS_CACHE_READ_TIMEOUT * 2) {
|
if (timeout < DNS_CACHE_READ_TIMEOUT * 2) {
|
||||||
timeout = DNS_CACHE_READ_TIMEOUT + (rand_r(&seed_tmp) % DNS_CACHE_READ_TIMEOUT);
|
timeout = DNS_CACHE_READ_TIMEOUT + (rand() % DNS_CACHE_READ_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timeout > dns_conf_serve_expired_ttl && dns_conf_serve_expired_ttl >= 0) {
|
if (_dns_cache_insert(&cache_record->info, cache_data, head, timeout) != 0) {
|
||||||
timeout = dns_conf_serve_expired_ttl;
|
|
||||||
}
|
|
||||||
info->timeout = timeout;
|
|
||||||
|
|
||||||
if (_dns_cache_insert(&cache_record->info, cache_data, head) != 0) {
|
|
||||||
tlog(TLOG_ERROR, "insert cache data failed.");
|
tlog(TLOG_ERROR, "insert cache data failed.");
|
||||||
cache_data = NULL;
|
cache_data = NULL;
|
||||||
goto errout;
|
goto errout;
|
||||||
|
|||||||
@@ -4370,8 +4370,6 @@ int dns_client_init(void)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
srandom(time(NULL));
|
|
||||||
|
|
||||||
memset(&client, 0, sizeof(client));
|
memset(&client, 0, sizeof(client));
|
||||||
pthread_attr_init(&attr);
|
pthread_attr_init(&attr);
|
||||||
atomic_set(&client.dns_server_num, 0);
|
atomic_set(&client.dns_server_num, 0);
|
||||||
|
|||||||
@@ -6647,8 +6647,9 @@ static int _dns_server_prefetch_expired_domain(struct dns_cache *dns_cache)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* start prefetch domain */
|
/* start prefetch domain */
|
||||||
tlog(TLOG_DEBUG, "expired domain, prefetch by cache %s, qtype %d, ttl %llu", dns_cache->info.domain,
|
tlog(TLOG_DEBUG, "expired domain, prefetch by cache %s, qtype %d, ttl %llu, insert time %llu replace time %llu",
|
||||||
dns_cache->info.qtype, (unsigned long long)ttl);
|
dns_cache->info.domain, dns_cache->info.qtype, (unsigned long long)ttl,
|
||||||
|
(unsigned long long)dns_cache->info.insert_time, (unsigned long long)dns_cache->info.replace_time);
|
||||||
|
|
||||||
struct dns_server_query_option server_query_option;
|
struct dns_server_query_option server_query_option;
|
||||||
server_query_option.dns_group_name = dns_cache_get_dns_group_name(dns_cache);
|
server_query_option.dns_group_name = dns_cache_get_dns_group_name(dns_cache);
|
||||||
|
|||||||
@@ -830,6 +830,8 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
ret = dns_server_load_conf(config_file);
|
ret = dns_server_load_conf(config_file);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
fprintf(stderr, "load config failed.\n");
|
fprintf(stderr, "load config failed.\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user