commit e51179ccf85ada5670a53ac6d7312ebb599b67f5 Author: Asim Date: Tue Mar 15 18:38:16 2016 +0000 Rename to wrapper diff --git a/gobreaker.go b/gobreaker.go new file mode 100644 index 0000000..67fa2c3 --- /dev/null +++ b/gobreaker.go @@ -0,0 +1,28 @@ +package gobreaker + +import ( + "github.com/micro/go-micro/client" + "github.com/sony/gobreaker" + + "golang.org/x/net/context" +) + +type clientWrapper struct { + cb *gobreaker.CircuitBreaker + client.Client +} + +func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error { + _, err := c.cb.Execute(func() (interface{}, error) { + cerr := c.Client.Call(ctx, req, rsp, opts...) + return nil, cerr + }) + return err +} + +// NewClientWrapper takes a *gobreaker.CircuitBreaker and returns a client Wrapper. +func NewClientWrapper(cb *gobreaker.CircuitBreaker) client.Wrapper { + return func(c client.Client) client.Client { + return &clientWrapper{cb, c} + } +} diff --git a/gobreaker_test.go b/gobreaker_test.go new file mode 100644 index 0000000..ea81fef --- /dev/null +++ b/gobreaker_test.go @@ -0,0 +1,47 @@ +package gobreaker + +import ( + "testing" + + "github.com/micro/go-micro/client" + "github.com/micro/go-micro/registry/mock" + "github.com/micro/go-micro/selector" + "github.com/sony/gobreaker" + + "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( + gobreaker.NewCircuitBreaker(gobreaker.Settings{}), + )), + ) + + 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 < 6; 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() != "circuit breaker is open" { + t.Error("Expecting tripped breaker, got %v", err) + } +}