Rename to wrapper

This commit is contained in:
Asim 2016-03-15 18:38:16 +00:00 committed by Vasiliy Tolstov
commit e51179ccf8
2 changed files with 75 additions and 0 deletions

28
gobreaker.go Normal file
View File

@ -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}
}
}

47
gobreaker_test.go Normal file
View File

@ -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)
}
}