breaker

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrServiceUnavailable = errors.New("circuit breaker is open")

Functions

func MaxInt

func MaxInt(a, b int) int

func MinInt

func MinInt(a, b int) int

func Now

func Now() time.Duration

func Repr

func Repr(v any) string

func Since

func Since(d time.Duration) time.Duration

func Time

func Time() time.Time

Types

type Acceptable

type Acceptable func(err error) bool

type Breaker

type Breaker interface {
	Name() string

	Allow() (Promise, error)

	Do(req func() error) error

	DoWithAcceptable(req func() error, acceptable Acceptable) error

	DoWithFallback(req func() error, fallback func(err error) error) error

	DoWithFallbackAcceptable(req func() error, fallback func(err error) error, acceptable Acceptable) error
}
Example
b := NewBreaker()
fmt.Printf("%#v\n", len(b.Name()) > 0)
_, err := b.Allow()
fmt.Printf("%#v\n", err)
Output:

true
<nil>

func NewBreaker

func NewBreaker(opts ...Option) Breaker

type Bucket

type Bucket struct {
	Sum   float64
	Count int64
}

Bucket defines the bucket that holds sum and num of additions.

type Option

type Option func(breaker *circuitBreaker)

func WithName

func WithName(name string) Option

type Proba

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

A Proba is used to test if true on given probability.

func NewProba

func NewProba() *Proba

NewProba returns a Proba.

func (*Proba) TrueOnProba

func (p *Proba) TrueOnProba(proba float64) (truth bool)

TrueOnProba checks if true on given probability.

type Promise

type Promise interface {
	Accept()
	Reject(reason string)
}

type RollingWindow

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

RollingWindow defines a rolling window to calculate the events in buckets with time interval.

func NewRollingWindow

func NewRollingWindow(size int, interval time.Duration, opts ...RollingWindowOption) *RollingWindow

NewRollingWindow returns a RollingWindow that with size buckets and time interval, use opts to customize the RollingWindow.

func (*RollingWindow) Add

func (rw *RollingWindow) Add(v float64)

Add adds value to current bucket.

func (*RollingWindow) Reduce

func (rw *RollingWindow) Reduce(fn func(b *Bucket))

Reduce runs fn on all buckets, ignore current bucket if ignoreCurrent was set.

type RollingWindowOption

type RollingWindowOption func(rollingWindow *RollingWindow)

RollingWindowOption let callers customize the RollingWindow.

func IgnoreCurrentBucket

func IgnoreCurrentBucket() RollingWindowOption

IgnoreCurrentBucket lets the Reduce call ignore current bucket.

Jump to

Keyboard shortcuts

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