2020-07-26 17:09:05 +08:00
|
|
|
package clientinterceptors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"path"
|
|
|
|
|
2022-01-04 15:51:32 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/breaker"
|
|
|
|
"github.com/zeromicro/go-zero/zrpc/internal/codes"
|
2020-07-26 17:09:05 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2021-03-01 23:52:44 +08:00
|
|
|
// BreakerInterceptor is an interceptor that acts as a circuit breaker.
|
2020-07-26 17:09:05 +08:00
|
|
|
func BreakerInterceptor(ctx context.Context, method string, req, reply interface{},
|
|
|
|
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
|
|
breakerName := path.Join(cc.Target(), method)
|
|
|
|
return breaker.DoWithAcceptable(breakerName, func() error {
|
|
|
|
return invoker(ctx, method, req, reply, cc, opts...)
|
2020-08-06 20:55:38 +08:00
|
|
|
}, codes.Acceptable)
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|