Skip to content

Commit

Permalink
Remove waiters when context is done (#24)
Browse files Browse the repository at this point in the history
* remove waiters when context is done

* add test for context done with dynamic priority and timeout set
  • Loading branch information
vivek-ng authored Nov 24, 2020
1 parent d9c5b29 commit bf13f9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions priority/priorityRateLimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ func (p *PriorityLimiter) dynamicPriorityAndTimeout(ctx context.Context, w *queu
p.removeWaiter(w)
return
case <-ticker.C:
// edge case where we receive ctx.Done and ticker.C at the same time...
select {
case <-ctx.Done():
p.removeWaiter(w)
return
default:
}
Expand Down
20 changes: 20 additions & 0 deletions priority/priorityRateLimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,23 @@ func TestDynamicPriorityAndTimeout(t *testing.T) {
time.Sleep(200 * time.Millisecond)
assert.Zero(t, nl.waitListSize())
}

func TestDynamicPriorityWithTimeout_ContextDone(t *testing.T) {
nl := NewLimiter(3,
WithTimeout(300),
WithDynamicPriority(5))
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i < 5; i++ {
go func(pr int) {
defer wg.Done()
nl.Wait(ctx, 1)
}(i)
}
time.Sleep(100 * time.Millisecond)
cancel()
time.Sleep(100 * time.Millisecond)
assert.Zero(t, nl.waitListSize())
}

0 comments on commit bf13f9b

Please sign in to comment.