circuitbreaker

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2020 License: Apache-2.0 Imports: 10 Imported by: 46

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 ClearStateChangeListeners added in v0.6.0

func ClearStateChangeListeners()

ClearStateChangeListeners will clear the all StateChangeListener Note: this function is not thread-safe.

func IsValid added in v0.6.0

func IsValid(r *Rule) error

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 RegisterStateChangeListeners

func RegisterStateChangeListeners(listeners ...StateChangeListener)

Note: this function is not thread-safe.

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 is called only when a passed invocation finished.
	OnRequestComplete(rtt uint64, err error)
}

CircuitBreaker is the basic interface of circuit breaker

type CircuitBreakerGenFunc

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

type MetricStatSlot

type MetricStatSlot struct {
}

MetricStatSlot records metrics for circuit breaker on invocation completed. MetricStatSlot must be filled into slot chain if circuit breaker is alive.

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 struct {
	// unique id
	Id string `json:"id,omitempty"`
	// resource name
	Resource string   `json:"resource"`
	Strategy Strategy `json:"strategy"`
	// RetryTimeoutMs represents recovery timeout (in milliseconds) 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 `json:"retryTimeoutMs"`
	// MinRequestAmount represents the minimum number of requests (in an active statistic time span)
	// that can trigger circuit breaking.
	MinRequestAmount uint64 `json:"minRequestAmount"`
	// StatIntervalMs represents statistic time interval of the internal circuit breaker (in ms).
	StatIntervalMs uint32 `json:"statIntervalMs"`
	// MaxAllowedRtMs indicates that any invocation whose response time exceeds this value (in ms)
	// will be recorded as a slow request.
	// MaxAllowedRtMs only takes effect for SlowRequestRatio strategy
	MaxAllowedRtMs uint64 `json:"maxAllowedRtMs"`
	// Threshold represents the threshold of circuit breaker.
	// for SlowRequestRatio, it represents the max slow request ratio
	// for ErrorRatio, it represents the max error request ratio
	// for ErrorCount, it represents the max error request count
	Threshold float64 `json:"threshold"`
}

Rule encompasses the fields of circuit breaking rule.

func GetResRules

func GetResRules(resource string) []*Rule

func (*Rule) ResourceName added in v0.6.0

func (r *Rule) ResourceName() string

func (*Rule) String added in v0.6.0

func (r *Rule) String() string

type Slot added in v0.5.0

type Slot struct {
}

func (*Slot) Check added in v0.5.0

func (b *Slot) Check(ctx *base.EntryContext) *base.TokenResult

type State

type State int32
 Circuit Breaker State Machine:

                                switch to open based on rule
				+-----------------------------------------------------------------------+
				|                                                                       |
				|                                                                       v
		+----------------+                   +----------------+      Probe      +----------------+
		|                |                   |                |<----------------|                |
		|                |   Probe succeed   |                |                 |                |
		|     Closed     |<------------------|    HalfOpen    |                 |      Open      |
		|                |                   |                |   Probe failed  |                |
		|                |                   |                +---------------->|                |
		+----------------+                   +----------------+                 +----------------+
const (
	Closed State = iota
	HalfOpen
	Open
)

func (*State) String

func (s *State) String() string

type StateChangeListener

type StateChangeListener interface {
	// OnTransformToClosed is triggered when circuit breaker state transformed to Closed.
	// Argument rule is copy from circuit breaker's rule, any changes of rule don't take effect for circuit breaker
	// Copying rule has a performance penalty and avoids invalid listeners as much as possible
	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.
	// Argument rule is copy from circuit breaker's rule, any changes of rule don't take effect for circuit breaker
	// Copying rule has a performance penalty and avoids invalid listeners as much as possible
	OnTransformToOpen(prev State, rule Rule, snapshot interface{})

	// OnTransformToHalfOpen is triggered when circuit breaker state transformed to HalfOpen.
	// Argument rule is copy from circuit breaker's rule, any changes of rule don't take effect for circuit breaker
	// Copying rule has a performance penalty and avoids invalid listeners as much as possible
	OnTransformToHalfOpen(prev State, rule Rule)
}

StateChangeListener listens on the circuit breaker state change event

type Strategy

type Strategy int8

Strategy represents the strategy of circuit breaker. Each strategy is associated with one rule type.

const (
	// SlowRequestRatio strategy changes the circuit breaker state based on slow request ratio
	SlowRequestRatio Strategy = iota
	// ErrorRatio strategy changes the circuit breaker state based on error request ratio
	ErrorRatio
	// ErrorCount strategy changes the circuit breaker state based on error amount
	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