/
filter_cooldown.go
73 lines (63 loc) · 1.96 KB
/
filter_cooldown.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
package exevent
import (
"github.com/ichenhe/syncthing-hook/domain"
"github.com/ichenhe/syncthing-hook/utils/clock"
"go.uber.org/zap"
)
// options ----------------------------------------------------------------------------------
type CoolDownFilterOptions struct {
timeProvider domain.TimeProvider
}
type CoolDownFilterOption func(opt *CoolDownFilterOptions)
func CoolDownFilterTimeProvider(provider domain.TimeProvider) CoolDownFilterOption {
return func(opt *CoolDownFilterOptions) {
opt.timeProvider = provider
}
}
// -----------------------------------------------------------------------------------------
type CoolDownFilter struct {
baseHandler
*CoolDownFilterOptions
// coolDown indicates the maximum frequency in millisecond that this event can be triggered.
// All events during coolDown period will be discarded. coolDown is calculated based on the moment receive and
// deal with the event, not the moment it occurred.
coolDown int64
logger *zap.SugaredLogger
// the system time when the last event was processed and sent
lastTime int64
}
func NewCoolDownFilter(cooldown int64, logger *zap.SugaredLogger, options ...CoolDownFilterOption) *CoolDownFilter {
_logger := wrapLogger(logger, "CoolDownFilter")
var _cooldown int64
if cooldown < 0 {
_logger.Warnf("'cooldown' must >= 0, use 0 instead of '%d'", cooldown)
_cooldown = 0
} else {
_cooldown = cooldown
}
opt := &CoolDownFilterOptions{
timeProvider: &clock.TimeProvider{},
}
for _, f := range options {
f(opt)
}
return &CoolDownFilter{
CoolDownFilterOptions: opt,
coolDown: _cooldown,
logger: _logger,
lastTime: 0,
}
}
func (h *CoolDownFilter) Handle(event *domain.Event) {
if h.coolDown == 0 {
h.callNext(event)
return
}
sendTime := h.timeProvider.NowUnixMilli()
if sendTime-h.lastTime < h.coolDown {
h.logger.Debugln("still cooling down, discard event")
return // still cooling down
}
h.lastTime = sendTime
h.callNext(event)
}