bandwidth

package
v0.0.0-...-06c1c62 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2023 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const Inf = math.MaxInt64

Inf infinite rate

View Source
const InfDuration = time.Duration(math.MaxInt64)

InfDuration is the duration returned by Delay when a Reservation is not OK.

Variables

This section is empty.

Functions

func NewBandwidthLimitedConn

func NewBandwidthLimitedConn(ctx context.Context, readLimiter BandwidthLimiter, writeLimiter BandwidthLimiter, conn net.Conn) net.Conn

NewBandwidthLimitedConn returns a net.Conn that has its Read method rate limited by the limiter.

func NewListener

func NewListener(ctx context.Context, listenerConfig *ListenerConfig, listener net.Listener) net.Listener

NewListener returns a net.Listener that will apply rate limits to each connection and also globally for all connections via the listenerConfig.ReadServerRate and listenerConfig.WriteServerRate configs.

Types

type BandwidthConfig

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

BandwidthConfig holds the limiter configuration limit and burst values.

func NewBandwidthConfig

func NewBandwidthConfig(limit int64, burst int64) *BandwidthConfig

NewBandwidthConfig contains the over limit in bytes per second and the burst; maximum bytes that can be read in a single call. The BandwidthConfig instance that can be read and updated from multiple go routines.

func (*BandwidthConfig) GetBurst

func (conf *BandwidthConfig) GetBurst() int64

Burst returns the burst in bytes per second.

func (*BandwidthConfig) GetLimit

func (conf *BandwidthConfig) GetLimit() int64

Limit returns the limit in bytes per second.

func (*BandwidthConfig) SetBurst

func (conf *BandwidthConfig) SetBurst(burst int64)

SetBurst sets the number of bytes that can be consumed in a single Read call

func (*BandwidthConfig) SetLimit

func (conf *BandwidthConfig) SetLimit(limit int64)

SetLimit sets the overall bytes per second rate

type BandwidthLimiter

type BandwidthLimiter interface {
	// Wait blocks till n bytes per second are available.
	// This can be for the server or per connection
	WaitN(tx context.Context, n int64) error
	Configure(conf *BandwidthConfig)

	// Child create's a child limiter, that will call check the parent's limit before
	// checking its own limit
	Child(conf *BandwidthConfig) BandwidthLimiter
}

Limiter abstracts the idea of a rate limiter in this package. A Limiter can also create a hierarchy of parent child limiters.

func NewBandwidthLimiter

func NewBandwidthLimiter(conf *BandwidthConfig) BandwidthLimiter

NewBandwidthLimiter creates a limiter to use with tcp connection and tcp listener bytes per second rate limiting.

type Limit

type Limit float64

Limit defines the maximum frequency of some events. Limit is represented as number of events per second. A zero Limit allows no events.

type Limiter

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

A Limiter controls how frequently events are allowed to happen. It implements a "token bucket" of size b, initially full and refilled at rate r tokens per second. Informally, in any large enough time interval, the Limiter limits the rate to r tokens per second, with a maximum burst size of b events. As a special case, if r == Inf (the infinite rate), b is ignored. See https://en.wikipedia.org/wiki/Token_bucket for more about token buckets.

The zero value is a valid Limiter, but it will reject all events. Use NewLimiter to create non-zero Limiters.

Limiter has three main methods, Allow, Reserve, and Wait. Most callers should use Wait.

Each of the three methods consumes a single token. They differ in their behavior when no token is available. If no token is available, Allow returns false. If no token is available, Reserve returns a reservation for a future token and the amount of time the caller must wait before using it. If no token is available, Wait blocks until one can be obtained or its associated context.Context is canceled.

The methods AllowN, ReserveN, and WaitN consume n tokens.

func NewLimiter

func NewLimiter(r Limit, b int64) *Limiter

NewLimiter returns a new Limiter that allows events up to rate r and permits bursts of at most b tokens.

