File tree Expand file tree Collapse file tree 2 files changed +12
-9
lines changed Expand file tree Collapse file tree 2 files changed +12
-9
lines changed Original file line number Diff line number Diff line change @@ -26,8 +26,8 @@ type Identity interface {
26
26
}
27
27
28
28
type rated struct {
29
- seen atomic.Value
30
- locker * uint32
29
+ seen * atomic.Value
30
+ locker uint32
31
31
}
32
32
33
33
// Limiter implements an Enforcer to create an arbitrary ratelimiter.
@@ -42,7 +42,7 @@ type Limiter struct {
42
42
Debug bool
43
43
44
44
count atomic.Value
45
- known map [interface {}]* rated
45
+ known map [interface {}]rated
46
46
}
47
47
48
48
// Policy defines the mechanics of our ratelimiter.
Original file line number Diff line number Diff line change @@ -54,7 +54,8 @@ func newLimiter(policy Policy) *Limiter {
54
54
q := new (Limiter )
55
55
q .Ruleset = policy
56
56
q .Patrons = cache .New (time .Duration (q .Ruleset .Window )* time .Second , 5 * time .Second )
57
- q .known = make (map [interface {}]* rated )
57
+ q .known = make (map [interface {}]rated )
58
+
58
59
return q
59
60
}
60
61
@@ -68,11 +69,11 @@ func (q *Limiter) DebugChannel() chan string {
68
69
return debugChannel
69
70
}
70
71
71
- func (s * rated ) inc () {
72
- for ! atomic .CompareAndSwapUint32 (s .locker , stateUnlocked , stateLocked ) {
72
+ func (s rated ) inc () {
73
+ for ! atomic .CompareAndSwapUint32 (& s .locker , stateUnlocked , stateLocked ) {
73
74
time .Sleep (10 * time .Millisecond )
74
75
}
75
- defer atomic .StoreUint32 (s .locker , stateUnlocked )
76
+ defer atomic .StoreUint32 (& s .locker , stateUnlocked )
76
77
77
78
if s .seen .Load () == nil {
78
79
s .seen .Store (1 )
@@ -83,8 +84,10 @@ func (s *rated) inc() {
83
84
84
85
func (q * Limiter ) strictLogic (src string , count int ) {
85
86
if _ , ok := q .known [src ]; ! ok {
86
- atomic .StoreUint32 (q .known [src ].locker , stateUnlocked )
87
- q .known [src ]= & rated {seen : atomic.Value {}}
87
+ q .known [src ]= rated {
88
+ seen : & atomic.Value {},
89
+ locker : stateUnlocked ,
90
+ }
88
91
}
89
92
q .known [src ].inc ()
90
93
extwindow := q .Ruleset .Window + q .known [src ].seen .Load ().(int )
You can’t perform that action at this time.
0 commit comments