circuit

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package circuit implements the Circuit Breaker pattern. https://docs.microsoft.com/en-us/azure/architecture/patterns/circuit-breaker

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultShouldTripFunc is a default ShouldTripFunc.
	DefaultShouldTripFunc = func(counts Counts) bool {

		return counts.ConsecutiveFailures >= 3
	}
	// DefaultShouldResetFunc is a default ShouldResetFunc.
	DefaultShouldResetFunc = func(counts Counts) bool {

		return counts.ConsecutiveSuccesses >= 3
	}
	// DefaultBackoffDurationFunc is an exponential backoff function
	DefaultBackoffDurationFunc = ExponentialBackoffDuration(time.Duration(100)*time.Second, time.Duration(500)*time.Millisecond)
)

Functions

func ExponentialBackoffDuration

func ExponentialBackoffDuration(maxBackoff, baseTimeout time.Duration) func(Counts) time.Duration

ExponentialBackoffDuration returns a function that uses exponential backoff and full jitter

Types

type BackoffDurationFunc

type BackoffDurationFunc func(Counts) time.Duration

BackoffDurationFunc is a function that takes in a Counts and returns the backoff duration

type BackoffHook

type BackoffHook func(duration time.Duration, reset time.Time)

BackoffHook is a function that represents backoff.

type Breaker

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

Breaker is a state machine to prevent sending requests that are likely to fail.

func NewBreaker

func NewBreaker(opts *Options) *Breaker

NewBreaker returns a new Breaker configured with the given Settings.

func (*Breaker) Call

func (b *Breaker) Call(f func() (interface{}, error)) (interface{}, error)

Call runs the given function if the Breaker allows the call. Call returns an error instantly if the Breaker rejects the request. Otherwise, Call returns the result of the request.

type Counts

type Counts struct {
	CurrentRequests      int
	ConsecutiveSuccesses int
	ConsecutiveFailures  int
}

Counts holds the numbers of requests and their successes/failures.

type ErrOpenState

type ErrOpenState struct{}

ErrOpenState is returned when the b state is open

func (*ErrOpenState) Error

func (e *ErrOpenState) Error() string

type Options

type Options struct {
	HalfOpenConcurrentRequests int

	ShouldTripFunc      ShouldTripFunc
	ShouldResetFunc     ShouldResetFunc
	BackoffDurationFunc BackoffDurationFunc

	// hooks
	OnStateChange StateChangeHook
	OnBackoff     BackoffHook

	// used in tests
	TestClock clock.Clock
}

Options configures Breaker:

HalfOpenConcurrentRequests specifies how many concurrent requests to allow while the circuit is in the half-open state

ShouldTripFunc specifies when the circuit should trip from the closed state to the open state. It takes a Counts struct and returns a bool.

ShouldResetFunc specifies when the circuit should be reset from the half-open state to the closed state and allow all requests. It takes a Counts struct and returns a bool.

BackoffDurationFunc specifies how long to set the backoff duration. It takes a counts struct and returns a time.Duration

OnStateChange is called whenever the state of the Breaker changes.

OnBackoff is called whenever a backoff is set with the backoff duration and reset time

TestClock is used to mock the clock during tests

type ShouldResetFunc

type ShouldResetFunc func(Counts) bool

ShouldResetFunc is a function that takes in a Counts and returns true if the circuit breaker should be reset.

type ShouldTripFunc

type ShouldTripFunc func(Counts) bool

ShouldTripFunc is a function that takes in a Counts and returns true if the circuit breaker should be tripped.

type State

type State int

State is a type that represents a state of Breaker.

const (
	StateClosed State = iota
	StateHalfOpen
	StateOpen
)

These constants are states of Breaker.

func (State) String

func (s State) String() string

String implements stringer interface.

type StateChangeHook

type StateChangeHook func(prev, to State)

StateChangeHook is a function that represents a state change.

Jump to

Keyboard shortcuts

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