Fix cache issue

This commit is contained in:
Nick Peng
2019-02-15 00:11:21 +08:00
parent a1150a7ceb
commit e8b920fb18
2 changed files with 16 additions and 14 deletions

View File

@@ -83,10 +83,13 @@ int dns_cache_replace(char *domain, char *cname, int cname_ttl, int ttl, dns_typ
ttl = DNS_CACHE_TTL_MIN; ttl = DNS_CACHE_TTL_MIN;
} }
pthread_mutex_lock(&dns_cache_head.lock);
dns_cache->ttl = ttl; dns_cache->ttl = ttl;
dns_cache->qtype = qtype; dns_cache->qtype = qtype;
dns_cache->ttl = ttl; dns_cache->ttl = ttl;
dns_cache->del_pending = 0;
time(&dns_cache->insert_time); time(&dns_cache->insert_time);
pthread_mutex_unlock(&dns_cache_head.lock);
if (qtype == DNS_T_A) { if (qtype == DNS_T_A) {
if (addr_len != DNS_RR_A_LEN) { if (addr_len != DNS_RR_A_LEN) {
goto errout; goto errout;
@@ -148,6 +151,7 @@ int dns_cache_insert(char *domain, char *cname, int cname_ttl, int ttl, dns_type
dns_cache->qtype = qtype; dns_cache->qtype = qtype;
dns_cache->ttl = ttl; dns_cache->ttl = ttl;
dns_cache->hitnum = 2; dns_cache->hitnum = 2;
dns_cache->del_pending = 0;
atomic_set(&dns_cache->ref, 1); atomic_set(&dns_cache->ref, 1);
time(&dns_cache->insert_time); time(&dns_cache->insert_time);
if (qtype == DNS_T_A) { if (qtype == DNS_T_A) {
@@ -286,29 +290,26 @@ void dns_cache_invalidate(dns_cache_preinvalid_callback callback, int ttl_pre)
list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.cache_list, list) list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.cache_list, list)
{ {
ttl = dns_cache->insert_time + dns_cache->ttl - now; ttl = dns_cache->insert_time + dns_cache->ttl - now;
if (ttl > 0) { if (ttl > 0 && ttl < ttl_pre) {
if (ttl < ttl_pre) { if (callback && dns_cache->del_pending == 0) {
if (callback) { list_add_tail(&dns_cache->check_list, &checklist);
list_add_tail(&dns_cache->check_list, &checklist); dns_cache_get(dns_cache);
dns_cache_get(dns_cache); dns_cache->del_pending = 1;
continue;
}
}
if (callback) {
continue; continue;
} }
break;
} }
_dns_cache_remove(dns_cache); if (ttl < 0) {
_dns_cache_remove(dns_cache);
}
} }
pthread_mutex_unlock(&dns_cache_head.lock); pthread_mutex_unlock(&dns_cache_head.lock);
list_for_each_entry_safe(dns_cache, tmp, &checklist, check_list) list_for_each_entry_safe(dns_cache, tmp, &checklist, check_list)
{ {
callback(dns_cache); if (callback) {
dns_cache_delete(dns_cache); callback(dns_cache);
}
dns_cache_release(dns_cache); dns_cache_release(dns_cache);
} }
} }

View File

@@ -20,6 +20,7 @@ struct dns_cache {
unsigned int cname_ttl; unsigned int cname_ttl;
unsigned int ttl; unsigned int ttl;
int hitnum; int hitnum;
int del_pending;
time_t insert_time; time_t insert_time;
dns_type_t qtype; dns_type_t qtype;
union { union {