|
1 | 1 | package timecache
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "sync"
|
5 | 6 | "time"
|
6 |
| - |
7 |
| - "github.com/emirpasic/gods/maps/linkedhashmap" |
8 | 7 | )
|
9 | 8 |
|
10 |
| -// LastSeenCache is a LRU cache that keeps entries for up to a specified time duration. After this duration has elapsed, |
11 |
| -// "old" entries will be purged from the cache. |
12 |
| -// |
13 |
| -// It's also a "sliding window" cache. Every time an unexpired entry is seen again, its timestamp slides forward. This |
14 |
| -// keeps frequently occurring entries cached and prevents them from being propagated, especially because of network |
15 |
| -// issues that might increase the number of duplicate messages in the network. |
16 |
| -// |
17 |
| -// Garbage collection of expired entries is event-driven, i.e. it only happens when there is a new entry added to the |
18 |
| -// cache. This should be ok - if existing entries are being looked up then the cache is not growing, and when a new one |
19 |
| -// appears that would grow the cache, garbage collection will attempt to reduce the pressure on the cache. |
20 |
| -// |
21 |
| -// This implementation is heavily inspired by https://github.com/whyrusleeping/timecache. |
| 9 | +// LastSeenCache is a time cache that extends the expiry of a seen message when added |
| 10 | +// or checked for presence with Has.. |
22 | 11 | type LastSeenCache struct {
|
23 |
| - m *linkedhashmap.Map |
24 |
| - span time.Duration |
25 |
| - guard *sync.Mutex |
| 12 | + lk sync.Mutex |
| 13 | + m map[string]time.Time |
| 14 | + ttl time.Duration |
| 15 | + |
| 16 | + done func() |
26 | 17 | }
|
27 | 18 |
|
28 |
| -func newLastSeenCache(span time.Duration) TimeCache { |
29 |
| - return &LastSeenCache{ |
30 |
| - m: linkedhashmap.New(), |
31 |
| - span: span, |
32 |
| - guard: new(sync.Mutex), |
| 19 | +var _ TimeCache = (*LastSeenCache)(nil) |
| 20 | + |
| 21 | +func newLastSeenCache(ttl time.Duration) *LastSeenCache { |
| 22 | + tc := &LastSeenCache{ |
| 23 | + m: make(map[string]time.Time), |
| 24 | + ttl: ttl, |
33 | 25 | }
|
34 |
| -} |
35 | 26 |
|
36 |
| -func (tc *LastSeenCache) Add(s string) { |
37 |
| - tc.guard.Lock() |
38 |
| - defer tc.guard.Unlock() |
| 27 | + ctx, done := context.WithCancel(context.Background()) |
| 28 | + tc.done = done |
| 29 | + go background(ctx, &tc.lk, tc.m) |
39 | 30 |
|
40 |
| - tc.add(s) |
| 31 | + return tc |
| 32 | +} |
41 | 33 |
|
42 |
| - // Garbage collect expired entries |
43 |
| - // TODO(#515): Do GC in the background |
44 |
| - tc.gc() |
| 34 | +func (tc *LastSeenCache) Done() { |
| 35 | + tc.done() |
45 | 36 | }
|
46 | 37 |
|
47 |
| -func (tc *LastSeenCache) add(s string) { |
48 |
| - // We don't need a lock here because this function is always called with the lock already acquired. |
| 38 | +func (tc *LastSeenCache) Add(s string) bool { |
| 39 | + tc.lk.Lock() |
| 40 | + defer tc.lk.Unlock() |
49 | 41 |
|
50 |
| - // If an entry already exists, remove it and add a new one to the back of the list to maintain temporal ordering and |
51 |
| - // an accurate sliding window. |
52 |
| - tc.m.Remove(s) |
53 |
| - now := time.Now() |
54 |
| - tc.m.Put(s, &now) |
55 |
| -} |
| 42 | + _, ok := tc.m[s] |
| 43 | + tc.m[s] = time.Now().Add(tc.ttl) |
56 | 44 |
|
57 |
| -func (tc *LastSeenCache) gc() { |
58 |
| - // We don't need a lock here because this function is always called with the lock already acquired. |
59 |
| - iter := tc.m.Iterator() |
60 |
| - for iter.Next() { |
61 |
| - key := iter.Key() |
62 |
| - ts := iter.Value().(*time.Time) |
63 |
| - // Exit if we've found an entry with an unexpired timestamp. Since we're iterating in order of insertion, all |
64 |
| - // entries hereafter will be unexpired. |
65 |
| - if time.Since(*ts) <= tc.span { |
66 |
| - return |
67 |
| - } |
68 |
| - tc.m.Remove(key) |
69 |
| - } |
| 45 | + return !ok |
70 | 46 | }
|
71 | 47 |
|
72 | 48 | func (tc *LastSeenCache) Has(s string) bool {
|
73 |
| - tc.guard.Lock() |
74 |
| - defer tc.guard.Unlock() |
| 49 | + tc.lk.Lock() |
| 50 | + defer tc.lk.Unlock() |
75 | 51 |
|
76 |
| - // If the entry exists and has not already expired, slide it forward. |
77 |
| - if ts, found := tc.m.Get(s); found { |
78 |
| - if t := ts.(*time.Time); time.Since(*t) <= tc.span { |
79 |
| - tc.add(s) |
80 |
| - return true |
81 |
| - } |
| 52 | + _, ok := tc.m[s] |
| 53 | + if ok { |
| 54 | + tc.m[s] = time.Now().Add(tc.ttl) |
82 | 55 | }
|
83 |
| - return false |
| 56 | + |
| 57 | + return ok |
84 | 58 | }
|
0 commit comments