]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-dns: Cleanup lookup cache only when there are entries
authorAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 27 Sep 2021 15:24:06 +0000 (18:24 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 17 Jan 2022 11:52:09 +0000 (13:52 +0200)
src/lib-dns/dns-lookup.c

index 2190c47e0c909905dd6c290f99f98a44af43b191..3d85cd3ecea840ac6b741e2e1118f4188f0c9525 100644 (file)
@@ -80,6 +80,8 @@ struct dns_client {
        bool deinit_client_at_free:1;
 };
 
+static void dns_client_cache_clean(struct dns_client *client);
+
 /* cache code */
 static int dns_client_cache_entry_cmp(const void *p1, const void *p2)
 {
@@ -103,6 +105,13 @@ static void dns_client_cache_entry(struct dns_client *client,
 {
        if (client->cache_ttl_secs == 0)
                return;
+
+       /* start cache cleanup since put something there */
+       if (client->to_cache_clean == NULL)
+               client->to_cache_clean =
+                       timeout_add((client->cache_ttl_secs/2)*1000,
+                                    dns_client_cache_clean, client);
+
        struct dns_client_cache_entry *entry =
                hash_table_lookup(client->cache_table, lookup->cache_key);
        if (lookup->result.ret < 0) {
@@ -243,6 +252,10 @@ static void dns_client_cache_clean(struct dns_client *client)
                        break;
                }
        }
+
+       /* stop cleaning cache if it becomes empty */
+       if (priorityq_count(client->cache_queue) == 0)
+               timeout_remove(&client->to_cache_clean);
 }
 
 static void dns_client_cache_init(struct dns_client *client, unsigned int ttl_secs)
@@ -251,8 +264,6 @@ static void dns_client_cache_init(struct dns_client *client, unsigned int ttl_se
        hash_table_create(&client->cache_table, default_pool, 0, strfastcase_hash,
                          strcmp);
        client->cache_queue = priorityq_init(dns_client_cache_entry_cmp, 0);
-       client->to_cache_clean = timeout_add((client->cache_ttl_secs/2)*1000,
-                                              dns_client_cache_clean, client);
 }
 
 static void dns_client_cache_deinit(struct dns_client *client)