diff options
author | David Benjamin <davidben@mit.edu> | 2013-08-19 05:49:00 -0400 |
---|---|---|
committer | Karl Ramm <kcr@1ts.org> | 2013-09-28 13:45:17 -0400 |
commit | fa474fe09f89d69a058eb85c863c8778e9dfb77a (patch) | |
tree | dd8245df5d14a067b415bbe1e1e7c7e305d4df0d | |
parent | 5984f552acaf748228dec50e6dbc62d6c2fb0fe3 (diff) |
Turn find_or_insert_uid's buffer into an LRU cache
If we see a duplicated packet, that means the server missed (or raced with) our
CLIENTACK, which means we should update the timestamp on the entry to reset the
aging.
-rw-r--r-- | lib/Zinternal.c | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/lib/Zinternal.c b/lib/Zinternal.c index cc03c66..ace1a81 100644 --- a/lib/Zinternal.c +++ b/lib/Zinternal.c @@ -134,8 +134,8 @@ find_or_insert_uid(ZUnique_Id_t *uid, time_t now; struct _filter *new; - long i, new_size; - int result; + long i, j, new_size; + int result, found = 0; /* Initialize the uid buffer if it hasn't been done already. */ if (!buffer) { @@ -168,18 +168,25 @@ find_or_insert_uid(ZUnique_Id_t *uid, /* Search for this uid in the buffer, starting from the end. */ for (i = start + num - 1; i >= start; i--) { result = memcmp(uid, &buffer[i % size].uid, sizeof(*uid)); - if (result == 0 && buffer[i % size].kind == kind) - return 1; + if (result == 0 && buffer[i % size].kind == kind) { + /* Remove it from the buffer. We'll re-add it at the end. */ + for (j = i; j < start + num - 1; j++) { + buffer[j % size] = buffer[(j + 1) % size]; + } + num--; + found = 1; + break; + } } - /* We didn't find it; stick it on the end */ + /* Whether or not we found it, stick it at the end. */ i = start + num; buffer[i % size].uid = *uid; buffer[i % size].kind = kind; buffer[i % size].t = now; num++; - return 0; + return found; } |