Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ func LimitRetry(s RetryStrategy, max int) RetryStrategy {
}

func (r *limitedRetry) NextBackoff() time.Duration {
if r.cnt.Load() >= r.max {
if r.cnt.Add(1) > r.max {
return 0
}
r.cnt.Add(1)
return r.s.NextBackoff()
}

Expand Down
31 changes: 31 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package redislock_test

import (
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -60,3 +62,32 @@ func TestLimitRetry(t *testing.T) {
}
}
}

func TestLimitRetry_concurrent(t *testing.T) {
const (
max = 100
callers = 64
)

var allowed atomic.Int64
retry := LimitRetry(LinearBackoff(time.Millisecond), max)

var wg sync.WaitGroup
start := make(chan struct{})
for range callers {
wg.Go(func() {
<-start
for range max {
if retry.NextBackoff() > 0 {
allowed.Add(1)
}
}
})
}
close(start)
wg.Wait()

if got := allowed.Load(); got != max {
t.Fatalf("expected exactly %d allowed retries, got %d", max, got)
}
}