circuitbreaker

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2020 License: Apache-2.0 Imports: 10 Imported by: 49

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearRules added in v0.4.0

func ClearRules() error

ClearRules clear all the previous rules.

func LoadRules

func LoadRules(rules []Rule) (bool, error)

LoadRules replaces old rules with the given circuit breaking rules.

return value:

bool: was designed to indicate whether the internal map has been changed error: was designed to indicate whether occurs the error.

func NewErrorCountRule

func NewErrorCountRule(resource string, intervalMs uint32, retryTimeoutMs uint32, minRequestAmount, maxErrorCount uint64) *errorCountRule

func NewErrorRatioRule

func NewErrorRatioRule(resource string, intervalMs uint32, retryTimeoutMs uint32, minRequestAmount uint64, maxErrorRatio float64) *errorRatioRule

func NewSlowRtRule

func NewSlowRtRule(resource string, intervalMs uint32, retryTimeoutMs uint32, maxAllowedRt, minRequestAmount uint64, maxSlowRequestRatio float64) *slowRtRule

func RegisterStateChangeListeners

func RegisterStateChangeListeners(listeners ...StateChangeListener)

func RemoveCircuitBreakerGenerator

func RemoveCircuitBreakerGenerator(s Strategy) error

func SetCircuitBreakerGenerator

func SetCircuitBreakerGenerator(s Strategy, generator CircuitBreakerGenFunc) error

SetCircuitBreakerGenerator sets the circuit breaker generator for the given strategy. Note that modifying the generator of default strategies is not allowed.

Types

type CircuitBreaker

type CircuitBreaker interface {
	// BoundRule returns the associated circuit breaking rule.
	BoundRule() Rule
	// BoundStat returns the associated statistic data structure.
	BoundStat() interface{}
	// TryPass acquires permission of an invocation only if it is available at the time of invocation.
	TryPass(ctx *base.EntryContext) bool
	// CurrentState returns current state of the circuit breaker.
	CurrentState() State
	// OnRequestComplete record a completed request with the given response time as well as error (if present),
	// and handle state transformation of the circuit breaker.
	OnRequestComplete(rtt uint64, err error)
}

type CircuitBreakerGenFunc

type CircuitBreakerGenFunc func(r Rule, reuseStat interface{}) CircuitBreaker

type CircuitBreakerSlot

type CircuitBreakerSlot struct {
}

func (*CircuitBreakerSlot) Check

type MetricStatSlot

type MetricStatSlot struct {
}

MetricStatSlot records metrics for circuit breaker on invocation completed.

func (*MetricStatSlot) OnCompleted

func (c *MetricStatSlot) OnCompleted(ctx *base.EntryContext)

func (*MetricStatSlot) OnEntryBlocked

func (c *MetricStatSlot) OnEntryBlocked(_ *base.EntryContext, _ *base.BlockError)

func (*MetricStatSlot) OnEntryPassed

func (c *MetricStatSlot) OnEntryPassed(_ *base.EntryContext)

type Rule

type Rule interface {
	base.SentinelRule
	// BreakerStrategy returns the strategy.
	BreakerStrategy() Strategy
	// IsApplicable checks whether the rule is valid and could be converted to a corresponding circuit breaker.
	IsApplicable() error
	// BreakerStatIntervalMs returns the statistic interval of circuit breaker (in milliseconds).
	BreakerStatIntervalMs() uint32
	// IsEqualsTo checks whether current rule is consistent with the given rule.
	IsEqualsTo(r Rule) bool
	// IsStatReusable checks whether current rule is "statistically" equal to the given rule.
	IsStatReusable(r Rule) bool
}

Rule represents the base interface of the circuit breaker rule.

func GetResRules

func GetResRules(resource string) []Rule

type RuleBase

type RuleBase struct {
	// unique id
	Id string
	// resource name
	Resource string
	Strategy Strategy
	// RetryTimeoutMs represents recovery timeout (in seconds) before the circuit breaker opens.
	// During the open period, no requests are permitted until the timeout has elapsed.
	// After that, the circuit breaker will transform to half-open state for trying a few "trial" requests.
	RetryTimeoutMs uint32
	// MinRequestAmount represents the minimum number of requests (in an active statistic time span)
	// that can trigger circuit breaking.
	MinRequestAmount uint64
	// StatIntervalMs represents statistic time interval of the internal circuit breaker (in ms).
	StatIntervalMs uint32
}

RuleBase encompasses common fields of circuit breaking rule.

func (*RuleBase) BreakerStatIntervalMs

func (b *RuleBase) BreakerStatIntervalMs() uint32

func (*RuleBase) BreakerStrategy

func (b *RuleBase) BreakerStrategy() Strategy

func (*RuleBase) IsApplicable

func (b *RuleBase) IsApplicable() error

func (*RuleBase) IsStatReusable

func (b *RuleBase) IsStatReusable(r Rule) bool

func (*RuleBase) ResourceName

func (b *RuleBase) ResourceName() string

func (*RuleBase) String

func (b *RuleBase) String() string

type State

type State int32
const (
	Closed State = iota
	HalfOpen
	Open
)

*

Circuit Breaker State Machine:

                               switch to open based on rule
       +-----------------------------------------------------------------------+
       |                                                                       |
       |                                                                       v

+----------------+ +----------------+ Probe +----------------+ | | | |<----------------| | | | Probe succeed | | | | | Closed |<------------------| HalfOpen | | Open | | | | | Probe failed | | | | | +---------------->| | +----------------+ +----------------+ +----------------+

func (*State) String

func (s *State) String() string

type StateChangeListener

type StateChangeListener interface {
	// OnTransformToClosed is triggered when circuit breaker state transformed to Closed.
	OnTransformToClosed(prev State, rule Rule)

	// OnTransformToOpen is triggered when circuit breaker state transformed to Open.
	// The "snapshot" indicates the triggered value when the transformation occurs.
	OnTransformToOpen(prev State, rule Rule, snapshot interface{})

	// OnTransformToHalfOpen is triggered when circuit breaker state transformed to HalfOpen.
	OnTransformToHalfOpen(prev State, rule Rule)
}

type Strategy

type Strategy int8

The strategy of circuit breaker. Each strategy represents one rule type.

const (
	SlowRequestRatio Strategy = iota
	ErrorRatio
	ErrorCount
)

func (Strategy) String

func (s Strategy) String() string

Jump to

Keyboard shortcuts

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