breaker

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// StateOpen when circuit breaker open, request not allowed, after sleep
	// some duration, allow one single request for testing the health, if ok
	// then state reset to closed, if not continue the step.
	StateOpen int32 = iota
	// StateClosed when circuit breaker closed, request allowed, the breaker
	// calc the succeed ratio, if request num greater request setting and
	// ratio lower than the setting ratio, then reset state to open.
	StateClosed
	// StateHalfopen when circuit breaker open, after slepp some duration, allow
	// one request, but not state closed.
	StateHalfopen
)

Variables

This section is empty.

Functions

func Go

func Go(name string, run, fallback func() error) error

Go runs your function while tracking the breaker state of default group.

func Init

func Init(conf *Config)

Init init global breaker config, also can reload config after first time call.

Types

type Breaker

type Breaker interface {
	Allow() error
	MarkSuccess()
	MarkFailed()
}

Breaker is a CircuitBreaker pattern. FIXME on int32 atomic.LoadInt32(&b.on) == _switchOn

type Config

type Config struct {
	// breaker switch
	// set true to close breaker
	SwitchOff bool

	// Google
	K float64

	// 统计时间窗口
	// 默认值 5s
	Window time.Duration
	Bucket int
	// 熔断触发临界请求量
	// 统计窗口内请求量低于Request值则不触发熔断
	// 默认值 20
	Request int64
}

Config broker config.

type Group

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

Group represents a class of CircuitBreaker and forms a namespace in which units of CircuitBreaker.

func NewGroup

func NewGroup(conf *Config) *Group

NewGroup new a breaker group container, if conf nil use default conf.

func (*Group) Get

func (g *Group) Get(key string) Breaker

Get get a breaker by a specified key, if breaker not exists then make a new one.

func (*Group) Go

func (g *Group) Go(name string, run, fallback func() error) error

Go runs your function while tracking the breaker state of group.

func (*Group) Reload

func (g *Group) Reload(conf *Config)

Reload reload the group by specified config, this may let all inner breaker reset to a new one.

Jump to

Keyboard shortcuts

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