Skip to content

Commit

Permalink
Add more docs (#5)
Browse files Browse the repository at this point in the history
* add docs

* add more docs
  • Loading branch information
vivek-ng committed Nov 23, 2020
1 parent ad5425a commit 7e722fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 8 additions & 0 deletions priority/priorityRateLimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ func NewLimiter(limit int) *PriorityLimiter {
return nl
}

// 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.
func (p *PriorityLimiter) Wait(priority int) {
ok, ch := p.proceed(priority)
if !ok {
<-ch
}
}

// 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 int) (bool, chan struct{}) {
p.mu.Lock()
defer p.mu.Unlock()
Expand All @@ -49,6 +55,8 @@ func (p *PriorityLimiter) proceed(priority int) (bool, chan struct{}) {
return false, ch
}

// Finish will remove the goroutine from the priority queue and sends a signal
// to the waiting goroutine to access the resource
func (p *PriorityLimiter) Finish() {
p.mu.Lock()
defer p.mu.Unlock()
Expand Down
9 changes: 7 additions & 2 deletions rateLimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

// waiter is the individual goroutine waiting for accessing the request.
// waiter is the individual goroutine waiting for accessing the resource.
// waiter waits for the signal through the done channel.
type waiter struct {
done chan struct{}
Expand All @@ -31,7 +31,7 @@ func (l *Limiter) WithTimeout(timeout int) *Limiter {
return l
}

// Wait waits if the number of concurrent requests is more than the limit specified.
// Wait method waits if the number of concurrent requests is more than the limit specified.
// If a timeout is configured , then the goroutine will wait until the timeout occurs and then proceeds to
// access the resource irrespective of whether it has received a signal in the done channel.
func (l *Limiter) Wait() {
Expand Down Expand Up @@ -60,6 +60,9 @@ func (l *Limiter) Wait() {
<-ch
}

// proceed will return true if the number of concurrent requests is less than the limit else it
// will add the goroutine to the waiting list 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 (l *Limiter) proceed() (bool, chan struct{}) {
l.mu.Lock()
defer l.mu.Unlock()
Expand All @@ -76,6 +79,8 @@ func (l *Limiter) proceed() (bool, chan struct{}) {
return false, ch
}

// Finish will remove the goroutine from the waiting list and sends a signal
// to the waiting goroutine to access the resource
func (l *Limiter) Finish() {
l.mu.Lock()
defer l.mu.Unlock()
Expand Down

0 comments on commit 7e722fa

Please sign in to comment.