-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteface.go
67 lines (51 loc) · 1.12 KB
/
inteface.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
package filter_traffic
import "sync/atomic"
type (
PerValueFilter[T any] interface {
GetCounter(T) *Counter
GetLimit(T) uint64
}
Counter struct {
ResetNumber uint64
counter atomic.Uint64
}
GlobalFilter[T any] struct {
Limit uint64
Counter *Counter
}
PerValueFilterMap[T comparable] struct {
limits map[T]uint64
counter map[T]*Counter
}
)
var (
_ PerValueFilter[string] = PerValueFilterMap[string]{}
_ PerValueFilter[string] = GlobalFilter[string]{}
)
func (p GlobalFilter[T]) GetCounter(key T) *Counter {
return p.Counter
}
func NewPerValueFilterMap[T comparable](max uint64, limits map[T]uint64) PerValueFilterMap[T] {
counter := make(map[T]*Counter, len(limits))
for key := range limits {
counter[key] = &Counter{ResetNumber: max}
}
return PerValueFilterMap[T]{
limits,
counter,
}
}
func (g GlobalFilter[T]) GetLimit(key T) uint64 {
return g.Limit
}
func (p PerValueFilterMap[T]) GetCounter(key T) *Counter {
counter, ok := p.counter[key]
if !ok {
return nil
}
return counter
}
func (p PerValueFilterMap[T]) GetLimit(key T) uint64 {
limit := p.limits[key]
return limit
}