2
0
Эх сурвалжийг харах

dns_cache: fix issue of incorrect cache timeout when the process is restarted.

Nick Peng 1 жил өмнө
parent
commit
b3e16c3c60
4 өөрчлөгдсөн 14 нэмэгдсэн , 14 устгасан
  1. 9 10
      src/dns_cache.c
  2. 0 2
      src/dns_client.c
  3. 3 2
      src/dns_server.c
  4. 2 0
      src/smartdns.c

+ 9 - 10
src/dns_cache.c

@@ -268,7 +268,8 @@ static void _dns_cache_remove_by_domain(struct dns_cache_key *cache_key)
 	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;
 	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->timer.function = dns_cache_expired;
 	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;
 	pthread_mutex_lock(&dns_cache_head.lock);
 	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.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)
@@ -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;
 
 	time_t now = time(NULL);
-	unsigned int seed_tmp = now;
 	int passed_time = now - info->replace_time;
 	int timeout = info->timeout - passed_time;
-	if (timeout < DNS_CACHE_READ_TIMEOUT * 2) {
-		timeout = DNS_CACHE_READ_TIMEOUT + (rand_r(&seed_tmp) % DNS_CACHE_READ_TIMEOUT);
+	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_conf_serve_expired_ttl && dns_conf_serve_expired_ttl >= 0) {
-		timeout = dns_conf_serve_expired_ttl;
+	if (timeout < DNS_CACHE_READ_TIMEOUT * 2) {
+		timeout = DNS_CACHE_READ_TIMEOUT + (rand() % DNS_CACHE_READ_TIMEOUT);
 	}
-	info->timeout = timeout;
 
-	if (_dns_cache_insert(&cache_record->info, cache_data, head) != 0) {
+	if (_dns_cache_insert(&cache_record->info, cache_data, head, timeout) != 0) {
 		tlog(TLOG_ERROR, "insert cache data failed.");
 		cache_data = NULL;
 		goto errout;

+ 0 - 2
src/dns_client.c

@@ -4370,8 +4370,6 @@ int dns_client_init(void)
 		return -1;
 	}
 
-	srandom(time(NULL));
-
 	memset(&client, 0, sizeof(client));
 	pthread_attr_init(&attr);
 	atomic_set(&client.dns_server_num, 0);

+ 3 - 2
src/dns_server.c

@@ -6647,8 +6647,9 @@ static int _dns_server_prefetch_expired_domain(struct dns_cache *dns_cache)
 	}
 
 	/* start prefetch domain */
-	tlog(TLOG_DEBUG, "expired domain, prefetch by cache %s, qtype %d, ttl %llu", dns_cache->info.domain,
-		 dns_cache->info.qtype, (unsigned long long)ttl);
+	tlog(TLOG_DEBUG, "expired domain, prefetch by cache %s, qtype %d, ttl %llu, insert time %llu replace time %llu",
+		 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;
 	server_query_option.dns_group_name = dns_cache_get_dns_group_name(dns_cache);

+ 2 - 0
src/smartdns.c

@@ -830,6 +830,8 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	srand(time(NULL));
+
 	ret = dns_server_load_conf(config_file);
 	if (ret != 0) {
 		fprintf(stderr, "load config failed.\n");