serve-expired: new feature, support cache serve-expired feature
This commit is contained in:
@@ -28,19 +28,25 @@
|
||||
struct dns_cache_head {
|
||||
DECLARE_HASHTABLE(cache_hash, 10);
|
||||
struct list_head cache_list;
|
||||
struct list_head inactive_list;
|
||||
atomic_t num;
|
||||
int size;
|
||||
int enable_inactive;
|
||||
int inactive_list_expired;
|
||||
pthread_mutex_t lock;
|
||||
};
|
||||
|
||||
static struct dns_cache_head dns_cache_head;
|
||||
|
||||
int dns_cache_init(int size)
|
||||
int dns_cache_init(int size, int enable_inactive, int inactive_list_expired)
|
||||
{
|
||||
INIT_LIST_HEAD(&dns_cache_head.cache_list);
|
||||
INIT_LIST_HEAD(&dns_cache_head.inactive_list);
|
||||
hash_init(dns_cache_head.cache_hash);
|
||||
atomic_set(&dns_cache_head.num, 0);
|
||||
dns_cache_head.size = size;
|
||||
dns_cache_head.enable_inactive = enable_inactive;
|
||||
dns_cache_head.inactive_list_expired = inactive_list_expired;
|
||||
|
||||
pthread_mutex_init(&dns_cache_head.lock, NULL);
|
||||
|
||||
@@ -49,11 +55,25 @@ int dns_cache_init(int size)
|
||||
|
||||
static __attribute__((unused)) struct dns_cache *_dns_cache_last(void)
|
||||
{
|
||||
struct dns_cache *dns_cache = NULL;
|
||||
|
||||
dns_cache = list_last_entry(&dns_cache_head.inactive_list, struct dns_cache, list);
|
||||
if (dns_cache) {
|
||||
return dns_cache;
|
||||
}
|
||||
|
||||
return list_last_entry(&dns_cache_head.cache_list, struct dns_cache, list);
|
||||
}
|
||||
|
||||
static struct dns_cache *_dns_cache_first(void)
|
||||
{
|
||||
struct dns_cache *dns_cache = NULL;
|
||||
|
||||
dns_cache = list_first_entry_or_null(&dns_cache_head.inactive_list, struct dns_cache, list);
|
||||
if (dns_cache) {
|
||||
return dns_cache;
|
||||
}
|
||||
|
||||
return list_first_entry_or_null(&dns_cache_head.cache_list, struct dns_cache, list);
|
||||
}
|
||||
|
||||
@@ -92,6 +112,12 @@ static void _dns_cache_remove(struct dns_cache *dns_cache)
|
||||
dns_cache_release(dns_cache);
|
||||
}
|
||||
|
||||
static void _dns_cache_move_inactive(struct dns_cache *dns_cache)
|
||||
{
|
||||
list_del_init(&dns_cache->list);
|
||||
list_add_tail(&dns_cache->list, &dns_cache_head.inactive_list);
|
||||
}
|
||||
|
||||
int dns_cache_replace(char *domain, char *cname, int cname_ttl, int ttl, dns_type_t qtype, unsigned char *addr, int addr_len, int speed)
|
||||
{
|
||||
struct dns_cache *dns_cache = NULL;
|
||||
@@ -136,13 +162,16 @@ int dns_cache_replace(char *domain, char *cname, int cname_ttl, int ttl, dns_typ
|
||||
safe_strncpy(dns_cache->cname, cname, DNS_MAX_CNAME_LEN);
|
||||
dns_cache->cname_ttl = cname_ttl;
|
||||
}
|
||||
|
||||
list_del_init(&dns_cache->list);
|
||||
list_add_tail(&dns_cache->list, &dns_cache_head.cache_list);
|
||||
pthread_mutex_unlock(&dns_cache_head.lock);
|
||||
|
||||
dns_cache_release(dns_cache);
|
||||
return 0;
|
||||
errout_unlock:
|
||||
pthread_mutex_unlock(&dns_cache_head.lock);
|
||||
// errout:
|
||||
// errout:
|
||||
if (dns_cache) {
|
||||
dns_cache_release(dns_cache);
|
||||
}
|
||||
@@ -263,7 +292,7 @@ struct dns_cache *dns_cache_lookup(char *domain, dns_type_t qtype)
|
||||
|
||||
if (dns_cache_ret) {
|
||||
/* Return NULL if the cache times out */
|
||||
if (now - dns_cache_ret->insert_time > dns_cache_ret->ttl) {
|
||||
if (dns_cache_head.enable_inactive == 0 && (now - dns_cache_ret->insert_time > dns_cache_ret->ttl)) {
|
||||
_dns_cache_remove(dns_cache_ret);
|
||||
dns_cache_ret = NULL;
|
||||
} else {
|
||||
@@ -328,6 +357,27 @@ void dns_cache_update(struct dns_cache *dns_cache)
|
||||
pthread_mutex_unlock(&dns_cache_head.lock);
|
||||
}
|
||||
|
||||
void _dns_cache_remove_expired_ttl(time_t *now)
|
||||
{
|
||||
struct dns_cache *dns_cache = NULL;
|
||||
struct dns_cache *tmp;
|
||||
int ttl = 0;
|
||||
|
||||
list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.inactive_list, list)
|
||||
{
|
||||
ttl = dns_cache->insert_time + dns_cache->ttl - *now;
|
||||
if (ttl > 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dns_cache_head.inactive_list_expired + ttl > 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
_dns_cache_remove(dns_cache);
|
||||
}
|
||||
}
|
||||
|
||||
void dns_cache_invalidate(dns_cache_preinvalid_callback callback, int ttl_pre)
|
||||
{
|
||||
struct dns_cache *dns_cache = NULL;
|
||||
@@ -356,9 +406,18 @@ void dns_cache_invalidate(dns_cache_preinvalid_callback callback, int ttl_pre)
|
||||
}
|
||||
|
||||
if (ttl < 0) {
|
||||
_dns_cache_remove(dns_cache);
|
||||
if (dns_cache_head.enable_inactive) {
|
||||
_dns_cache_move_inactive(dns_cache);
|
||||
} else {
|
||||
_dns_cache_remove(dns_cache);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dns_cache_head.enable_inactive && dns_cache_head.inactive_list_expired != 0) {
|
||||
_dns_cache_remove_expired_ttl(&now);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&dns_cache_head.lock);
|
||||
|
||||
list_for_each_entry_safe(dns_cache, tmp, &checklist, check_list)
|
||||
@@ -376,6 +435,11 @@ void dns_cache_destroy(void)
|
||||
struct dns_cache *dns_cache = NULL;
|
||||
struct dns_cache *tmp;
|
||||
pthread_mutex_lock(&dns_cache_head.lock);
|
||||
list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.inactive_list, list)
|
||||
{
|
||||
_dns_cache_delete(dns_cache);
|
||||
}
|
||||
|
||||
list_for_each_entry_safe(dns_cache, tmp, &dns_cache_head.cache_list, list)
|
||||
{
|
||||
_dns_cache_delete(dns_cache);
|
||||
|
||||
@@ -55,7 +55,7 @@ struct dns_cache {
|
||||
};
|
||||
};
|
||||
|
||||
int dns_cache_init(int size);
|
||||
int dns_cache_init(int size, int enable_inactive, int inactive_list_expired);
|
||||
|
||||
int dns_cache_replace(char *domain, char *cname, int cname_ttl, int ttl, dns_type_t qtype, unsigned char *addr, int addr_len, int speed);
|
||||
|
||||
|
||||
@@ -48,6 +48,8 @@ int dns_conf_tcp_idle_time = 120;
|
||||
/* cache */
|
||||
int dns_conf_cachesize = DEFAULT_DNS_CACHE_SIZE;
|
||||
int dns_conf_prefetch = 0;
|
||||
int dns_conf_serve_expired = 0;
|
||||
int dns_conf_serve_expired_ttl = 0;
|
||||
|
||||
/* upstream servers */
|
||||
struct dns_servers dns_conf_servers[DNS_MAX_SERVERS];
|
||||
@@ -1340,6 +1342,8 @@ static struct config_item _config_item[] = {
|
||||
CONF_INT("tcp-idle-time", &dns_conf_tcp_idle_time, 0, 3600),
|
||||
CONF_INT("cache-size", &dns_conf_cachesize, 0, CONF_INT_MAX),
|
||||
CONF_YESNO("prefetch-domain", &dns_conf_prefetch),
|
||||
CONF_YESNO("serve-expired", &dns_conf_serve_expired),
|
||||
CONF_INT("serve-expired-ttl", &dns_conf_serve_expired_ttl, 0, CONF_INT_MAX),
|
||||
CONF_YESNO("dualstack-ip-selection", &dns_conf_dualstack_ip_selection),
|
||||
CONF_INT("dualstack-ip-selection-threshold", &dns_conf_dualstack_ip_selection_threshold, 0, 1000),
|
||||
CONF_CUSTOM("log-level", _config_log_level, NULL),
|
||||
|
||||
@@ -200,6 +200,8 @@ extern int dns_conf_bind_ip_num;
|
||||
extern int dns_conf_tcp_idle_time;
|
||||
extern int dns_conf_cachesize;
|
||||
extern int dns_conf_prefetch;
|
||||
extern int dns_conf_serve_expired;
|
||||
extern int dns_conf_serve_expired_ttl;
|
||||
extern struct dns_servers dns_conf_servers[DNS_MAX_SERVERS];
|
||||
extern int dns_conf_server_num;
|
||||
|
||||
|
||||
@@ -205,6 +205,8 @@ static struct dns_server server;
|
||||
|
||||
static tlog_log *dns_audit;
|
||||
|
||||
static int _dns_server_prefetch_request(char *domain, dns_type_t qtype);
|
||||
|
||||
static int _dns_server_forward_request(unsigned char *inpacket, int inpacket_len)
|
||||
{
|
||||
tlog(TLOG_DEBUG, "forward request.\n");
|
||||
@@ -1617,6 +1619,7 @@ static int dns_server_resolve_callback(char *domain, dns_result_type rtype, unsi
|
||||
|
||||
/* Not need to wait check result if only has one ip address */
|
||||
if (ip_num == 1 && request_wait == 1) {
|
||||
request->has_ping_result = 1;
|
||||
_dns_server_request_complete(request);
|
||||
_dns_server_request_remove(request);
|
||||
}
|
||||
@@ -2009,7 +2012,11 @@ static int _dns_server_process_cache(struct dns_request *request)
|
||||
_dns_reply(request);
|
||||
}
|
||||
|
||||
dns_cache_update(dns_cache);
|
||||
if (dns_cache_get_ttl(dns_cache) == 0) {
|
||||
_dns_server_prefetch_request(request->domain, request->qtype);
|
||||
} else {
|
||||
dns_cache_update(dns_cache);
|
||||
}
|
||||
dns_cache_release(dns_cache);
|
||||
|
||||
if (dns_cache_A) {
|
||||
@@ -3136,7 +3143,7 @@ int dns_server_init(void)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dns_cache_init(dns_conf_cachesize) != 0) {
|
||||
if (dns_cache_init(dns_conf_cachesize, dns_conf_serve_expired, dns_conf_serve_expired_ttl) != 0) {
|
||||
tlog(TLOG_ERROR, "init cache failed.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user