2016-03-15 21:38:16 +03:00
|
|
|
package gobreaker
|
|
|
|
|
|
|
|
import (
|
2019-03-05 15:24:45 +03:00
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2016-03-15 21:38:16 +03:00
|
|
|
"github.com/micro/go-micro/client"
|
2019-03-05 15:24:45 +03:00
|
|
|
"github.com/micro/go-micro/errors"
|
2016-03-15 21:38:16 +03:00
|
|
|
"github.com/sony/gobreaker"
|
2019-03-05 15:24:45 +03:00
|
|
|
)
|
2016-03-15 21:38:16 +03:00
|
|
|
|
2019-03-05 15:24:45 +03:00
|
|
|
type BreakerMethod int
|
|
|
|
|
|
|
|
const (
|
|
|
|
BreakService BreakerMethod = iota
|
|
|
|
BreakServiceEndpoint
|
2016-03-15 21:38:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type clientWrapper struct {
|
2019-03-05 15:24:45 +03:00
|
|
|
bs gobreaker.Settings
|
|
|
|
bm BreakerMethod
|
|
|
|
cbs map[string]*gobreaker.TwoStepCircuitBreaker
|
|
|
|
mu sync.Mutex
|
2016-03-15 21:38:16 +03:00
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientWrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
2019-03-05 15:24:45 +03:00
|
|
|
var svc string
|
|
|
|
|
|
|
|
switch c.bm {
|
|
|
|
case BreakService:
|
|
|
|
svc = req.Service()
|
|
|
|
case BreakServiceEndpoint:
|
|
|
|
svc = req.Service() + "." + req.Endpoint()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.mu.Lock()
|
|
|
|
cb, ok := c.cbs[svc]
|
|
|
|
if !ok {
|
|
|
|
cb = gobreaker.NewTwoStepCircuitBreaker(c.bs)
|
|
|
|
c.cbs[svc] = cb
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
|
|
|
cbAllow, err := cb.Allow()
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(req.Service(), err.Error(), 502)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.Client.Call(ctx, req, rsp, opts...); err == nil {
|
|
|
|
cbAllow(true)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:26:51 +03:00
|
|
|
merr := errors.Parse(err.Error())
|
|
|
|
switch {
|
|
|
|
case merr.Code == 0:
|
|
|
|
merr.Code = 503
|
|
|
|
case len(merr.Id) == 0:
|
|
|
|
merr.Id = req.Service()
|
2019-03-05 15:24:45 +03:00
|
|
|
}
|
|
|
|
|
2019-03-06 16:26:51 +03:00
|
|
|
if merr.Code >= 500 {
|
2019-03-05 15:24:45 +03:00
|
|
|
cbAllow(false)
|
|
|
|
} else {
|
|
|
|
cbAllow(true)
|
|
|
|
}
|
|
|
|
|
2019-03-06 16:26:51 +03:00
|
|
|
return merr
|
2016-03-15 21:38:16 +03:00
|
|
|
}
|
|
|
|
|
2019-03-05 15:24:45 +03:00
|
|
|
// NewClientWrapper returns a client Wrapper.
|
|
|
|
func NewClientWrapper() client.Wrapper {
|
|
|
|
return func(c client.Client) client.Client {
|
|
|
|
w := &clientWrapper{}
|
|
|
|
w.bs = gobreaker.Settings{}
|
|
|
|
w.cbs = make(map[string]*gobreaker.TwoStepCircuitBreaker)
|
|
|
|
w.Client = c
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCustomClientWrapper takes a gobreaker.Settings and BreakerMethod. Returns a client Wrapper.
|
|
|
|
func NewCustomClientWrapper(bs gobreaker.Settings, bm BreakerMethod) client.Wrapper {
|
2016-03-15 21:38:16 +03:00
|
|
|
return func(c client.Client) client.Client {
|
2019-03-05 15:24:45 +03:00
|
|
|
w := &clientWrapper{}
|
|
|
|
w.bm = bm
|
|
|
|
w.bs = bs
|
|
|
|
w.cbs = make(map[string]*gobreaker.TwoStepCircuitBreaker)
|
|
|
|
w.Client = c
|
|
|
|
return w
|
2016-03-15 21:38:16 +03:00
|
|
|
}
|
|
|
|
}
|