...

Source file src/github.com/go-kit/kit/circuitbreaker/hystrix.go

Documentation: github.com/go-kit/kit/circuitbreaker

     1  package circuitbreaker
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/afex/hystrix-go/hystrix"
     7  
     8  	"github.com/go-kit/kit/endpoint"
     9  )
    10  
    11  // Hystrix returns an endpoint.Middleware that implements the circuit
    12  // breaker pattern using the afex/hystrix-go package.
    13  //
    14  // When using this circuit breaker, please configure your commands separately.
    15  //
    16  // See https://godoc.org/github.com/afex/hystrix-go/hystrix for more
    17  // information.
    18  func Hystrix(commandName string) endpoint.Middleware {
    19  	return func(next endpoint.Endpoint) endpoint.Endpoint {
    20  		return func(ctx context.Context, request interface{}) (response interface{}, err error) {
    21  			var resp interface{}
    22  			if err := hystrix.Do(commandName, func() (err error) {
    23  				resp, err = next(ctx, request)
    24  				return err
    25  			}, nil); err != nil {
    26  				return nil, err
    27  			}
    28  			return resp, nil
    29  		}
    30  	}
    31  }
    32  

View as plain text