Skip to content

Commit

Permalink
increase go routine count when timepout happens
Browse files Browse the repository at this point in the history
  • Loading branch information
vivek-ng committed Nov 22, 2020
1 parent f255ea8 commit 003536c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
11 changes: 2 additions & 9 deletions rateLimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ func (l *Limiter) Wait() {
if l.timeout != nil {
select {
case <-ch:
case <-time.After((time.Duration(*l.timeout) * time.Second)):
case <-time.After((time.Duration(*l.timeout) * time.Millisecond)):
l.mu.Lock()
for w := l.waitList.Front(); w != nil; w = w.Next() {
ele := w.Value.(waiter)
if ele.done == ch {
close(ch)
l.waitList.Remove(w)
l.count += 1
break
}
}
Expand Down Expand Up @@ -74,7 +75,6 @@ func (l *Limiter) Finish() {
l.mu.Lock()
defer l.mu.Unlock()
l.count -= 1
l.count = max(0, l.count)
first := l.waitList.Front()
if first == nil {
return
Expand All @@ -83,10 +83,3 @@ func (l *Limiter) Finish() {
w.done <- struct{}{}
close(w.done)
}

func max(a, b int) int {
if a >= b {
return a
}
return b
}
5 changes: 3 additions & 2 deletions rateLimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestConcurrentRateLimiterBlocking(t *testing.T) {
}

func TestConcurrentRateLimiterTimeout(t *testing.T) {
l := NewLimiter(2).WithTimeout(2)
l := NewLimiter(2).WithTimeout(300)

var wg sync.WaitGroup
wg.Add(5)
Expand All @@ -58,9 +58,10 @@ func TestConcurrentRateLimiterTimeout(t *testing.T) {
l.Wait()
}()
}
time.Sleep(3 * time.Second)
time.Sleep(500 * time.Millisecond)
wg.Wait()
l.Finish()
l.Finish()
assert.Equal(t, 3, l.count)
assert.Equal(t, 0, l.waitList.Len())
}

0 comments on commit 003536c

Please sign in to comment.