浏览代码

cache: fix cache serve-expired TTL issue.

Nick Peng 1 月之前
父节点
当前提交
aa61468382
共有 5 个文件被更改,包括 47 次插入31 次删除
  1. 8 4
      etc/smartdns/smartdns.conf
  2. 16 8
      src/dns_cache.c
  3. 7 1
      src/dns_conf/dns_conf_group.c
  4. 15 18
      src/dns_stats.c
  5. 1 0
      src/include/smartdns/dns_stats.h

+ 8 - 4
etc/smartdns/smartdns.conf

@@ -77,7 +77,7 @@ bind [::]:53
 # cache-persist no
 
 # cache persist file
-# cache-file /tmp/smartdns.cache
+# cache-file /var/cache/smartdns.cache
 
 # cache persist time
 # cache-checkpoint-time [second]
@@ -93,11 +93,11 @@ bind [::]:53
 
 # cache serve expired TTL
 # serve-expired-ttl [num]
-# serve-expired-ttl 0
+# serve-expired-ttl 86400
 
 # reply TTL value to use when replying with expired data
 # serve-expired-reply-ttl [num]
-# serve-expired-reply-ttl 30
+# serve-expired-reply-ttl 3
 
 # List of hosts that supply bogus NX domain results 
 # bogus-nxdomain [ip/subnet]
@@ -187,7 +187,7 @@ log-level info
 # audit-enable yes
 # audit-SOA [yes|no]: enable or disable log soa result.
 # audit-size size of each audit file, support k,m,g
-# audit-file /var/log/smartdns-audit.log
+# audit-file /var/log/smartdns/smartdns-audit.log
 # audit-console [yes|no]: output audit log to console.
 # audit-syslog [yes|no]: output audit log to syslog.
 # audit-file-mode [mode]: file mode of audit file.
@@ -436,6 +436,10 @@ log-level info
 # group-match [-g|group group-name] [-domain domain] [-client-ip [ip-cidr|mac|ip-set]]
 # group-end
 
+# data directory
+# data-dir [path]
+# data-dir /var/lib/smartdns
+
 # load plugin
 # plugin [path/to/file] [args]
 

+ 16 - 8
src/dns_cache.c

@@ -642,18 +642,26 @@ static int _dns_cache_read_to_cache(struct dns_cache_record *cache_record, struc
 		info->replace_time = now;
 	}
 
+	int passed_time = now - info->replace_time;
+	int timeout = info->timeout - passed_time;
+
 	struct dns_conf_group *rule_group = dns_server_get_rule_group(info->dns_group_name);
-	expired_time = rule_group->dns_serve_expired_prefetch_time;
-	if (expired_time == 0) {
-		expired_time = rule_group->dns_serve_expired_ttl / 2;
-		if (expired_time == 0 || expired_time > EXPIRED_DOMAIN_PREFETCH_TIME) {
-			expired_time = EXPIRED_DOMAIN_PREFETCH_TIME;
+
+	if (rule_group->dns_prefetch) {
+		if (rule_group->dns_serve_expired) {
+			expired_time = rule_group->dns_serve_expired_prefetch_time;
+			if (expired_time == 0) {
+				expired_time = rule_group->dns_serve_expired_ttl / 2;
+				if (expired_time == 0 || expired_time > EXPIRED_DOMAIN_PREFETCH_TIME) {
+					expired_time = EXPIRED_DOMAIN_PREFETCH_TIME;
+				}
+			}
+		} else {
+			timeout -= 3;
 		}
 	}
 
-	int passed_time = now - info->replace_time;
-	int timeout = info->timeout - passed_time;
-	if ((timeout > expired_time + info->ttl) && expired_time >= 0) {
+	if ((timeout > expired_time + info->ttl) && expired_time > 0) {
 		timeout = expired_time + info->ttl;
 	}
 

+ 7 - 1
src/dns_conf/dns_conf_group.c

@@ -213,7 +213,13 @@ static int _config_rule_group_setup_value(struct dns_conf_group_info *group_info
 	group_rule->dns_dualstack_ip_selection_threshold = 10;
 	group_rule->dns_rr_ttl_min = 600;
 	group_rule->dns_serve_expired = 1;
-	group_rule->dns_serve_expired_ttl = 24 * 3600 * 3;
+	
+	if (group_rule->dns_prefetch == 1) {
+		group_rule->dns_serve_expired_ttl = 24 * 3600 * 7;
+	} else {
+		group_rule->dns_serve_expired_ttl = 24 * 3600;
+	}
+	
 	group_rule->dns_serve_expired_reply_ttl = 3;
 	group_rule->dns_max_reply_ip_num = DNS_MAX_REPLY_IP_NUM;
 	group_rule->dns_response_mode = dns_conf.default_response_mode;

+ 15 - 18
src/dns_stats.c

@@ -22,8 +22,6 @@
 
 struct dns_stats dns_stats;
 
-#define SAMPLE_PERIOD 5
-
 void dns_stats_avg_time_update_add(struct dns_stats_avg_time *avg_time, uint64_t time)
 {
 	if (avg_time == NULL) {
@@ -44,30 +42,29 @@ void dns_stats_avg_time_update(struct dns_stats_avg_time *avg_time)
 		return;
 	}
 
-	float sample_avg = (float)time / count;
+	double sample_avg = (double)time / count;
 
-	if (avg_time->avg_time == 0) {
+	if (avg_time->count == 0) {
 		avg_time->avg_time = sample_avg;
-	} else {
-		int base = 1000;
-		if (count > 100) {
-			count = 100;
-		}
-
-		float weight_new = (float)count / base;
-		float weight_prev = 1.0 - weight_new;
+		avg_time->count = count;
+		return;
+	}
 
-		avg_time->avg_time = (avg_time->avg_time * weight_prev) + (sample_avg * weight_new);
+	int base = 1000;
+	if (count > 100) {
+		count = 100;
 	}
+
+	double weight_new = (double)count / base;
+	double weight_prev = 1.0 - weight_new;
+
+	avg_time->avg_time = (avg_time->avg_time * weight_prev) + (sample_avg * weight_new);
+	avg_time->count += count;
 }
 
 void dns_stats_period_run_second(void)
 {
-	static int last_total = 0;
-	last_total++;
-	if (last_total % SAMPLE_PERIOD == 0) {
-		dns_stats_avg_time_update(&dns_stats.avg_time);
-	}
+	dns_stats_avg_time_update(&dns_stats.avg_time);
 }
 
 float dns_stats_avg_time_get(void)

+ 1 - 0
src/include/smartdns/dns_stats.h

@@ -28,6 +28,7 @@ extern "C" {
 
 struct dns_stats_avg_time {
 	uint64_t total; /* Hight 4 bytes, count, Low 4 bytes time*/
+	uint64_t count;
 	float avg_time;
 };