-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement priority rate limiter (#2)
- Loading branch information
vivek-ng
authored
Nov 22, 2020
1 parent
fa25e71
commit c24be6e
Showing
4 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package priority | ||
|
||
import ( | ||
"container/heap" | ||
"sync" | ||
|
||
"github.com/vivek-ng/concurrency-limiter/queue" | ||
) | ||
|
||
type PriorityLimiter struct { | ||
count int | ||
limit int | ||
mu sync.Mutex | ||
waitList queue.PriorityQueue | ||
} | ||
|
||
func NewLimiter(limit int) *PriorityLimiter { | ||
pq := make(queue.PriorityQueue, 0) | ||
nl := &PriorityLimiter{ | ||
limit: limit, | ||
waitList: pq, | ||
} | ||
|
||
heap.Init(&pq) | ||
return nl | ||
} | ||
|
||
func (p *PriorityLimiter) Wait(priority int) { | ||
ok, ch := p.proceed(priority) | ||
if !ok { | ||
<-ch | ||
} | ||
} | ||
|
||
func (p *PriorityLimiter) proceed(priority int) (bool, chan struct{}) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
if p.count < p.limit { | ||
p.count++ | ||
return true, nil | ||
} | ||
ch := make(chan struct{}) | ||
w := &queue.Item{ | ||
Priority: priority, | ||
Done: ch, | ||
} | ||
heap.Push(&p.waitList, w) | ||
return false, ch | ||
} | ||
|
||
func (p *PriorityLimiter) Finish() { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
p.count -= 1 | ||
ele := heap.Pop(&p.waitList) | ||
if ele == nil { | ||
return | ||
} | ||
it := ele.(*queue.Item) | ||
it.Done <- struct{}{} | ||
close(it.Done) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package priority | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/vivek-ng/concurrency-limiter/queue" | ||
) | ||
|
||
func TestPriorityLimiter(t *testing.T) { | ||
nl := NewLimiter(3) | ||
var wg sync.WaitGroup | ||
wg.Add(5) | ||
|
||
for i := 0; i < 5; i++ { | ||
go func(pr int) { | ||
defer wg.Done() | ||
nl.Wait(pr) | ||
}(i) | ||
} | ||
time.Sleep(200 * time.Millisecond) | ||
assert.Equal(t, 2, nl.waitList.Len()) | ||
pVal := nl.waitList.Top() | ||
pValItem := pVal.(queue.Item) | ||
expectedVal1 := pValItem.Priority | ||
nl.Finish() | ||
pVal = nl.waitList.Top() | ||
pValItem = pVal.(queue.Item) | ||
expectedVal2 := pValItem.Priority | ||
assert.Greater(t, expectedVal1, expectedVal2) | ||
nl.Finish() | ||
wg.Wait() | ||
assert.Equal(t, 0, nl.waitList.Len()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters