Documentation
¶
Overview ¶
Package circuit 提供熔断器(参考 gobreaker/业界通用设计):
closed(正常) → open(熔断) → half-open(试探) → closed(恢复)
用于保护对第三方/下游服务的调用:失败率达到阈值时快速失败, 避免故障扩散(雪崩);冷却期后放行试探请求,成功即恢复。
与 thirdparty 客户端集成:
client := thirdparty.NewClient(thirdparty.Config{
BaseURL: "https://sms.partner.com",
Signer: thirdparty.NewHMACSigner("key", "secret"),
Breaker: circuit.New(circuit.DefaultConfig()), // 熔断保护
})
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrOpen = errors.New("circuit breaker is open")
ErrOpen 熔断器打开时快速失败返回的错误。
Functions ¶
This section is empty.
Types ¶
type Breaker ¶
type Breaker struct {
// contains filtered or unexported fields
}
Breaker 熔断器(并发安全)。
type Config ¶
type Config struct {
// FailureThreshold 失败率阈值(0-1):窗口内失败请求占比达到该值触发熔断。
// 默认 0.5。
FailureThreshold float64
// MinRequests 触发评估的最小请求数:窗口内请求数不足时不触发熔断。
// 默认 10。
MinRequests int
// Window 统计窗口时长:每窗口评估一次失败率。
// 默认 10 秒。
Window time.Duration
// Cooldown 熔断打开后保持时长,之后进入半开试探。
// 默认 10 秒。
Cooldown time.Duration
// HalfOpenMaxRequests 半开状态放行的试探请求数。
// 默认 1。
HalfOpenMaxRequests int
}
Config 熔断器配置。
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig 返回推荐配置(失败率 50%、最小 10 请求、10s 窗口、10s 冷却)。
Click to show internal directories.
Click to hide internal directories.