29 lines
698 B
Go
29 lines
698 B
Go
|
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}
|
||
|
}
|
||
|
}
|