add a basic cache refresh logic
authorJohn Crispin <blogic@openwrt.org>
Thu, 4 Sep 2014 20:37:41 +0000 (22:37 +0200)
committerJohn Crispin <blogic@openwrt.org>
Thu, 4 Sep 2014 20:39:36 +0000 (22:39 +0200)
Signed-off-by: John Crispin <blogic@openwrt.org>
cache.c
cache.h

diff --git a/cache.c b/cache.c
index e4479226609b0b2ac69b92ec0f3755af03270ef1..f2687e115b50a386dfdc50e1abf9210d5d028e5c 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -61,9 +61,9 @@ cache_service_free(struct cache_service *s)
 }
 
 static int
-cache_is_expired(time_t t, uint32_t ttl)
+cache_is_expired(time_t t, uint32_t ttl, int frac)
 {
-       if (time(NULL) - t >= ttl)
+       if (time(NULL) - t >= ttl * frac / 100)
                return 1;
 
        return 0;
@@ -76,14 +76,20 @@ cache_gc_timer(struct uloop_timeout *timeout)
        struct cache_service *s, *t;
 
        avl_for_each_element_safe(&records, r, avl, p)
-               if (cache_is_expired(r->time, r->ttl))
+               if (cache_is_expired(r->time, r->ttl, 100))
                        cache_record_free(r);
 
        avl_for_each_element_safe(&services, s, avl, t) {
                if (!s->host)
                        continue;
-               if (cache_is_expired(s->time, s->ttl))
+               if (!cache_is_expired(s->time, s->ttl, s->refresh))
+                       continue;
+               if (s->refresh >= 100) {
                        cache_service_free(s);
+                       continue;
+               }
+               s->refresh += 50;
+               dns_send_question(s->iface, s->entry, TYPE_PTR, 1);
        }
 
        uloop_timeout_set(timeout, 10000);
@@ -134,8 +140,11 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
        char *type;
 
        avl_for_each_element_safe(&services, s, avl, t)
-               if (!strcmp(s->entry, entry))
+               if (!strcmp(s->entry, entry)) {
+                       s->refresh = 50;
+                       s->time = time(NULL);
                        return s;
+               }
 
        s = calloc_a(sizeof(*s),
                &entry_buf, strlen(entry) + 1,
@@ -145,6 +154,7 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
        s->time = time(NULL);
        s->ttl = ttl;
        s->iface = iface;
+       s->refresh = 50;
 
        if (hlen)
                s->host = strncpy(host_buf, s->entry, hlen);
diff --git a/cache.h b/cache.h
index 211f464b401dd6f7857a374e7efe175312515dcf..f11466560fe76a130241860e13b20a4878a1802e 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -29,6 +29,7 @@ struct cache_service {
        uint32_t ttl;
        time_t time;
        struct interface *iface;
+       int refresh;
 };
 
 struct cache_record {