#include <string.h>
#include <stdlib.h>
+#include <time.h>
#include <avahi-common/timeval.h>
#include <avahi-common/malloc.h>
#include "cache.h"
#include "log.h"
+#include "rr-util.h"
-#define AVAHI_MAX_CACHE_ENTRIES 500
+#define AVAHI_CACHE_ENTRIES_MAX 500
static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
AvahiCacheEntry *t;
AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries);
c->n_entries = 0;
+
+ c->last_rand_timestamp = 0;
return c;
}
avahi_free(c);
}
-AvahiCacheEntry *avahi_cache_lookup_key(AvahiCache *c, AvahiKey *k) {
+static AvahiCacheEntry *lookup_key(AvahiCache *c, AvahiKey *k) {
assert(c);
assert(k);
} else {
AvahiCacheEntry *e, *n;
- for (e = avahi_cache_lookup_key(c, pattern); e; e = n) {
+ for (e = lookup_key(c, pattern); e; e = n) {
n = e->by_key_next;
if ((ret = cb(c, pattern, e, userdata)))
return NULL;
}
-AvahiCacheEntry *avahi_cache_lookup_record(AvahiCache *c, AvahiRecord *r) {
+static AvahiCacheEntry *lookup_record(AvahiCache *c, AvahiRecord *r) {
assert(c);
assert(r);
assert(percent > 0);
- /* Request a cache update, if we are subscribed to this entry */
- if (avahi_querier_exists(e->cache->interface, e->record->key)) {
-/* avahi_log_debug("Requesting cache entry update at %i%% for %s.", percent, txt); */
- avahi_interface_post_query(e->cache->interface, e->record->key, 1);
- }
+ /* Request a cache update if we are subscribed to this entry */
+ if (avahi_querier_shall_refresh_cache(e->cache->interface, e->record->key))
+ avahi_interface_post_query(e->cache->interface, e->record->key, 0, NULL);
/* Check again later */
next_expiry(e->cache, e, percent);
static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent) {
AvahiUsec usec, left, right;
+ time_t now;
assert(c);
assert(e);
left = usec * percent;
right = usec * (percent+2); /* 2% jitter */
- usec = left + (AvahiUsec) ((double) (right-left) * rand() / (RAND_MAX+1.0));
+ now = time(NULL);
+
+ if (now >= c->last_rand_timestamp + 10) {
+ c->last_rand = rand();
+ c->last_rand_timestamp = now;
+ }
+
+ usec = left + (AvahiUsec) ((double) (right-left) * c->last_rand / (RAND_MAX+1.0));
e->expiry = e->timestamp;
avahi_timeval_add(&e->expiry, usec);
AvahiCacheEntry *e;
- if ((e = avahi_cache_lookup_record(c, r)))
+ if ((e = lookup_record(c, r)))
expire_in_one_second(c, e, AVAHI_CACHE_GOODBYE_FINAL);
} else {
/* This is an update request */
- if ((first = avahi_cache_lookup_key(c, r->key))) {
+ if ((first = lookup_key(c, r->key))) {
if (cache_flush) {
/* avahi_log_debug("cache: couldn't find matching cache entry for %s", txt); */
- if (c->n_entries >= AVAHI_MAX_CACHE_ENTRIES)
+ if (c->n_entries >= AVAHI_CACHE_ENTRIES_MAX)
return;
if (!(e = avahi_new(AvahiCacheEntry, 1))) {
assert(c);
assert(key);
- avahi_cache_walk(c, key, start_poof_callback, a);
+ avahi_cache_walk(c, key, start_poof_callback, (void*) a);
}
void avahi_cache_stop_poof(AvahiCache *c, AvahiRecord *record, const AvahiAddress *a) {
assert(record);
assert(a);
- if (!(e = avahi_cache_lookup_record(c, record)))
+ if (!(e = lookup_record(c, record)))
return;
/* This function is called for each response suppression