func (*Limiter) Burst

func (lim *Limiter) Burst() int64

Burst returns the maximum burst size. Burst is the maximum number of tokens that can be consumed in a single call to Allow, Reserve, or Wait, so higher Burst values allow more events to happen at once. A zero Burst allows no events, unless limit == Inf.

func (*Limiter) Limit

func (lim *Limiter) Limit() Limit

Limit returns the maximum overall event rate.

func (*Limiter) SetBurst

func (lim *Limiter) SetBurst(newBurst int64)

SetBurst is shorthand for SetBurstAt(time.Now(), newBurst).

func (*Limiter) SetLimit

func (lim *Limiter) SetLimit(newLimit Limit)

SetLimit is shorthand for SetLimitAt(time.Now(), newLimit).

func (*Limiter) SetLimitAt

func (lim *Limiter) SetLimitAt(t time.Time, newLimit Limit)

SetLimitAt sets a new Limit for the limiter. The new Limit, and Burst, may be violated or underutilized by those which reserved (using Reserve or Wait) but did not yet act before SetLimitAt was called.

func (*Limiter) Tokens

func (lim *Limiter) Tokens() float64

Tokens returns the number of tokens available now.

func (*Limiter) TokensAt

func (lim *Limiter) TokensAt(t time.Time) float64

TokensAt returns the number of tokens available at time t.

func (*Limiter) WaitN

func (lim *Limiter) WaitN(ctx context.Context, n int64) (err error)

WaitN blocks until lim permits n events to happen. It returns an error if n exceeds the Limiter's burst size, the Context is canceled, or the expected wait time exceeds the Context's Deadline. The burst limit is ignored if the rate limit is Inf.

type ListenerConfig

type ListenerConfig struct {

	// ReadServerRate the global server read limit and burst config
	ReadServerRate *BandwidthConfig

	// WriteServerRate the global server write limit and burst config
	WriteServerRate *BandwidthConfig

	// ReadConnRate the per connection read limit and burst config
	ReadConnRate *BandwidthConfig

	// WriteConnRate the per connection write limit and burst config
	WriteConnRate *BandwidthConfig
}

ListenerConfig groups together the configuration for a Listener and the limiters that should be used.

func NewListenerConfig

func NewListenerConfig(BandwidthConfig *BandwidthConfig) *ListenerConfig

NewListenerConfig is a helper function to create a ListenerConfig from a single BandwidthConfig. the ReadServerRate, WriterServerRate, ReadConnRate, and WriteConnRate are all set to the BandwidthConfig.

func NeweSimpleListenerConfig

func NeweSimpleListenerConfig(read, write int64) *ListenerConfig

type Reservation

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

A Reservation holds information about events that are permitted by a Limiter to happen after a delay. A Reservation may be canceled, which may enable the Limiter to permit additional events.

func (*Reservation) Cancel

func (r *Reservation) Cancel()

Cancel is shorthand for CancelAt(time.Now()).

func (*Reservation) CancelAt

func (r *Reservation) CancelAt(t time.Time)

CancelAt indicates that the reservation holder will not perform the reserved action and reverses the effects of this Reservation on the rate limit as much as possible, considering that other reservations may have already been made.

func (*Reservation) Delay

func (r *Reservation) Delay() time.Duration

Delay is shorthand for DelayFrom(time.Now()).

func (*Reservation) DelayFrom

func (r *Reservation) DelayFrom(t time.Time) time.Duration

DelayFrom returns the duration for which the reservation holder must wait before taking the reserved action. Zero duration means act immediately. InfDuration means the limiter cannot grant the tokens requested in this Reservation within the maximum wait time.

func (*Reservation) OK

func (r *Reservation) OK() bool

OK returns whether the limiter can provide the requested number of tokens within the maximum wait time. If OK is false, Delay returns InfDuration, and Cancel does nothing.

Jump to

Keyboard shortcuts

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