circuitbreaker

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CircuitBreaker

func CircuitBreaker(opts ...Option) middleware.Middleware

CircuitBreaker 返回一个断路器中间件

Types

type Breaker

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

Breaker 断路器中间件

func NewBreaker

func NewBreaker(opts ...Option) *Breaker

NewBreaker 创建一个新的断路器

func (*Breaker) AllowRequest

func (cb *Breaker) AllowRequest() bool

AllowRequest 判断是否允许请求通过断路器

func (*Breaker) Execute

func (cb *Breaker) Execute(ctx context.Context, req interface{}, handler middleware.Handler) (interface{}, error)

Execute 执行断路器保护的操作

func (*Breaker) Middleware

func (cb *Breaker) Middleware() middleware.Middleware

Middleware 返回断路器中间件

func (*Breaker) RegisterFailure

func (cb *Breaker) RegisterFailure()

RegisterFailure 注册失败请求

func (*Breaker) RegisterSuccess

func (cb *Breaker) RegisterSuccess()

RegisterSuccess 注册成功请求

func (*Breaker) State

func (cb *Breaker) State() State

State 获取断路器当前状态

type Option

type Option func(*options)

Option 是断路器中间件的选项

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger 设置日志记录器

func WithMaxRequests

func WithMaxRequests(n int) Option

WithMaxRequests 设置半开状态下允许的最大请求数

func WithName

func WithName(name string) Option

WithName 设置断路器名称

func WithOnStateChange

func WithOnStateChange(fn func(name string, from, to State)) Option

WithOnStateChange 设置状态变化回调函数

func WithThreshold

func WithThreshold(threshold int) Option

WithThreshold 设置触发断路器的错误阈值

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout 设置断路器打开状态的超时时间

type State

type State int

State 定义断路器状态

const (
	// StateClosed 表示断路器关闭(允许请求通过)
	StateClosed State = iota
	// StateOpen 表示断路器打开(阻止请求通过)
	StateOpen
	// StateHalfOpen 表示断路器半开(允许有限请求通过以测试服务)
	StateHalfOpen
)

func (State) String

func (s State) String() string

String 返回状态字符串表示

Jump to

Keyboard shortcuts

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