breaker

package
v0.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCircuitOpen     = NewBreakerError("circuit open")
	ErrTooManyRequests = NewBreakerError("number of requests exceeded the circuit limit")
)
View Source
var (
	ReasonStateChange = NewResetReason("state changes")
	ReasonClosedReset = NewResetReason("reset counter in Closed state")
)

Functions

This section is empty.

Types

type Breaker

type Breaker func(stats Statistics) bool

type BreakerError

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

Implements the error interface

func NewBreakerError

func NewBreakerError(m string) *BreakerError

func (BreakerError) Error

func (e BreakerError) Error() string

type CircuitBreaker

type CircuitBreaker struct {
	sync.Mutex
	// contains filtered or unexported fields
}

A state machine to prevent sending requests that are likely to fails

func New

func New(opts Options) *CircuitBreaker

Provided an Options, it returns the new Circuit Breaker

func (*CircuitBreaker) Do

func (c *CircuitBreaker) Do(what func() (interface{}, error)) (interface{}, error)

Perform the action if the Circuit Breaker allows it, either in Closed state of in Half-open if the number requests does not exceed the limit. It returns an error of type BreakerError if the Circuit Breaker rejects the task. It treats any panic as regular error and forwards it back to the consumer.

type IsSuccessCallback

type IsSuccessCallback func(err error) bool

type Options

type Options struct {
	// The name of this Circuit Breaker
	Name string `json:"name"`

	// The maximum number of task allowed to be performed during Half-open state
	MaxDiscoveryRequests uint32

	// The timeout of the Open state.
	// After which, the state transitions to Half-open
	OpenTimeout time.Duration

	// The interval for resetting count during Closed state.
	// For example, the transition to Open should only occur after failed attempts reaches 5, in a 10 seconds window.
	// To achieve that, you can set this to 10 seconds and set ShouldBreakCircuit to returns a breaker that breaks the circuit after 5 failures.
	//
	// By default, the interval is 0, meaning that it doesn't reset the count
	ResetCountInterval time.Duration

	// The callback to be called whenever the internal state changes
	OnStateChange StateChangeCallback

	// The callback to be called on counter reset
	OnCountReset ResetCallback

	// This determines if a task is success or not, based on the returned error.
	// By default, all errors returned from the task indicate a failed task.
	IsSuccess IsSuccessCallback

	// This callback is used to determine when a Closed circuit should transitions to Open state.
	// By default, it should breaks the circuit when the number of failed attempts reaches 5.
	// Runs only when the task returns an error
	ShouldBreakCircuit Breaker
}

type ResetCallback

type ResetCallback func(stats Statistics, reason ResetReason)

type ResetReason

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

Contains the reason for a reset in the breaker's internal counter

func NewResetReason

func NewResetReason(r string) ResetReason

func (*ResetReason) String

func (r *ResetReason) String() string

Returns the reason of the reset

type State

type State int8
const (
	StateClosed State = iota
	StateHalfOpen
	StateOpen
)

func (State) String

func (s State) String() string

Implements

stringer

interface

type StateChangeCallback

type StateChangeCallback func(name string, prev State, now State)

type Statistics

type Statistics struct {
	Requests  uint32
	Successes uint32
	Failures  uint32

	ConsecutiveSuccesses uint32
	ConsecutiveFailures  uint32
}

Jump to

Keyboard shortcuts

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