micro-wrapper-breaker-sony/gobreaker.go

29 lines
681 B
Go
Raw Normal View History

2016-03-15 21:38:16 +03:00
package gobreaker
import (
"github.com/micro/go-micro/client"
"github.com/sony/gobreaker"
2018-03-03 15:28:44 +03:00
"context"
2016-03-15 21:38:16 +03:00
)
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}
}
}