Skip to content

Commit

Permalink
Update readme and Docs (#13)
Browse files Browse the repository at this point in the history
* update readme

* update readme and docs

* more updates
  • Loading branch information
vivek-ng authored Nov 23, 2020
1 parent 322b973 commit 819be70
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,60 @@
## 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.
timeouts and priority of goroutines.

## Examples

### Limiter

```go
nl := limiter.New(3)
nl.Wait()
Execute......
nl.Finish()
```
In the above example , there can be a maximum of 3 goroutines accessing a resource concurrently. The other goroutines are added to the waiting list and are removed and given a
chance to access the resource in the FIFO order.

### Limiter with Timeout

```go
nl := limiter.New(3).WithTimeout(10)
nl.Wait()
Execute......
nl.Finish()
```
In the above example , the goroutines will wait for a maximum of 10 milliseconds. Goroutines will be removed from the waitlist after 10 ms even if the
number of concurrent goroutines is greater than the limit specified.

### Priority Limiter

```go
nl := priority.NewLimiter(3)
nl.Wait(constants.High)
Execute......
nl.Finish()
```

In Priority Limiter , goroutines with higher priority will be given preference to be removed from the waitlist. For instance in the above example , the goroutine will be
given the maximum preference because it is of high priority. In the case of tie between the priorities , the goroutines will be removed from the waitlist in the FIFO order.

### Priority Limiter with Dynamic priority

```go
nl := priority.NewLimiter(3).WithDynamicPriority(5)
nl.Wait(constants.Low)
Execute......
nl.Finish()
```
In Dynamic Priority Limiter , the goroutines with lower priority will get their priority increased periodically by the time period specified. For instance in the above example , the goroutine will get it's priority increased every 5 ms. This will ensure that goroutines with lower priority do not suffer from starvation. It's highly recommended to use Dynamic Priority Limiter to avoid starving low priority goroutines.

### Priority Limiter with Timeout

```go
nl := priority.NewLimiter(3).WithTimeout(30)
nl.Wait(constants.High)
Execute......
nl.Finish()
```
This is similar to the timeouts in the normal limiter. In the above example , goroutines will wait a maximum of 30 milliseconds.
6 changes: 4 additions & 2 deletions priority/priorityRateLimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
// waitList: Priority queue of goroutines waiting to access a resource. Goroutines will be added to
// this list if the number of concurrent requests are greater than the limit specified. Greater value for priority means
// higher priority for that particular goroutine.
//
// dynamicPeriod: If this field is specified , priority is increased for low priority goroutines periodically by the
// interval specified by dynamicPeriod
// interval specified by dynamicPeriod (in ms)
//
// timeout: If this field is specified , goroutines will be automatically removed from the waitlist
// after the time passes the timeout specified even if the number of concurrent requests is greater than the limit.
// after the time passes the timeout specified even if the number of concurrent requests is greater than the limit. (in ms)
type PriorityLimiter struct {
count int
limit int
Expand Down
5 changes: 4 additions & 1 deletion rateLimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ type waiter struct {
}

// limit: max number of concurrent goroutines that can access aresource
//
// count: current number of goroutines accessing a resource
//
// waitList: list of goroutines waiting to access a resource. Goroutines will be added to
// this list if the number of concurrent requests are greater than the limit specified
//
// timeout: If this field is specified , goroutines will be automatically removed from the waitlist
// after the time passes the timeout specified even if the number of concurrent requests is greater than the limit.
// after the time passes the timeout specified even if the number of concurrent requests is greater than the limit. (in ms)
type Limiter struct {
count int
limit int
Expand Down

0 comments on commit 819be70

Please sign in to comment.