Skip to content

Commit 64db6c5

Browse files
committed
Bug fix: the sequel
1 parent 1b86097 commit 64db6c5

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

models.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type Identity interface {
2626
}
2727

2828
type rated struct {
29-
seen atomic.Value
30-
locker *uint32
29+
seen *atomic.Value
30+
locker uint32
3131
}
3232

3333
// Limiter implements an Enforcer to create an arbitrary ratelimiter.
@@ -42,7 +42,7 @@ type Limiter struct {
4242
Debug bool
4343

4444
count atomic.Value
45-
known map[interface{}]*rated
45+
known map[interface{}]rated
4646
}
4747

4848
// Policy defines the mechanics of our ratelimiter.

ratelimiter.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func newLimiter(policy Policy) *Limiter {
5454
q := new(Limiter)
5555
q.Ruleset = policy
5656
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+
5859
return q
5960
}
6061

@@ -68,11 +69,11 @@ func (q *Limiter) DebugChannel() chan string {
6869
return debugChannel
6970
}
7071

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) {
7374
time.Sleep(10 * time.Millisecond)
7475
}
75-
defer atomic.StoreUint32(s.locker, stateUnlocked)
76+
defer atomic.StoreUint32(&s.locker, stateUnlocked)
7677

7778
if s.seen.Load() == nil {
7879
s.seen.Store(1)
@@ -83,8 +84,10 @@ func (s *rated) inc() {
8384

8485
func (q *Limiter) strictLogic(src string, count int) {
8586
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+
}
8891
}
8992
q.known[src].inc()
9093
extwindow := q.Ruleset.Window + q.known[src].seen.Load().(int)

0 commit comments

Comments
 (0)