Skip to content

Commit

Permalink
Move constants to priority package and readme , docs update (#14)
Browse files Browse the repository at this point in the history
* update readme

* update readme and docs

* more updates

* move constants to priority package

* more readme and docs
  • Loading branch information
vivek-ng committed Nov 23, 2020
1 parent 819be70 commit 04407ba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

## About

concurrency-limiter is a minimalistic package that allows you to limit the number of concurrent goroutines accessing a resource with support for
timeouts and priority of goroutines.
concurrency-limiter allows you to limit the number of goroutines accessing a resource with support for
timeouts and dynamic priority of goroutines.

## Examples

Expand Down Expand Up @@ -36,7 +36,7 @@ number of concurrent goroutines is greater than the limit specified.

```go
nl := priority.NewLimiter(3)
nl.Wait(constants.High)
nl.Wait(priority.High)
Execute......
nl.Finish()
```
Expand All @@ -48,7 +48,7 @@ given the maximum preference because it is of high priority. In the case of tie

```go
nl := priority.NewLimiter(3).WithDynamicPriority(5)
nl.Wait(constants.Low)
nl.Wait(priority.Low)
Execute......
nl.Finish()
```
Expand Down
10 changes: 0 additions & 10 deletions constants/constants.go

This file was deleted.

20 changes: 15 additions & 5 deletions priority/priorityRateLimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ import (
"sync"
"time"

"github.com/vivek-ng/concurrency-limiter/constants"
"github.com/vivek-ng/concurrency-limiter/queue"
)

// PriorityValue defines the priority values of goroutines.
// Greater priority value means higher priority
type PriorityValue int

const (
Low PriorityValue = 1
Medium PriorityValue = 2
MediumHigh PriorityValue = 3
High PriorityValue = 4
)

// limit: max number of concurrent goroutines that can access aresource
//
// count: current number of goroutines accessing a resource
Expand Down Expand Up @@ -59,13 +69,13 @@ func (p *PriorityLimiter) WithTimeout(timeout int) *PriorityLimiter {
// Wait method waits if the number of concurrent requests is more than the limit specified.
// If the priority of two goroutines are same , the FIFO order is followed.
// Greater priority value means higher priority.
// priority must be one fo the values specified by constants.PriorityValue
// priority must be one fo the values specified by PriorityValue
//
// Low = 1
// Medium = 2
// MediumHigh = 3
// High = 4
func (p *PriorityLimiter) Wait(priority constants.PriorityValue) {
func (p *PriorityLimiter) Wait(priority PriorityValue) {
ok, w := p.proceed(priority)
if ok {
return
Expand All @@ -83,7 +93,7 @@ func (p *PriorityLimiter) Wait(priority constants.PriorityValue) {
return
case <-ticker.C:
p.mu.Lock()
if w.Priority < int(constants.High) {
if w.Priority < int(High) {
currentPriority := w.Priority
p.waitList.Update(w, currentPriority+1)
}
Expand All @@ -105,7 +115,7 @@ func (p *PriorityLimiter) Wait(priority constants.PriorityValue) {
// proceed will return true if the number of concurrent requests is less than the limit else it
// will add the goroutine to the priority queue and will return a channel. This channel is used by goutines to
// check for signal when they are granted access to use the resource.
func (p *PriorityLimiter) proceed(priority constants.PriorityValue) (bool, *queue.Item) {
func (p *PriorityLimiter) proceed(priority PriorityValue) (bool, *queue.Item) {
p.mu.Lock()
defer p.mu.Unlock()

Expand Down
7 changes: 3 additions & 4 deletions priority/priorityRateLimiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/vivek-ng/concurrency-limiter/constants"
"github.com/vivek-ng/concurrency-limiter/queue"
)

Expand All @@ -18,7 +17,7 @@ func TestPriorityLimiter(t *testing.T) {
for i := 0; i < 5; i++ {
go func(pr int) {
defer wg.Done()
nl.Wait(constants.PriorityValue(pr))
nl.Wait(PriorityValue(pr))
}(i)
}
time.Sleep(200 * time.Millisecond)
Expand Down Expand Up @@ -55,8 +54,8 @@ func TestDynamicPriority(t *testing.T) {
pVal = nl.waitList.Top()
pValItem = pVal.(queue.Item)
expectedVal2 := pValItem.Priority
assert.GreaterOrEqual(t, expectedVal1, int(constants.High))
assert.GreaterOrEqual(t, expectedVal2, int(constants.High))
assert.GreaterOrEqual(t, expectedVal1, int(High))
assert.GreaterOrEqual(t, expectedVal2, int(High))
}

func TestPriorityLimiter_Timeout(t *testing.T) {
Expand Down

0 comments on commit 04407ba

Please sign in to comment.