@@ -305,6 +305,7 @@ static int _dns_cache_insert(struct dns_cache_info *info, struct dns_cache_data
305
305
{
306
306
uint32_t key = 0 ;
307
307
struct dns_cache * dns_cache = NULL ;
308
+ int loop_count = 0 ;
308
309
309
310
if (cache_data == NULL || info == NULL ) {
310
311
goto errout ;
@@ -324,7 +325,6 @@ static int _dns_cache_insert(struct dns_cache_info *info, struct dns_cache_data
324
325
}
325
326
326
327
memset (dns_cache , 0 , sizeof (* dns_cache ));
327
- atomic_add (sizeof (* dns_cache ), & dns_cache_head .mem_size );
328
328
key = hash_string (info -> domain );
329
329
key = jhash (& info -> qtype , sizeof (info -> qtype ), key );
330
330
key = hash_string_initval (info -> dns_group_name , key );
@@ -342,16 +342,32 @@ static int _dns_cache_insert(struct dns_cache_info *info, struct dns_cache_data
342
342
pthread_mutex_lock (& dns_cache_head .lock );
343
343
hash_table_add (dns_cache_head .cache_hash , & dns_cache -> node , key );
344
344
list_add_tail (& dns_cache -> list , head );
345
+ atomic_add (sizeof (* dns_cache ), & dns_cache_head .mem_size );
346
+ atomic_inc (& dns_cache_head .num );
345
347
346
348
/* Release extra cache, remove oldest cache record */
347
- if (atomic_inc_return (& dns_cache_head .num ) > dns_cache_head .size ||
348
- (dns_cache_head .max_mem_size > 0 && atomic_read (& dns_cache_head .mem_size ) > dns_cache_head .max_mem_size )) {
349
- struct dns_cache * del_cache = NULL ;
350
- del_cache = _dns_cache_first ();
351
- if (del_cache ) {
352
- _dns_cache_remove (del_cache );
349
+ do {
350
+ int need_remove = 0 ;
351
+
352
+ if (dns_cache_head .max_mem_size > 0 && atomic_read (& dns_cache_head .mem_size ) > dns_cache_head .max_mem_size ) {
353
+ need_remove = 1 ;
353
354
}
354
- }
355
+
356
+ if (atomic_read (& dns_cache_head .num ) > dns_cache_head .size ) {
357
+ need_remove = 1 ;
358
+ }
359
+
360
+ if (need_remove == 0 ) {
361
+ break ;
362
+ }
363
+
364
+ struct dns_cache * del_cache = _dns_cache_first ();
365
+ if (del_cache == NULL ) {
366
+ break ;
367
+ }
368
+
369
+ _dns_cache_remove (del_cache );
370
+ } while (loop_count ++ < 32 );
355
371
356
372
dns_cache_get (dns_cache );
357
373
dns_timer_add (& dns_cache -> timer );
0 commit comments