hook

package
v1.5.21 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2024 License: MIT, Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

内部钩子

Index

Constants

View Source
const (
	SlowReqRecord  = "SlowReqRecord"
	ReqRecord      = "ReqRecord"
	ErrorReqRecord = "ErrorReqRecord"
)
View Source
const CircuitHookKey = "CircuitHook"

Variables

View Source
var (
	ErrTooManyRequests = errors.New("too many requests")
	ErrOpenState       = errors.New("circuit breaker is open")
)

Functions

This section is empty.

Types

type CircuitBreaker

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

断路器

断路器的状态改变都是延迟懒惰的

func NewCircuitBreaker

func NewCircuitBreaker(st CircuitSettings) *CircuitBreaker

func (*CircuitBreaker) Name

func (cb *CircuitBreaker) Name() string

func (*CircuitBreaker) State

func (cb *CircuitBreaker) State() State

返回断路器状态

type CircuitHook

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

断路器钩子

func NewCircuitHook

func NewCircuitHook(settings CircuitSettings) *CircuitHook

func (*CircuitHook) AfterRequest

func (ch *CircuitHook) AfterRequest(cErr error, req core.Request, client core.Client)

func (*CircuitHook) BeforeRequest

func (ch *CircuitHook) BeforeRequest(req core.Request, client core.Client) error

func (*CircuitHook) SetHandleCErr

func (ch *CircuitHook) SetHandleCErr(handleFunc func(cErr error, req core.Request) error)

type CircuitSettings

type CircuitSettings struct {
	Name          string
	MaxRequests   uint32
	Interval      time.Duration
	Timeout       time.Duration
	ReadyToTrip   func(counts Counts) bool
	OnStateChange func(name string, from State, to State)
}

Name 名字,请务必保障名字的唯一性

MaxRequests Half-Open状态下允许通过的最大请求数

Interval 重置时间间隔(Closed状态下有效)。如果为零,永远不重置。

Timeout 超时时间(Open状态下有效)。

超时之后,状态将转变为Half-Open状态。
如果为零,默认为60秒

ReadyToTrip 测试是否应该从Closed状态转变为Open状态。

true 表示可以转变,否则不可以。
如果不配置,则采用默认的。默认失败次数达到5次则进入Open状

OnStateChange 状态变化将调用此方法。

func (*CircuitSettings) Clone

func (set *CircuitSettings) Clone() interface{}

type Counts

type Counts struct {
	Requests             uint32
	TotalSuccesses       uint32
	TotalFailures        uint32
	ConsecutiveSuccesses uint32
	ConsecutiveFailures  uint32
}

记录请求的数量以及失败和成功数量

type LogHook

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

func NewLogHook

func NewLogHook(slowReqLong time.Duration, record func(tag, msg string)) *LogHook

func (*LogHook) AfterRequest

func (log *LogHook) AfterRequest(cErr error, req core.Request, client core.Client)

func (*LogHook) BeforeRequest

func (log *LogHook) BeforeRequest(req core.Request, client core.Client) error

type State

type State int

断路器状态

const (
	StateClosed State = iota
	StateHalfOpen
	StateOpen
)

状态常量

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