errbag

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2015 License: BSD-3-Clause Imports: 2 Imported by: 1

Documentation

Overview

Package errbag implements an error rate based throttler. It can be used to to limit function calls rate once a certain error rate threshold has been reached.

Index

Constants

View Source
const (
	// StatusThrottling indicates the errbag is throttling.
	StatusThrottling = iota

	// StatusOK indicates that all is well.
	StatusOK
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackFunc

type CallbackFunc func(status Status)

CallbackFunc is used as an argument to the Record() method.

type ErrBag

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

ErrBag is very effective at preventing an error rate to reach a certain threshold.

func New

func New(waitTime, errBagSize, leakInterval uint) (*ErrBag, error)

New creates a new ErrBag, for safety purpose. waitTime corresponds to the number of seconds to wait when the error rate threshold is reached. errBagSize is, in seconds, the size of the sliding window to consider for throttling. You can see it as the size of the errbag. The larger it is, the larger the window to consider for error rate is. Consider this value along with the leakInterval. leakInterval corresponds to the time to wait, in milliseconds, before an error is discarded from the errbag. It must be equal or greater than 100, otherwise throttling will be ineffective.

func (ErrBag) Deflate

func (eb ErrBag) Deflate()

Deflate needs to be called when the errbag is of no use anymore. Calling Record() with a deflated errbag will induce a panic.

func (ErrBag) Inflate

func (eb ErrBag) Inflate()

Inflate needs to be called once to prepare the ErrBag. Once the ErrBag is not needed anymore, a proper call to Deflate() shall be made.

func (ErrBag) Record

func (eb ErrBag) Record(err error, callback CallbackFunc)

Record records an error if its value is non nil. It shall be called by any function returning an error in order to properly rate limit the errors produced. RecordError will wait for waitTime seconds if the error rate is too high. callback purpose is for the caller to be informed about the errbag status after an error has been recorded in order to help take the appropriate actions. nil can be passed if the caller is not interested in the status. Note that record will panic if called after Deflate() has been called.

type Status

type Status struct {
	// State indicates whether throttling had to be activated after an error
	// has been recorded (StatusThrottling) or if it was simply registered and
	// all is well (StatusOK).
	State int

	// WaitTime indicates for how long the Record() method will wait before
	// being available to record new errors.
	WaitTime uint
}

Status structure is used as argument to CallbackFunc. It indicates the the sate of the errbag after having recorded an error.

Jump to

Keyboard shortcuts

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