← ClaudeAtlas

resilience-golisted

Go 韧性模式专家 - 熔断器(sony/gobreaker)、重试策略(avast/retry-go, 指数退避)、限流(令牌桶/滑动窗口/按Key)、超时控制(Context分层)、舱壁隔离(信号量/Worker Pool)、降级处理(缓存降级/默认值)、组合弹性调用。适用:微服务容错、外部服务调用、高可用系统、分布式系统韧性设计。不适用:单体应用内部函数调用、无外部依赖的纯计算逻辑、已有成熟框架(如 Istio)处理韧性的场景。触发词:熔断, circuit breaker, 重试, retry, 限流, rate limit, 超时, timeout, 降级, fallback, 舱壁, bulkhead, 韧性, resilience
omeyang/Thoth · ★ 0 · AI & Automation · score 73
Install: claude install-skill omeyang/Thoth
# Go 韧性模式专家 使用 Go 实现微服务韧性模式:$ARGUMENTS --- ## 1. 熔断器(xbreaker - sony/gobreaker) ### 创建熔断器 ```go func NewBreaker(name string, opts ...BreakerOption) *Breaker // 选项 WithTripPolicy(p TripPolicy) // 触发策略 WithSuccessPolicy(p SuccessPolicy) // 成功判断策略 WithTimeout(d time.Duration) // 恢复超时(默认 60s) WithInterval(d time.Duration) // 统计窗口 WithMaxRequests(n uint32) // 半开状态允许请求数(默认 1) WithOnStateChange(f func(...)) // 状态变更回调 ``` ### 执行与泛型调用 ```go // 无返回值 err := breaker.Do(ctx, func() error { return callService() }) // 泛型(有返回值) result, err := xbreaker.Execute[*User](ctx, breaker, func() (*User, error) { return userClient.GetUser(ctx, id) }) ``` ### TripPolicy(触发策略) ```go NewConsecutiveFailures(threshold uint32) // N 次连续失败触发 NewFailureRatio(ratio float64, minReqs uint32) // 失败率超阈值触发 NewFailureCount(threshold uint32) // 总失败次数触发 NewCompositePolicy(policies ...TripPolicy) // OR 组合 NewSlowCallRatio(ratio float64, minReqs uint32) // 慢调用比率触发 ``` ### Context 处理 ```go // 入口检查 ctx.Err(),快速失败 // Context 取消/超时 → 立即返回,不计入熔断统计 ``` > 自定义策略和 HTTP 客户端集成见 [references/examples.md](references/examples.md#熔断器高级用法) --- ## 2. 重试(xretry - avast/retry-go) ### 创建重试器 ```go func NewRetryer(opts ...RetryerOption) *Retryer // 默认: FixedRetry(3) + ExponentialBackoff // 选项 WithRetryPolicy(p RetryPolicy) // 重试决策策略 WithBackoffPolicy(p BackoffPolicy) // 退避计算策略 WithOnRetry(f func(attempt int, err error)) //