-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
99 lines (90 loc) · 1.77 KB
/
map.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
87
88
89
90
91
92
93
94
95
96
97
98
99
package httprl
import (
"sync"
"time"
)
// Map is a rate limiter implementation using a map and goroutine
// to expire keys.
type Map struct {
m sync.Mutex
s map[string]*rldata
p time.Duration
stop chan struct{}
}
type rldata struct {
Count uint64
Expire time.Time
}
// NewMap creates and initializes a new Map. The precision determines
// how often the map is scanned for expired keys, in seconds.
func NewMap(precision int32) *Map {
return &Map{
s: make(map[string]*rldata),
p: time.Duration(precision) * time.Second,
}
}
// Hit implements the httprl.Backend interface.
func (m *Map) Hit(key string, ttlsec int32) (count uint64, remttl int32, err error) {
m.m.Lock()
defer m.m.Unlock()
v, ok := m.s[key]
if !ok {
m.s[key] = &rldata{
Count: 1,
Expire: time.Now().Add(time.Duration(ttlsec) * time.Second),
}
return 1, ttlsec, nil
}
v.Count++
rttl := v.Expire.Sub(time.Now()).Seconds()
if rttl < 1 {
return v.Count, 0, nil
}
return v.Count, int32(rttl), nil
}
// Start starts the internal goroutine that scans the map for
// expired keys and remove them.
func (m *Map) Start() {
m.m.Lock()
defer m.m.Unlock()
if m.stop != nil {
return
}
m.stop = make(chan struct{})
ready := make(chan struct{})
go m.run(ready)
<-ready
}
// Stop stops the internal goroutine started by Start.
func (m *Map) Stop() {
m.m.Lock()
defer m.m.Unlock()
if m.stop != nil {
close(m.stop)
}
}
func (m *Map) run(ready chan struct{}) {
tick := time.NewTicker(m.p)
close(ready)
for {
select {
case <-m.stop:
tick.Stop()
m.m.Lock()
m.stop = nil
m.m.Unlock()
case <-tick.C:
m.clear()
}
}
}
func (m *Map) clear() {
now := time.Now()
m.m.Lock()
for k, v := range m.s {
if v.Expire.Sub(now) <= 0 {
delete(m.s, k)
}
}
m.m.Unlock()
}