2017-03-31 19:01:58 +03:00
|
|
|
package circuitbreaker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/afex/hystrix-go/hystrix"
|
2017-05-18 19:54:23 +03:00
|
|
|
"golang.org/x/net/context"
|
2017-03-31 19:01:58 +03:00
|
|
|
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hystrix returns an endpoint.Middleware that implements the circuit
|
|
|
|
// breaker pattern using the afex/hystrix-go package.
|
|
|
|
//
|
|
|
|
// When using this circuit breaker, please configure your commands separately.
|
|
|
|
//
|
|
|
|
// See https://godoc.org/github.com/afex/hystrix-go/hystrix for more
|
|
|
|
// information.
|
|
|
|
func Hystrix(commandName string) endpoint.Middleware {
|
|
|
|
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
|
|
|
|
var resp interface{}
|
|
|
|
if err := hystrix.Do(commandName, func() (err error) {
|
|
|
|
resp, err = next(ctx, request)
|
|
|
|
return err
|
|
|
|
}, nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|