circuitbreaker

package
v2.7.3 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package circuitbreaker implements the circuit breaker logic.

Index

Constants

View Source
const (
	CountBased = iota
	TimeBased
)

sliding window types

Variables

View Source
var (

	// ErrRejected is returned by 'Execute' if the function call is
	// rejected by the CircuitBreaker
	ErrRejected = fmt.Errorf("call rejected")
)

Functions

This section is empty.

Types

type CallResult

type CallResult uint8

CallResult is the result (success/failure/slow) of a call

const (
	CallResultUnknown CallResult = iota
	CallResultSuccess
	CallResultSlow
	CallResultFailure
)

call results

type CircuitBreaker

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

CircuitBreaker defines a circuit breaker

func New

func New(policy *Policy) *CircuitBreaker

New creates a circuit breaker based on `policy`,

func (*CircuitBreaker) AcquirePermission

func (cb *CircuitBreaker) AcquirePermission() (bool, uint32)

AcquirePermission acquires a permission from the circuit breaker returns true & stateID if the request is permitted returns false & stateID if the request is rejected

func (*CircuitBreaker) Execute

func (cb *CircuitBreaker) Execute(fn func() (interface{}, error)) (interface{}, error)

Execute executes the given function if the CircuitBreaker accepts it and returns the result of the function, and ErrRejected is returned if the CircuitBreaker rejects the request. If a panic occurs in the function, CircuitBreaker regards it as an error and causes the same panic again.

func (*CircuitBreaker) RecordResult

func (cb *CircuitBreaker) RecordResult(stateID uint32, hasErr bool, d time.Duration)

RecordResult records the result in window

func (*CircuitBreaker) SetState

func (cb *CircuitBreaker) SetState(state State)

SetState sets the state of the circuit breaker to `state`

func (*CircuitBreaker) SetStateListener

func (cb *CircuitBreaker) SetStateListener(listener EventListenerFunc)

SetStateListener sets an event listener for the CircuitBreaker

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() State

State returns the state of the circuit breaker

type CountBasedWindow

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

CountBasedWindow defines the count based window

func NewCountBasedWindow

func NewCountBasedWindow(size uint32) *CountBasedWindow

NewCountBasedWindow creates a new count based window with `size` buckets

func (*CountBasedWindow) FailureRate

func (cbw *CountBasedWindow) FailureRate() uint8

FailureRate returns the failure rate of recorded results

func (*CountBasedWindow) Push

func (cbw *CountBasedWindow) Push(result CallResult)

Push pushes a new result into the window and may evict existing results if needed

func (*CountBasedWindow) Reset

func (cbw *CountBasedWindow) Reset()

Reset resets the count based window to initial state

func (*CountBasedWindow) SlowRate

func (cbw *CountBasedWindow) SlowRate() uint8

SlowRate returns the slow rate of recorded results

func (*CountBasedWindow) Total

func (cbw *CountBasedWindow) Total() uint32

Total returns the total number of results currently recorded

type Event

type Event struct {
	Time     time.Time
	OldState string
	NewState string
	Reason   string
}

Event stores the state change event

type EventListenerFunc

type EventListenerFunc func(event *Event)

EventListenerFunc is a listener function to listen state transit event

type Policy

type Policy struct {
	FailureRateThreshold             uint8
	SlowCallRateThreshold            uint8
	SlidingWindowType                uint8
	SlidingWindowSize                uint32
	PermittedNumberOfCallsInHalfOpen uint32
	MinimumNumberOfCalls             uint32
	SlowCallDurationThreshold        time.Duration
	MaxWaitDurationInHalfOpen        time.Duration
	WaitDurationInOpen               time.Duration
}

Policy defines the policy of a circuit breaker

func NewDefaultPolicy

func NewDefaultPolicy() *Policy

NewDefaultPolicy create and initialize a policy with default configuration

func NewPolicy

func NewPolicy(failureRateThreshold, slowCallRateThreshold, slidingWindowType uint8,
	slidingWindowSize, permittedNumberOfCallsInHalfOpen, minimumNumberOfCalls uint32,
	slowCallDurationThreshold, maxWaitDurationInHalfOpen, waitDurationInOpen time.Duration,
) *Policy

NewPolicy create and initialize a policy

type State

type State uint8

State is circuit breaker state

const (
	StateDisabled State = iota
	StateClosed
	StateHalfOpen
	StateOpen
	StateForceOpen
)

circuit breaker states

type TimeBasedWindow

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

TimeBasedWindow defines the time based window

func NewTimeBasedWindow

func NewTimeBasedWindow(size uint32) *TimeBasedWindow

NewTimeBasedWindow creates a new time based window with `size` buckets

func (*TimeBasedWindow) FailureRate

func (tbw *TimeBasedWindow) FailureRate() uint8

FailureRate returns the failure rate of recorded results

func (*TimeBasedWindow) Push

func (tbw *TimeBasedWindow) Push(result CallResult)

Push pushes a new result into the window and may evict existing results if needed

func (*TimeBasedWindow) Reset

func (tbw *TimeBasedWindow) Reset()

Reset resets the time based window to initial state

func (*TimeBasedWindow) SlowRate

func (tbw *TimeBasedWindow) SlowRate() uint8

SlowRate returns the slow rate of recorded results

func (*TimeBasedWindow) Total

func (tbw *TimeBasedWindow) Total() uint32

Total returns the total number of results currently recorded

type Window

type Window interface {
	Total() uint32
	Reset()
	Push(result CallResult)
	FailureRate() uint8
	SlowRate() uint8
}

Window defines the interface of a window

Jump to

Keyboard shortcuts

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