Documentation
¶
Overview ¶
Package resiliencex 提供服务治理组件: 令牌桶限流、熔断器与舱壁隔离, 拒绝语义统一 errx,观测外部注入,零第三方依赖。
Index ¶
- Constants
- func ErrCircuitOpen() error
- func ErrRateLimited() error
- type Bulkhead
- type CircuitBreaker
- func (cb *CircuitBreaker) Allow() error
- func (cb *CircuitBreaker) Counts() Counts
- func (cb *CircuitBreaker) Execute(fn func() error) error
- func (cb *CircuitBreaker) ExecuteContext(ctx context.Context, fn func(context.Context) error) error
- func (cb *CircuitBreaker) Failure()
- func (cb *CircuitBreaker) State() State
- func (cb *CircuitBreaker) Success()
- type Counts
- type KeyedWindow
- type KeyedWindowOption
- type Limiter
- func (l *Limiter) Allow() bool
- func (l *Limiter) AllowN(n int) bool
- func (l *Limiter) Burst() int
- func (l *Limiter) Rate() float64
- func (l *Limiter) RetryAfter() time.Duration
- func (l *Limiter) SetRate(rate float64) error
- func (l *Limiter) Wait(ctx context.Context) error
- func (l *Limiter) WaitN(ctx context.Context, n int) error
- type Metrics
- type Option
- func WithFailureThreshold(ratio float64) Option
- func WithHalfOpenMax(n int) Option
- func WithLogger(l logx.Logger) Option
- func WithMetrics(m Metrics) Option
- func WithMinRequests(n int) Option
- func WithOnStateChange(fn func(from, to State)) Option
- func WithOpenTimeout(d time.Duration) Option
- func WithTraceHook(h TraceHook) Option
- func WithWindow(slot time.Duration, size int) Option
- type State
- type TraceAttr
- type TraceHook
- type Window
Constants ¶
const ( // CodeInvalidConfig 配置非法。 CodeInvalidConfig errx.Code = "RESX_INVALID_CONFIG" // CodeRateLimited 限流拒绝。 CodeRateLimited errx.Code = "RESX_RATE_LIMITED" // CodeCircuitOpen 熔断拒绝。 CodeCircuitOpen errx.Code = "RESX_CIRCUIT_OPEN" // CodeBulkheadFull 舱壁拒绝。 CodeBulkheadFull errx.Code = "RESX_BULKHEAD_FULL" // CodeWaitCanceled 等待限流许可被取消。 CodeWaitCanceled errx.Code = "RESX_WAIT_CANCELED" )
错误码定义:resiliencex 各失败场景的错误码。
const Version = "v1.5.1"
Version 是当前库版本,与 git tag 保持一致。
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bulkhead ¶
type Bulkhead struct {
// contains filtered or unexported fields
}
Bulkhead 是信号量舱壁:限制并发在途请求数。
func NewBulkhead ¶
NewBulkhead 创建舱壁,限制 maxConcurrent 个并发。
func (*Bulkhead) TryAcquire ¶
TryAcquire 非阻塞获取许可;成功返回 release 函数(幂等), 失败返回 ok=false。
type CircuitBreaker ¶
type CircuitBreaker struct {
// contains filtered or unexported fields
}
CircuitBreaker 是三态熔断器:Closed / Open / HalfOpen。
func NewCircuitBreaker ¶
func NewCircuitBreaker(opts ...Option) (*CircuitBreaker, error)
NewCircuitBreaker 创建熔断器。配置非法返回 RESX_INVALID_CONFIG。
func (*CircuitBreaker) Allow ¶
func (cb *CircuitBreaker) Allow() error
Allow 检查请求是否可放行。 Closed 放行;Open 拒绝并在超时后转入 HalfOpen;HalfOpen 限量放行探测。
func (*CircuitBreaker) Execute ¶
func (cb *CircuitBreaker) Execute(fn func() error) error
Execute 执行并自动上报成功/失败。
func (*CircuitBreaker) ExecuteContext ¶
ExecuteContext 以受保护方式执行 fn,自动熔断并记录链路 span。
func (*CircuitBreaker) Failure ¶
func (cb *CircuitBreaker) Failure()
Failure 上报一次失败。 Closed 计入窗口,失败率达标后转入 Open;HalfOpen 任一失败立即 Open。
func (*CircuitBreaker) Success ¶
func (cb *CircuitBreaker) Success()
Success 上报一次成功。 Closed 计入窗口;HalfOpen 累计探测成功,达到阈值后回到 Closed。
type Counts ¶
type Counts struct {
// Requests 窗口内请求总数。
Requests uint64
// Successes 窗口内成功数。
Successes uint64
// Failures 窗口内失败数。
Failures uint64
}
Counts 是熔断器窗口统计快照。
type KeyedWindow ¶ added in v1.5.1
type KeyedWindow struct {
// contains filtered or unexported fields
}
KeyedWindow 是按任意 key 维护固定窗口的限流器: 每个 key 独立计数,带容量上限与空闲 TTL 清理,防止 key 膨胀。
func NewKeyedFixedWindow ¶ added in v1.5.1
func NewKeyedFixedWindow(limit int, window time.Duration, opts ...KeyedWindowOption) (*KeyedWindow, error)
NewKeyedFixedWindow 创建按 key 固定窗口限流器。 每个 key 在 window 内最多 limit 次;空闲超过 TTL 或容量超限时淘汰。
func (*KeyedWindow) Allow ¶ added in v1.5.1
func (k *KeyedWindow) Allow(key string) bool
Allow 尝试通过指定 key 的窗口;超限返回 false。
type KeyedWindowOption ¶ added in v1.5.1
type KeyedWindowOption func(*keyedWindowConfig)
KeyedWindowOption 修改按 key 窗口限流器配置。
func WithKeyedWindowClock ¶ added in v1.5.1
func WithKeyedWindowClock(now func() time.Time) KeyedWindowOption
WithKeyedWindowClock 注入时间源(测试用)。
func WithKeyedWindowLogger ¶ added in v1.5.1
func WithKeyedWindowLogger(l logx.Logger) KeyedWindowOption
WithKeyedWindowLogger 注入结构化日志实现。
func WithKeyedWindowMaxKeys ¶ added in v1.5.1
func WithKeyedWindowMaxKeys(n int) KeyedWindowOption
WithKeyedWindowMaxKeys 设置 key 容量上限;达到上限后按最近使用淘汰。
func WithKeyedWindowMetrics ¶ added in v1.5.1
func WithKeyedWindowMetrics(m Metrics) KeyedWindowOption
WithKeyedWindowMetrics 注入指标钩子。
func WithKeyedWindowTTL ¶ added in v1.5.1
func WithKeyedWindowTTL(d time.Duration) KeyedWindowOption
WithKeyedWindowTTL 设置 key 空闲过期时长;0 表示仅按容量淘汰。
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter 是令牌桶限流器: 以 rate 匀速补充令牌,burst 允许瞬时突发,惰性计算零后台任务。
func NewTokenBucket ¶
NewTokenBucket 创建令牌桶限流器。 rate 为每秒补充令牌数(>0),burst 为桶容量(>=1)。
func (*Limiter) RetryAfter ¶
RetryAfter 返回补满 1 枚令牌所需的等待时间;当前余量足够时返回 0。 用于生成 HTTP 429 响应头 Retry-After 等场景。
type Option ¶
type Option func(configApplier)
Option 修改组件配置,在组件 New 时按顺序应用。 组件专属选项对不匹配的组件无效(文档说明)。
func WithFailureThreshold ¶
WithFailureThreshold 设置失败率阈值(0,1],默认 0.5。
func WithOnStateChange ¶
WithOnStateChange 设置状态切换回调(锁外调用)。
func WithOpenTimeout ¶
WithOpenTimeout 设置熔断打开后的探测等待时长,默认 10s。
type Window ¶
type Window struct {
// contains filtered or unexported fields
}
Window 是固定或滑动窗口限流器。
func NewFixedWindow ¶
NewFixedWindow 创建固定窗口限流:每 window 内最多 limit 次。
func NewSlidingWindow ¶
NewSlidingWindow 创建滑动窗口限流:任意连续 window 内最多 limit 次。