Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge "Remove expired dns cache entries before removing oldest"
Browse files Browse the repository at this point in the history
  • Loading branch information
enh-google authored and android code review committed May 7, 2012
2 parents 8657eaf + dd16182 commit 73a6566
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion libc/netbsd/resolv/res_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,27 @@ _cache_remove_oldest( Cache* cache )
_cache_remove_p(cache, lookup);
}

/* Remove all expired entries from the hash table.
*/
static void _cache_remove_expired(Cache* cache) {
Entry* e;
time_t now = _time_now();

for (e = cache->mru_list.mru_next; e != &cache->mru_list;) {
// Entry is old, remove
if (now >= e->expires) {
Entry** lookup = _cache_lookup_p(cache, e);
if (*lookup == NULL) { /* should not happen */
XLOG("%s: ENTRY NOT IN HTABLE ?", __FUNCTION__);
return;
}
e = e->mru_next;
_cache_remove_p(cache, lookup);
} else {
e = e->mru_next;
}
}
}

ResolvCacheStatus
_resolv_cache_lookup( struct resolv_cache* cache,
Expand Down Expand Up @@ -1526,7 +1547,10 @@ _resolv_cache_add( struct resolv_cache* cache,
}

if (cache->num_entries >= cache->max_entries) {
_cache_remove_oldest(cache);
_cache_remove_expired(cache);
if (cache->num_entries >= cache->max_entries) {
_cache_remove_oldest(cache);
}
/* need to lookup again */
lookup = _cache_lookup_p(cache, key);
e = *lookup;
Expand Down

0 comments on commit 73a6566

Please sign in to comment.