limio

package
v0.0.0-...-ec16f60 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2021 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultWindow = 10 * time.Millisecond

DefaultWindow is the window used to smooth SimpleLimit rates. That is, SimpleLimit distributes the given quantity evenly into buckets of size t. This is useful for avoiding tcp silly window syndrome and providing predictable resource usage.

View Source
var ErrTimeoutExceeded error = errors.New("Timeout Exceeded")

ErrTimeoutExceeded will be returned upon a timeout lapsing without a read occuring

Functions

func Distribute

func Distribute(n int, t, w time.Duration) (int, time.Duration)

Distribute takes a rate (n, t) and window (w), evenly distributes the n/t to n'/t' (n'<=n && t'>=w)

Types

type Limiter

type Limiter interface {
	Limit(chan int) <-chan bool //The channel is useful for knowing that the channel has been unlimited. The boolean represents finality.
	Unlimit()
}

A Limiter is an interface that meters some underlying discretely quantifiable operation with respect to time.

The Limit() function, when implemented, should apply a limit to some underlying operation when called. Supporting concurrency is up to the implementer and as such, should be documented. The semantics of the channel are that of a token bucket. The actual integer sent through the channel represents a quantity of operations that can take place. The implementation should be sure to specify its interpretation of the quantity.

Limit() returns a new boolean channel, used to comunicate that the given `chan int` is no longer being used and may be closed. A false value indicates that the Limiter has not been shut down and may still be acted upon. True indicates that the limiter has been shutdown and any further function calls will have no effect.

Unlimit() removes any formerly imposed limits and allows the underlying operation.

type Reader

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

Reader implements an io-limited reader that conforms to the io.Reader and limio.Limiter interface. Reader can have its limits updated concurrently with any Read() calls.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader takes any io.Reader and returns a limio.Reader.

func (*Reader) Close

func (r *Reader) Close() error

Close allows the goroutines that were managing limits and reads to shut down and free up memory. It should be called by any clients of the limio.Reader, much as http.Response.Body should be closed to free up system resources.

func (*Reader) Limit

func (r *Reader) Limit(lch chan int) <-chan bool

Limit can be used to precisely control the limit at which bytes can be Read, whether burstily or not.

func (*Reader) Read

func (r *Reader) Read(p []byte) (written int, err error)

Read implements io.Reader in a blocking manner according to the limits of the limio.Reader.

func (*Reader) SetTimeout

func (r *Reader) SetTimeout(t time.Duration) error

SetTimeout takes some time.Duration t and configures the underlying Reader to return a limio.TimedOut error if the timeout is exceeded while waiting for a read operation.

func (*Reader) SimpleLimit

func (r *Reader) SimpleLimit(n int, t time.Duration) <-chan bool

SimpleLimit takes an integer and a time.Duration and limits the underlying reader non-burstily (given rate is averaged over a small time).

func (*Reader) Unlimit

func (r *Reader) Unlimit()

Unlimit removes any restrictions on the underlying io.Reader.

Jump to

Keyboard shortcuts

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