g_assert(c);
g_assert(e);
+ g_message("removing from cache: %p %p", c, e);
+
if (remove_from_hash_table) {
flxCacheEntry *t;
- t = g_hash_table_lookup(c->hash_table, &e->record->key);
+ t = g_hash_table_lookup(c->hash_table, e->record->key);
FLX_LLIST_REMOVE(flxCacheEntry, by_name, t, e);
if (t)
- g_hash_table_replace(c->hash_table, &t->record->key, t);
+ g_hash_table_replace(c->hash_table, t->record->key, t);
else
- g_hash_table_remove(c->hash_table, &e->record->key);
+ g_hash_table_remove(c->hash_table, e->record->key);
}
+ if (e->time_event)
+ flx_time_event_queue_remove(c->server->time_event_queue, e->time_event);
+
+ flx_subscription_notify(c->server, c->interface, e->record, FLX_SUBSCRIPTION_REMOVE);
+
flx_record_unref(e->record);
+
g_free(e);
}
g_assert(r);
for (e = flx_cache_lookup_key(c, r->key); e; e = e->by_name_next)
- if (e->record->size == r->size && !memcmp(e->record->data, r->data, r->size))
+ if (flx_record_equal_no_ttl(e->record, r))
return e;
return NULL;
}
-flxCacheEntry *flx_cache_update(flxCache *c, flxRecord *r, gboolean unique, const flxAddress *a) {
- flxCacheEntry *e, *t;
+static void next_expiry(flxCache *c, flxCacheEntry *e, guint percent);
+
+static void elapse_func(flxTimeEvent *t, void *userdata) {
+ flxCacheEntry *e = userdata;
- g_assert(c);
- g_assert(r);
+ g_assert(t);
+ g_assert(e);
- if ((t = e = flx_cache_lookup_key(c, r->key))) {
+ if (e->state == FLX_CACHE_FINAL) {
+ remove_entry(e->cache, e, TRUE);
+ g_message("Removing entry from cache due to expiration");
+ } else {
+ guint percent = 0;
+
+ switch (e->state) {
+ case FLX_CACHE_VALID:
+ e->state = FLX_CACHE_EXPIRY1;
+ percent = 85;
+ break;
+
+ case FLX_CACHE_EXPIRY1:
+ e->state = FLX_CACHE_EXPIRY2;
+ percent = 90;
+ break;
+ case FLX_CACHE_EXPIRY2:
+ e->state = FLX_CACHE_EXPIRY3;
+ percent = 95;
+ break;
+
+ case FLX_CACHE_EXPIRY3:
+ e->state = FLX_CACHE_FINAL;
+ percent = 100;
+ break;
+
+ default:
+ ;
+ }
- if (unique) {
- flxCacheEntry *n;
- /* Drop all entries but the first which we replace */
+ g_assert(percent > 0);
- while (e->by_name_next)
- remove_entry(c, e->by_name_next, TRUE);
+ g_message("Requesting cache entry update at %i%%.", percent);
- g_free(e->record->data);
- e->record->data = g_memdup(r->data, r->size);
- e->record->size = r->size;
- e->record->ttl = r->ttl;
+ /* Request a cache update */
+ flx_interface_post_query(e->cache->interface, e->record->key);
- } else {
- /* Look for exactly the same entry */
+ /* Check again later */
+ next_expiry(e->cache, e, percent);
+ }
+}
- for (; e; e = e->by_name_next) {
- if (e->record->size == r->size &&
- !memcmp(e->record->data, r->data, r->size)) {
+static void update_time_event(flxCache *c, flxCacheEntry *e) {
+ g_assert(c);
+ g_assert(e);
+
+ if (e->time_event)
+ flx_time_event_queue_update(c->server->time_event_queue, e->time_event, &e->expiry);
+ else
+ e->time_event = flx_time_event_queue_add(c->server->time_event_queue, &e->expiry, elapse_func, e);
+}
- /* We found it, so let's update the TTL */
- e->record->ttl = r->ttl;
- break;
- }
- }
- }
- }
+static void next_expiry(flxCache *c, flxCacheEntry *e, guint percent) {
+ gulong usec;
- if (!e) {
- /* No entry found, therefore we create a new one */
-
- e = g_new(flxCacheEntry, 1);
- e->node = NULL;
+ g_assert(c);
+ g_assert(e);
+ g_assert(percent > 0 && percent <= 100);
- e->record = flx_record_ref(r);
- FLX_LLIST_PREPEND(flxCacheEntry, by_name, t, e);
- g_hash_table_replace(c->hash_table, e->record->key, e);
- }
+ e->expiry = e->timestamp;
+
+ usec = e->record->ttl * 10000;
- e->origin = *a;
+ /* 2% jitter */
+ usec = g_random_int_range(usec*percent, usec*(percent+2));
- g_get_current_time(&e->timestamp);
- e->expiry = e->timestamp;
- g_time_val_add(&e->expiry, e->record->ttl * 1000000);
+ g_time_val_add(&e->expiry, usec);
+ update_time_event(c, e);
+}
+
+void flx_cache_update(flxCache *c, flxRecord *r, gboolean unique, const flxAddress *a) {
+ flxCacheEntry *e, *t;
+ gchar *txt;
+
+ g_assert(c);
+ g_assert(r && r->ref >= 1);
+
+ g_message("cache update: %s", (txt = flx_record_to_string(r)));
+ g_free(txt);
+
+ if (r->ttl == 0) {
- e->state = FLX_CACHE_VALID;
+ /* This is a goodbye request */
- return e;
+ if ((e = flx_cache_lookup_record(c, r))) {
+
+ e->state = FLX_CACHE_FINAL;
+ g_get_current_time(&e->timestamp);
+ e->expiry = e->timestamp;
+ g_time_val_add(&e->expiry, 1000000); /* 1s */
+ update_time_event(c, e);
+ }
+
+ } else {
+
+ /* This is an update request */
+
+ if ((t = e = flx_cache_lookup_key(c, r->key))) {
+
+ if (unique) {
+
+ /* For unique records, remove all entries but one */
+ while (e->by_name_next)
+ remove_entry(c, e->by_name_next, TRUE);
+
+ } else {
+
+ /* For non-unique record, look for exactly the same entry */
+ for (; e; e = e->by_name_next)
+ if (flx_record_equal_no_ttl(e->record, r))
+ break;
+ }
+ }
+
+ if (e) {
+
+/* g_message("found matching cache entry"); */
+
+ /* We are the first in the linked list so let's replace the hash table key with the new one */
+ if (e->by_name_prev == NULL)
+ g_hash_table_replace(c->hash_table, r->key, e);
+
+ /* Notify subscribers */
+ if (!flx_record_equal_no_ttl(e->record, r))
+ flx_subscription_notify(c->server, c->interface, r, FLX_SUBSCRIPTION_CHANGE);
+
+ /* Update the record */
+ flx_record_unref(e->record);
+ e->record = flx_record_ref(r);
+
+ } else {
+ /* No entry found, therefore we create a new one */
+
+/* g_message("couldn't find matching cache entry"); */
+
+ e = g_new(flxCacheEntry, 1);
+ e->cache = c;
+ e->time_event = NULL;
+ e->record = flx_record_ref(r);
+ FLX_LLIST_PREPEND(flxCacheEntry, by_name, t, e);
+ g_hash_table_replace(c->hash_table, e->record->key, t);
+
+ /* Notify subscribers */
+ flx_subscription_notify(c->server, c->interface, e->record, FLX_SUBSCRIPTION_NEW);
+ }
+
+ e->origin = *a;
+ g_get_current_time(&e->timestamp);
+ next_expiry(c, e, 80);
+ e->state = FLX_CACHE_VALID;
+ }
}
void flx_cache_drop_key(flxCache *c, flxKey *k) {
if ((e = flx_cache_lookup_record(c, r)))
remove_entry(c, e, TRUE);
}
+
+static void func(gpointer key, gpointer data, gpointer userdata) {
+ flxCacheEntry *e = data;
+ flxKey *k = key;
+ gchar *t;
+
+ t = flx_record_to_string(e->record);
+ fprintf((FILE*) userdata, "%s\n", t);
+ g_free(t);
+}
+
+void flx_cache_dump(flxCache *c, FILE *f) {
+ g_assert(c);
+ g_assert(f);
+
+ fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n");
+ g_hash_table_foreach(c->hash_table, func, f);
+}