resilience-golisted
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)) //