-
Notifications
You must be signed in to change notification settings - Fork 0
/
deduper.go
86 lines (75 loc) · 2.76 KB
/
deduper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package conductor
import (
"time"
)
// DeDuplication is the based interface for handling deduplication of messages.
// Use this for ensuring messages aren't processed twice in the hub.
type DeDuplication interface {
Start()
Add(message *Message)
Remove(message *Message)
IsDuplicate(message *Message) bool
}
// StandardDeDuplication is the default implmentation of DeDuplication.
// It works by holding the message in memory for a period of time waiting to see if a duplication will arrive.
// If "durablity" is enabled for the message it will be removed as soon as a message is fin'ed. - Might not do this...
// TODO: look at possible race condition with the timestamps map...
type StandardDeDuplication struct {
timestamps map[string]time.Time
ttl time.Duration //ttl is Time To Live in the timestamp list. A good default value for this is X seconds.
ticker *time.Ticker
}
// NewDeDuper creates a StandardDeDuplication to use.
// tick is how often the cleanup sweep should happen.
// ttl is how long a message should live in the deduper cache.
func NewDeDuper(tick, ttl time.Duration) *StandardDeDuplication {
return &StandardDeDuplication{timestamps: make(map[string]time.Time),
ticker: time.NewTicker(ttl),
ttl: ttl}
}
// Start kicks off the ticker so it can do a sweep based on the ttl and cleanup any stale messages
// that didn't get purged (this is much more likely with messages that aren't durable).
func (deduper *StandardDeDuplication) Start() {
go deduper.doTick()
}
// Add puts a timestamp in the timestamps map based on the message's ID.
// The message will then be checked in the ticker's clean up sweep to remove the message if it is past the ttl.
func (deduper *StandardDeDuplication) Add(message *Message) {
if len(message.Uuid) == 0 {
return
}
deduper.timestamps[message.Uuid] = time.Now()
}
// Remove removes a message based on the ID of the message from the timestamp map.
func (deduper *StandardDeDuplication) Remove(message *Message) {
delete(deduper.timestamps, message.Uuid)
}
// IsDuplicate checks to see if the message has a duplicate id of any of the messages stored in the timestamp map.
func (deduper *StandardDeDuplication) IsDuplicate(message *Message) bool {
if len(message.Uuid) == 0 {
return false
}
_, exist := deduper.timestamps[message.Uuid]
return exist
}
func (deduper *StandardDeDuplication) doTick() {
defer func() {
deduper.ticker.Stop()
}()
for { // blocking loop with select to wait for stimulation.
select {
case <-deduper.ticker.C:
deduper.cleanupSweep()
}
}
}
func (deduper *StandardDeDuplication) cleanupSweep() {
now := time.Now()
for key := range deduper.timestamps {
start := deduper.timestamps[key]
elapsed := now.Sub(start)
if elapsed > deduper.ttl {
delete(deduper.timestamps, key)
}
}
}