Documentation
¶
Index ¶
- Constants
- func NewBandwidthLimitedConn(ctx context.Context, readLimiter BandwidthLimiter, ...) net.Conn
- func NewListener(ctx context.Context, listenerConfig *ListenerConfig, listener net.Listener) net.Listener
- type BandwidthConfig
- type BandwidthLimiter
- type Limit
- type Limiter
- func (lim *Limiter) Burst() int64
- func (lim *Limiter) Limit() Limit
- func (lim *Limiter) SetBurst(newBurst int64)
- func (lim *Limiter) SetLimit(newLimit Limit)
- func (lim *Limiter) SetLimitAt(t time.Time, newLimit Limit)
- func (lim *Limiter) Tokens() float64
- func (lim *Limiter) TokensAt(t time.Time) float64
- func (lim *Limiter) WaitN(ctx context.Context, n int64) (err error)
- type ListenerConfig
- type Reservation
Constants ¶
const Inf = math.MaxInt64
Inf infinite rate
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 ¶
NewLimiter returns a new Limiter that allows events up to rate r and permits bursts of at most b tokens.
func (*Limiter) Burst ¶
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) SetLimitAt ¶
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.
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.