pacer

package
v0.0.0-...-1b1593a Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2017 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package pacer makes pacing and retrying API calls easy

Index

Constants

View Source
const (
	// DefaultPacer is a truncated exponential attack and decay.
	//
	// On retries the sleep time is doubled, on non errors then
	// sleeptime decays according to the decay constant as set
	// with SetDecayConstant.
	//
	// The sleep never goes below that set with SetMinSleep or
	// above that set with SetMaxSleep.
	DefaultPacer = Type(iota)

	// AmazonCloudDrivePacer is a specialised pacer for Amazon Drive
	//
	// It implements a truncated exponential backoff strategy with
	// randomization.  Normally operations are paced at the
	// interval set with SetMinSleep.  On errors the sleep timer
	// is set to 0..2**retries seconds.
	//
	// See https://developer.amazon.com/public/apis/experience/cloud-drive/content/restful-api-best-practices
	AmazonCloudDrivePacer

	// GoogleDrivePacer is a specialised pacer for Google Drive
	//
	// It implements a truncated exponential backoff strategy with
	// randomization.  Normally operations are paced at the
	// interval set with SetMinSleep.  On errors the sleep timer
	// is set to (2 ^ n) + random_number_milliseconds seconds
	//
	// See https://developers.google.com/drive/v2/web/handle-errors#exponential-backoff
	GoogleDrivePacer
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Paced

type Paced func() (bool, error)

Paced is a function which is called by the Call and CallNoRetry methods. It should return a boolean, true if it would like to be retried, and an error. This error may be returned or returned wrapped in a RetryError.

type Pacer

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

Pacer state

func New

func New() *Pacer

New returns a Pacer with sensible defaults

func (*Pacer) Call

func (p *Pacer) Call(fn Paced) (err error)

Call paces the remote operations to not exceed the limits and retry on rate limit exceeded

This calls fn, expecting it to return a retry flag and an error. This error may be returned wrapped in a RetryError if the number of retries is exceeded.

func (*Pacer) CallNoRetry

func (p *Pacer) CallNoRetry(fn Paced) error

CallNoRetry paces the remote operations to not exceed the limits and return a retry error on rate limit exceeded

This calls fn and wraps the output in a RetryError if it would like it to be retried

func (*Pacer) GetSleep

func (p *Pacer) GetSleep() time.Duration

GetSleep gets the current sleep time

func (*Pacer) SetAttackConstant

func (p *Pacer) SetAttackConstant(attack uint) *Pacer

SetAttackConstant sets the attack constant for the pacer

This is the speed the time grows from the minimum after errors have occurred.

bigger for slower attack, 1 is double, 0 is go straight to maximum

func (*Pacer) SetDecayConstant

func (p *Pacer) SetDecayConstant(decay uint) *Pacer

SetDecayConstant sets the decay constant for the pacer

This is the speed the time falls back to the minimum after errors have occurred.

bigger for slower decay, exponential. 1 is halve, 0 is go straight to minimum

func (*Pacer) SetMaxConnections

func (p *Pacer) SetMaxConnections(n int) *Pacer

SetMaxConnections sets the maximum number of concurrent connections. Setting the value to 0 will allow unlimited number of connections. Should not be changed once you have started calling the pacer. By default this will be set to fs.Config.Checkers.

func (*Pacer) SetMaxSleep

func (p *Pacer) SetMaxSleep(t time.Duration) *Pacer

SetMaxSleep sets the maximum sleep time for the pacer

func (*Pacer) SetMinSleep

func (p *Pacer) SetMinSleep(t time.Duration) *Pacer

SetMinSleep sets the minimum sleep time for the pacer

func (*Pacer) SetPacer

func (p *Pacer) SetPacer(t Type) *Pacer

SetPacer sets the pacing algorithm

It will choose the default algorithm if an incorrect value is passed in.

func (*Pacer) SetRetries

func (p *Pacer) SetRetries(retries int) *Pacer

SetRetries sets the max number of tries for Call

func (*Pacer) SetSleep

func (p *Pacer) SetSleep(t time.Duration) *Pacer

SetSleep sets the current sleep time

type TokenDispenser

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

TokenDispenser is for controlling concurrency

func NewTokenDispenser

func NewTokenDispenser(n int) *TokenDispenser

NewTokenDispenser makes a pool of n tokens

func (*TokenDispenser) Get

func (td *TokenDispenser) Get()

Get gets a token from the pool - don't forget to return it with Put

func (*TokenDispenser) Put

func (td *TokenDispenser) Put()

Put returns a token

type Type

type Type int

Type is for selecting different pacing algorithms

Jump to

Keyboard shortcuts

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