multilimiter

package module
v0.0.2-0...-64a9025 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2020 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

multilimiter

Combined rate and concurrency limiter for Go

Documentation

Index

Constants

View Source
const DEFAULT_CONCURRENCY = 1
View Source
const DEFAULT_RATE = 1.0

Variables

View Source
var DeadlineExceeded = errors.New("Timeout Exceeded")
View Source
var LimiterStopped = errors.New("Limiter has been stopped")
View Source
var OutStream io.Writer

If Limiter.Execute() panicks the stack trace will be sent down OutStream The default value is os.Stdout

Functions

func CreateOptions

func CreateOptions(opts ...Option) *options

Creates an instance of options out of a slice of Options

Types

type BasicConcLimiter

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

func NewConcLimiter

func NewConcLimiter(size int) *BasicConcLimiter

Creates a new concurrency limiter if size is <= 1, a default of 1 will be used

func (*BasicConcLimiter) Acquire

func (me *BasicConcLimiter) Acquire(ctx context.Context) (Slot, error)

func (*BasicConcLimiter) Cancel

func (me *BasicConcLimiter) Cancel()

func (*BasicConcLimiter) Concurrency

func (me *BasicConcLimiter) Concurrency() int

func (*BasicConcLimiter) Wait

func (me *BasicConcLimiter) Wait()

type BasicLimiter

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

A limiter that supports limiting by concurrency and rate

func DefaultLimiter

func DefaultLimiter(rate float64, concurrency int) *BasicLimiter

func NewLimiter

func NewLimiter(opts ...Option) *BasicLimiter

func (*BasicLimiter) Execute

func (me *BasicLimiter) Execute(ctx context.Context, fn func(context.Context)) error

Once available time or concurrency becomes available execute function fn in a go routine DeadlineExceeded is returned if the timeout elapses before rate and concurrency slots can be acquired

func (*BasicLimiter) Stop

func (me *BasicLimiter) Stop()

Stops the limiter

func (*BasicLimiter) Wait

func (me *BasicLimiter) Wait()

Waits for all executions to complete before returning

type BasicRateLimiter

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

func (*BasicRateLimiter) Cancel

func (me *BasicRateLimiter) Cancel()

func (*BasicRateLimiter) Rate

func (me *BasicRateLimiter) Rate() float64

func (*BasicRateLimiter) Wait

func (me *BasicRateLimiter) Wait(ctx context.Context) error

func (*BasicRateLimiter) XXX_TEST_Wait

func (me *BasicRateLimiter) XXX_TEST_Wait(tokens int64, ctx context.Context) error

This allows us to force a timeout in testing by setting the number of desired tokens to a high value

type Cancelable

type Cancelable interface {
	Cancel() bool
	IsCanceled() bool
	Done() <-chan struct{}
}

TODO: This interface may just be a waste, it's not being used right now

type Canceler

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

func NewCanceler

func NewCanceler() *Canceler

func (*Canceler) Cancel

func (me *Canceler) Cancel() bool

Cancels returns true if we were already canceled; otherwise false

func (*Canceler) Done

func (me *Canceler) Done() <-chan struct{}

func (*Canceler) IsCanceled

func (me *Canceler) IsCanceled() bool

type ConcLimitOption

type ConcLimitOption struct {
	Limiter ConcLimiter
}

option for controlling concurrency

type ConcLimiter

type ConcLimiter interface {
	// Wait for a slot to become available
	// only DeadlineExceeded and LimiterStopped errors can be returned
	Acquire(ctx context.Context) (Slot, error)
	// Put the slot back into the pool
	// Release()
	// Cancels processing outstanding acquisition requests
	Cancel()
	// The configured concurrency
	Concurrency() int
	// Wait for all Slots to be returned
	Wait()
}

Provides a thread-safe mechanism for acquiring and releasing slots Calling Stop() will unblock Acquire() if it's waiting on a slot

type ConcurrencyTracker

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

func (*ConcurrencyTracker) Add

func (me *ConcurrencyTracker) Add()

func (*ConcurrencyTracker) Current

func (me *ConcurrencyTracker) Current() int32

func (*ConcurrencyTracker) Elapsed

func (me *ConcurrencyTracker) Elapsed() time.Duration

func (*ConcurrencyTracker) Max

func (me *ConcurrencyTracker) Max() int32

func (*ConcurrencyTracker) Rate

func (me *ConcurrencyTracker) Rate() float64

func (*ConcurrencyTracker) Start

func (me *ConcurrencyTracker) Start()

func (*ConcurrencyTracker) Stop

func (me *ConcurrencyTracker) Stop()

func (*ConcurrencyTracker) Subtract

func (me *ConcurrencyTracker) Subtract()

func (*ConcurrencyTracker) Total

func (me *ConcurrencyTracker) Total() int32

type Limiter

type Limiter interface {
	// Stops the limiter
	Stop()
	// Waits for all executions to complete before returning
	Wait()
	// Once available time or concurrency becomes available
	// execute function fn in a go routine
	// DeadlineExceeded is returned if the timeout elapses before rate and concurrency slots can be acquired
	// fn's implementer can choose whether to adhere to the Context parameter's Doneness
	Execute(ctx context.Context, fn func(context.Context)) error
}

type NoLimitRateLimiter

type NoLimitRateLimiter struct{}

A Null implementation of RateLimiter

func (*NoLimitRateLimiter) Cancel

func (me *NoLimitRateLimiter) Cancel()

func (*NoLimitRateLimiter) Rate

func (me *NoLimitRateLimiter) Rate() float64

func (*NoLimitRateLimiter) Wait

func (me *NoLimitRateLimiter) Wait(ctx context.Context) error

type Option

type Option interface {
	// contains filtered or unexported methods
}

Base interface for all options

type RateLimitOption

type RateLimitOption struct {
	Limiter RateLimiter
}

option for controlling rate limiting

type RateLimiter

type RateLimiter interface {
	// Wait until there are resources available
	// only DeadlineExceeded and LimiterStopped errors can be returned
	Wait(ctx context.Context) error
	// The configured rate
	Rate() float64
	// Cancels Wait()ing
	Cancel()
}

func NewRateLimiter

func NewRateLimiter(rate float64) RateLimiter

Returns a *BasicRateLimiter if rate > 0; otherwise a *NoLimitRateLimiter

type Slot

type Slot interface {
	Release()
}

type TestableRateLimiter

type TestableRateLimiter interface {
	RateLimiter
	XXX_TEST_Wait(tokens int64, ctx context.Context) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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