Skip to content

Commit

Permalink
breakerbp: Add an Execute method to FailureRatioBreaker
Browse files Browse the repository at this point in the history
This allows you to use the circuit breaker logic in other implementations than just the Thrift middleware it provides
  • Loading branch information
pacejackson committed Aug 18, 2020
1 parent a3abec8 commit fb7a64d
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion breakerbp/failure_ratio.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,17 @@ func (cb FailureRatioBreaker) runStatsProducer() {
}
}

// Execute wraps the given function call in circuit breaker logic and returns
// the result.
func (cb FailureRatioBreaker) Execute(fn func() (interface{}, error)) (interface{}, error) {
return cb.goBreaker.Execute(fn)
}

// ThriftMiddleware is a thrift.ClientMiddleware that handles circuit breaking.
func (cb FailureRatioBreaker) ThriftMiddleware(next thrift.TClient) thrift.TClient {
return thrift.WrappedTClient{
Wrapped: func(ctx context.Context, method string, args, result thrift.TStruct) error {
_, err := cb.goBreaker.Execute(func() (interface{}, error) {
_, err := cb.Execute(func() (interface{}, error) {
return nil, next.Call(ctx, method, args, result)
})
return err
Expand Down

0 comments on commit fb7a64d

Please sign in to comment.