breaker

package
v0.0.0-...-012d1c6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2021 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrReject  = errors.New("reject")
	ErrTooMany = errors.New("too many request")
)

Functions

func Exec

func Exec(key string, run, fallback func() error) error

func Reload

func Reload(cfg *Config)

Types

type Breaker

type Breaker interface {
	Allow() error
	Mark(err error)
	MarkSuccess()
	MarkFailure()
	String() string
}

Breaker

Breaker仅仅是一个状态机 使用Allow判断能否通过,使用MarkSuccess,MarkFailure反馈成功或失败结果 Mark仅仅是MarkSuccess和MarkFailure的封装,方便外部操作

func Get

func Get(key string) Breaker

type Config

type Config struct {
	Disabled         bool          // 是否禁用
	RequestThreshold int           // 超过此值才会触发熔断校验,区间段为Window总和
	Ratio            float64       // 短路百分比,默认50%
	Sleep            time.Duration // 休眠时间,即open状态经过sleep时间后,进入HalfOpen状态
	Window           time.Duration // 窗口时间
	Bucket           int           // 窗口桶个数
	// contains filtered or unexported fields
}

type Group

type Group interface {
	Get(key string) Breaker
	Reload(cfg *Config)
	Exec(key string, run, fallback func() error) error
}

Breaker分组,共享一份配置

func NewGroup

func NewGroup(cfg *Config) Group

type State

type State int

服务的健康状况 = 请求失败数 / 请求总数. 熔断器开关由关闭到打开的状态转换是通过当前服务健康状况和设定阈值比较决定的.

当熔断器开关关闭时, 请求被允许通过熔断器. 如果当前健康状况高于设定阈值, 开关继续保持关闭. 如果当前健康状况低于设定阈值, 开关则切换为打开状态.
当熔断器开关打开时, 请求被禁止通过.
当熔断器开关处于打开状态, 经过一段时间后, 熔断器会自动进入半开状态, 这时熔断器只允许一个请求通过. 当该请求调用成功时, 熔断器恢复到关闭状态. 若该请求失败, 熔断器继续保持打开状态, 接下来的请求被禁止通过.
const (
	StateClosed State = iota
	StateOpen
	StateHalfOpen
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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