optimize prefetch feature

This commit is contained in:
Nick Peng
2019-01-18 00:19:17 +08:00
parent dd4372a961
commit bb1c4d2bfa
3 changed files with 20 additions and 1 deletions

View File

@@ -72,6 +72,10 @@ int dns_cache_replace(char *domain, char *cname, int cname_ttl, int ttl, dns_typ
return 0;
}
if (ttl < DNS_CACHE_TTL_MIN) {
ttl = DNS_CACHE_TTL_MIN;
}
dns_cache->ttl = ttl;
dns_cache->qtype = qtype;
dns_cache->ttl = ttl;
@@ -125,12 +129,17 @@ int dns_cache_insert(char *domain, char *cname, int cname_ttl, int ttl, dns_type
goto errout;
}
if (ttl < DNS_CACHE_TTL_MIN) {
ttl = DNS_CACHE_TTL_MIN;
}
key = hash_string(domain);
key = jhash(&qtype, sizeof(qtype), key);
strncpy(dns_cache->domain, domain, DNS_MAX_CNAME_LEN);
dns_cache->cname[0] = 0;
dns_cache->qtype = qtype;
dns_cache->ttl = ttl;
dns_cache->hitnum = 6;
atomic_set(&dns_cache->ref, 1);
time(&dns_cache->insert_time);
if (qtype == DNS_T_A) {
@@ -247,6 +256,7 @@ void dns_cache_update(struct dns_cache *dns_cache)
if (!list_empty(&dns_cache->list)) {
list_del_init(&dns_cache->list);
list_add_tail(&dns_cache->list, &dns_cache_head.cache_list);
dns_cache->hitnum++;
}
pthread_mutex_unlock(&dns_cache_head.lock);
}

View File

@@ -8,6 +8,8 @@
#include "list.h"
#include <time.h>
#define DNS_CACHE_TTL_MIN 30
struct dns_cache {
struct hlist_node node;
struct list_head list;
@@ -17,6 +19,7 @@ struct dns_cache {
char cname[DNS_MAX_CNAME_LEN];
unsigned int cname_ttl;
unsigned int ttl;
int hitnum;
time_t insert_time;
dns_type_t qtype;
union {

View File

@@ -1865,7 +1865,13 @@ void _dns_server_tcp_ping_check(struct dns_request *request)
void _dns_server_prefetch_domain(struct dns_cache *dns_cache)
{
tlog(TLOG_DEBUG, "prefetch by cache %s, qtype %d, ttl %d", dns_cache->domain, dns_cache->qtype, dns_cache->ttl);
if (dns_cache->hitnum <= 0) {
return;
}
dns_cache->hitnum--;
tlog(TLOG_DEBUG, "prefetch by cache %s, qtype %d, ttl %d, hitnum %d", dns_cache->domain, dns_cache->qtype, dns_cache->ttl, dns_cache->hitnum);
if (_dns_server_prefetch_request(dns_cache->domain, dns_cache->qtype) != 0) {
tlog(TLOG_ERROR, "prefetch domain %s, qtype %d, failed.", dns_cache->domain, dns_cache->qtype);
}