2017-03-31 19:01:58 +03:00
|
|
|
package circuitbreaker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/sony/gobreaker"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Gobreaker returns an endpoint.Middleware that implements the circuit
|
|
|
|
// breaker pattern using the sony/gobreaker package. Only errors returned by
|
|
|
|
// the wrapped endpoint count against the circuit breaker's error count.
|
|
|
|
//
|
|
|
|
// See http://godoc.org/github.com/sony/gobreaker for more information.
|
|
|
|
func Gobreaker(cb *gobreaker.CircuitBreaker) endpoint.Middleware {
|
|
|
|
return func(next endpoint.Endpoint) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
return cb.Execute(func() (interface{}, error) { return next(ctx, request) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|