limiters

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2020 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package limiters provides a set of wrappers intended to restrict the amount of resources consumed by the server.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed = errors.New("limiters: Rate bucket is closed")
)

Functions

This section is empty.

Types

type BucketSet

type BucketSet struct {
	// New function is used to construct underlying L instances.
	//
	// It is safe to change it only when BucketSet is not used by any
	// goroutine.
	New func() L

	// Time after which bucket is considered stale and can be removed from the
	// set. For safe use with Rate limiter, it should be at least as twice as
	// big as Rate refill interval.
	ReapInterval time.Duration

	MaxBuckets int
	// contains filtered or unexported fields
}

BucketSet combines a group of Ls into a single key-indexed structure. Basically, each unique key gets its own counter. The main use case for BucketSet is to apply per-resource rate limiting.

Amount of buckets is limited to a certain value. When the size of internal map is around or equal to that value, next Take call will attempt to remove any stale buckets from the group. If it is not possible to do so (all buckets are in active use), Take will return false. Alternatively, in some rare cases, some other (undefined) waiting Take can return false.

A BucksetSet without a New function assigned is no-op: Take and TakeContext always succeed and Release does nothing.

func NewBucketSet

func NewBucketSet(new_ func() L, reapInterval time.Duration, maxBuckets int) *BucketSet

func (*BucketSet) Close

func (r *BucketSet) Close()

func (*BucketSet) Release

func (r *BucketSet) Release(key string)

func (*BucketSet) Take

func (r *BucketSet) Take(key string) bool

func (*BucketSet) TakeContext

func (r *BucketSet) TakeContext(ctx context.Context, key string) error

type L

type L interface {
	Take() bool
	TakeContext(context.Context) error
	Release()

	// Close frees any resources used internally by Limiter for book-keeping.
	Close()
}

The L interface represents a blocking limiter that has some upper bound of resource use and blocks when it is exceeded until enough resources are freed.

type MultiLimit

type MultiLimit struct {
	Wrapped []L
}

MultiLimit wraps multiple L implementations into a single one, locking them in the specified order.

It does not implement any deadlock detection or avoidance algorithms.

func (*MultiLimit) Close

func (ml *MultiLimit) Close()

func (*MultiLimit) Release

func (ml *MultiLimit) Release()

func (*MultiLimit) Take

func (ml *MultiLimit) Take() bool

func (*MultiLimit) TakeContext

func (ml *MultiLimit) TakeContext(ctx context.Context) error

type Rate

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

Rate structure implements a basic rate-limiter for requests using the token bucket approach.

Take() is expected to be called before each request. Excessive calls will block. Timeouts can be implemented using the TakeContext method.

Rate.Close causes all waiting Take to return false. TakeContext returns ErrClosed in this case.

If burstSize = 0, all methods are no-op and always succeed.

func NewRate

func NewRate(burstSize int, interval time.Duration) Rate

func (Rate) Close

func (r Rate) Close()

func (Rate) Release

func (r Rate) Release()

func (Rate) Take

func (r Rate) Take() bool

func (Rate) TakeContext

func (r Rate) TakeContext(ctx context.Context) error

type Semaphore

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

Semaphore is a convenience wrapper for a channel that implements semaphore-kind synchronization.

If the argument given to the NewSemaphore is negative or zero, all methods are no-op.

func NewSemaphore

func NewSemaphore(max int) Semaphore

func (Semaphore) Close

func (s Semaphore) Close()

func (Semaphore) Release

func (s Semaphore) Release()

func (Semaphore) Take

func (s Semaphore) Take() bool

func (Semaphore) TakeContext

func (s Semaphore) TakeContext(ctx context.Context) error

Jump to

Keyboard shortcuts

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