commit e9508fdeea0091f542752a552d7d04080c0779b5 Author: Asim Date: Tue Mar 15 18:38:16 2016 +0000 Rename to wrapper diff --git a/hystrix.go b/hystrix.go new file mode 100644 index 0000000..84fd1af --- /dev/null +++ b/hystrix.go @@ -0,0 +1,25 @@ +package hystrix + +import ( + "github.com/afex/hystrix-go/hystrix" + "github.com/micro/go-micro/client" + + "golang.org/x/net/context" +) + +type clientWrapper struct { + client.Client +} + +func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { + return hystrix.Do(req.Service()+"."+req.Method(), func() error { + return c.Client.Call(ctx, req, rsp, opts...) + }, nil) +} + +// NewClientWrapper returns a hystrix client Wrapper. +func NewClientWrapper() client.Wrapper { + return func(c client.Client) client.Client { + return &clientWrapper{c} + } +} diff --git a/hystrix_test.go b/hystrix_test.go new file mode 100644 index 0000000..d4a3549 --- /dev/null +++ b/hystrix_test.go @@ -0,0 +1,44 @@ +package hystrix + +import ( + "testing" + + "github.com/micro/go-micro/client" + "github.com/micro/go-micro/registry/mock" + "github.com/micro/go-micro/selector" + + "golang.org/x/net/context" +) + +func TestBreaker(t *testing.T) { + // setup + r := mock.NewRegistry() + s := selector.NewSelector(selector.Registry(r)) + + c := client.NewClient( + // set the selector + client.Selector(s), + // add the breaker wrapper + client.Wrap(NewClientWrapper()), + ) + + req := c.NewJsonRequest("test.service", "Test.Method", map[string]string{ + "foo": "bar", + }) + + var rsp map[string]interface{} + + // Force to point of trip + for i := 0; i < 25; i++ { + c.Call(context.TODO(), req, rsp) + } + + err := c.Call(context.TODO(), req, rsp) + if err == nil { + t.Error("Expecting tripped breaker, got nil error") + } + + if err.Error() != "hystrix: circuit open" { + t.Error("Expecting tripped breaker, got %v", err) + } +}