ratecontrol

package
v0.0.0-...-e2c53ed Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 4 Imported by: 9

README

Package cloudeng.io/net/ratecontrol

import cloudeng.io/net/ratecontrol

Package ratecontrol provides mechanisms for controlling the rate at which requests are made and for backing off when the remote service is unwilling to process requests.

Package ratecontrol provides mechanisms for controlling the rate at which requests are made and for implementing backoff mechanisms.

Types

Type Backoff
type Backoff interface {
	// Wait implements a backoff algorithm. It returns true if the backoff
	// should be terminated, i.e. no more requests should be attempted.
	// The error returned is nil when the backoff algorithm has reached
	// its limit and will generally only be non-nil for an internal error
	// such as the context being cancelled.
	Wait(context.Context, *http.Response) (bool, error)

	// Retries returns the number of retries that the backoff aglorithm
	// has recorded, ie. the number of times that Backoff was called and
	// returned false.
	Retries() int
}

Backoff represents the interface to a backoff algorithm.

Functions
func NewExpontentialBackoff(initial time.Duration, steps int) Backoff

NewExpontentialBackoff returns a instance of Backoff that implements an exponential backoff algorithm starting with the specified initial delay and continuing for the specified number of steps.

Type Controller
type Controller struct {
	// contains filtered or unexported fields
}

Controller is used to control the rate at which requests are made and to implement backoff when the remote server is unwilling to process a request. Controller is safe to use concurrently.

Functions
func New(opts ...Option) *Controller

New returns a new Controller configuring using the specified options.

Methods
func (c *Controller) Backoff() Backoff
func (c *Controller) BytesTransferred(nBytes int)

BytesTransferred notifies the controller that the specified number of bytes have been transferred and is used when byte based rate control is configured via WithBytesPerTick.

func (c *Controller) Wait(ctx context.Context) error

Wait returns when a request can be made. Rate limiting of requests takes priority over rate limiting of bytes. That is, bytes are only considered when a new request can be made.

Type ExponentialBackoff
type ExponentialBackoff struct {
	// contains filtered or unexported fields
}
Methods
func (eb *ExponentialBackoff) Retries() int

Retries implements Backoff.

func (eb *ExponentialBackoff) Wait(ctx context.Context, _ *http.Response) (bool, error)

Wait implements Backoff.

Type Option
type Option func(c *options)

Option represents an option for configuring a ratecontrol Controller.

Functions
func WithBytesPerTick(tickInterval time.Duration, bpt int) Option

The algorithm used is very simple and will simply stop sending data wait for a single tick if the limit is reached without taking into account how long the tick is, nor how much excess data was sent over the previous tick (ie. no attempt is made to smooth out the rate and for now it's a simple start/stop model). The bytes to be accounted for are reported to the Controller via its BytesTransferred method.

func WithCustomBackoff(backoff func() Backoff) Option
func WithExponentialBackoff(first time.Duration, steps int) Option

WithExponentialBackoff enables an exponential backoff algorithm. First defines the first backoff delay, which is then doubled for every consecutive retry until the download either succeeds or the specified number of steps (attempted requests) is exceeded.

func WithRequestsPerTick(tickInterval time.Duration, rpt int) Option

WithRequestsPerTick sets the rate for requests in requests per tick.

Documentation

Overview

Package ratecontrol provides mechanisms for controlling the rate at which requests are made and for backing off when the remote service is unwilling to process requests.

Package ratecontrol provides mechanisms for controlling the rate at which requests are made and for implementing backoff mechanisms.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backoff

type Backoff interface {
	// Wait implements a backoff algorithm. It returns true if the backoff
	// should be terminated, i.e. no more requests should be attempted.
	// The error returned is nil when the backoff algorithm has reached
	// its limit and will generally only be non-nil for an internal error
	// such as the context being cancelled.
	Wait(context.Context, *http.Response) (bool, error)

	// Retries returns the number of retries that the backoff aglorithm
	// has recorded, ie. the number of times that Backoff was called and
	// returned false.
	Retries() int
}

Backoff represents the interface to a backoff algorithm.

func NewExpontentialBackoff

func NewExpontentialBackoff(initial time.Duration, steps int) Backoff

NewExpontentialBackoff returns a instance of Backoff that implements an exponential backoff algorithm starting with the specified initial delay and continuing for the specified number of steps.

type Controller

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

Controller is used to control the rate at which requests are made and to implement backoff when the remote server is unwilling to process a request. Controller is safe to use concurrently.

func New

func New(opts ...Option) *Controller

New returns a new Controller configuring using the specified options.

func (*Controller) Backoff

func (c *Controller) Backoff() Backoff

func (*Controller) BytesTransferred

func (c *Controller) BytesTransferred(nBytes int)

BytesTransferred notifies the controller that the specified number of bytes have been transferred and is used when byte based rate control is configured via WithBytesPerTick.

func (*Controller) Wait

func (c *Controller) Wait(ctx context.Context) error

Wait returns when a request can be made. Rate limiting of requests takes priority over rate limiting of bytes. That is, bytes are only considered when a new request can be made.

type ExponentialBackoff

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

func (*ExponentialBackoff) Retries

func (eb *ExponentialBackoff) Retries() int

Retries implements Backoff.

func (*ExponentialBackoff) Wait

func (eb *ExponentialBackoff) Wait(ctx context.Context, _ *http.Response) (bool, error)

Wait implements Backoff.

type Option

type Option func(c *options)

Option represents an option for configuring a ratecontrol Controller.

func WithBytesPerTick

func WithBytesPerTick(tickInterval time.Duration, bpt int) Option

The algorithm used is very simple and will simply stop sending data wait for a single tick if the limit is reached without taking into account how long the tick is, nor how much excess data was sent over the previous tick (ie. no attempt is made to smooth out the rate and for now it's a simple start/stop model). The bytes to be accounted for are reported to the Controller via its BytesTransferred method.

func WithCustomBackoff

func WithCustomBackoff(backoff func() Backoff) Option

func WithExponentialBackoff

func WithExponentialBackoff(first time.Duration, steps int) Option

WithExponentialBackoff enables an exponential backoff algorithm. First defines the first backoff delay, which is then doubled for every consecutive retry until the download either succeeds or the specified number of steps (attempted requests) is exceeded.

func WithRequestsPerTick

func WithRequestsPerTick(tickInterval time.Duration, rpt int) Option

WithRequestsPerTick sets the rate for requests in requests per tick.

Jump to

Keyboard shortcuts

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