limiter

package module
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2020 License: MIT Imports: 4 Imported by: 0

README

concurrency-limiter

GoDoc reference example GoReportCard example codecov

About

concurrency-limiter allows you to limit the number of goroutines accessing a resource with support for timeouts , dynamic priority of goroutines and context cancellation of goroutines.

Examples

Limiter
    nl := limiter.New(3)
    ctx := context.Background()
    nl.Wait(ctx)
    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. If the context is cancelled , the goroutine is removed from the waitlist.

Limiter with Timeout
    nl := limiter.New(3,
    WithTimeout(10),
    )
    ctx := context.Background()
    nl.Wait(ctx)
    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
    nl := priority.NewLimiter(3)
    ctx := context.Background()
    nl.Wait(ctx , priority.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
    nl := priority.NewLimiter(3,
    WithDynamicPriority(5),
    )
    ctx := context.Background()
    nl.Wait(ctx , priority.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
    nl := priority.NewLimiter(3,
    WithTimeout(30),
    WithDynamicPriority(5),
    )
    ctx := context.Background()
    nl.Wait(ctx , priority.Low)
    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. The low priority goroutines will get their priority increased every 5 ms.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithTimeout added in v0.5.0

func WithTimeout(timeout int) func(*Limiter)

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.

Types

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

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. (in ms)

func New added in v0.4.2

func New(limit int, options ...Option) *Limiter

New creates an instance of *Limiter. Configure the Limiter with the options specified. Example: limiter.New(4, WithTimeout(5))

func (*Limiter) Finish

func (l *Limiter) Finish()

Finish will remove the goroutine from the waiting list and sends a signal to the waiting goroutine to access the resource

func (*Limiter) Wait

func (l *Limiter) Wait(ctx context.Context)

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.

type Option added in v0.5.0

type Option func(*Limiter)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL