errbag: github.com/Rolinh/errbag Index | Files | Directories

package errbag

import "github.com/Rolinh/errbag"

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

Index

Package Files

errbag.go

Constants

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

    // StatusOK indicates that all is well.
    StatusOK
)

type CallbackFunc Uses

type CallbackFunc func(status Status)

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

type ErrBag Uses

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

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

func New Uses

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 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 Uses

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 Uses

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 Uses

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 Uses

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.

Directories

PathSynopsis
examples

Package errbag imports 2 packages (graph) and is imported by 2 packages. Updated 2018-11-26. Refresh now. Tools for package owners.