From e51179ccf85ada5670a53ac6d7312ebb599b67f5 Mon Sep 17 00:00:00 2001 From: Asim Date: Tue, 15 Mar 2016 18:38:16 +0000 Subject: [PATCH] Rename to wrapper --- gobreaker.go | 28 ++++++++++++++++++++++++++++ gobreaker_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 gobreaker.go create mode 100644 gobreaker_test.go 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) + } +}