@@ -69,8 +69,8 @@ func (c *client) buildDialOptions(opts ...ClientOption) []grpc.DialOption {
|
|||||||
WithUnaryClientInterceptors(
|
WithUnaryClientInterceptors(
|
||||||
clientinterceptors.TracingInterceptor,
|
clientinterceptors.TracingInterceptor,
|
||||||
clientinterceptors.DurationInterceptor,
|
clientinterceptors.DurationInterceptor,
|
||||||
clientinterceptors.BreakerInterceptor,
|
|
||||||
clientinterceptors.PrometheusInterceptor,
|
clientinterceptors.PrometheusInterceptor,
|
||||||
|
clientinterceptors.BreakerInterceptor,
|
||||||
clientinterceptors.TimeoutInterceptor(cliOpts.Timeout),
|
clientinterceptors.TimeoutInterceptor(cliOpts.Timeout),
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package codes
|
package codes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
)
|
)
|
||||||
@@ -10,6 +12,17 @@ func Acceptable(err error) bool {
|
|||||||
switch status.Code(err) {
|
switch status.Code(err) {
|
||||||
case codes.DeadlineExceeded, codes.Internal, codes.Unavailable, codes.DataLoss:
|
case codes.DeadlineExceeded, codes.Internal, codes.Unavailable, codes.DataLoss:
|
||||||
return false
|
return false
|
||||||
|
case codes.Unknown:
|
||||||
|
return acceptableUnknown(err)
|
||||||
|
default:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func acceptableUnknown(err error) bool {
|
||||||
|
switch err {
|
||||||
|
case context.DeadlineExceeded:
|
||||||
|
return false
|
||||||
default:
|
default:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,10 +58,12 @@ func (s *rpcServer) Start(register RegisterFn) error {
|
|||||||
serverinterceptors.UnaryCrashInterceptor(),
|
serverinterceptors.UnaryCrashInterceptor(),
|
||||||
serverinterceptors.UnaryStatInterceptor(s.metrics),
|
serverinterceptors.UnaryStatInterceptor(s.metrics),
|
||||||
serverinterceptors.UnaryPrometheusInterceptor(),
|
serverinterceptors.UnaryPrometheusInterceptor(),
|
||||||
|
serverinterceptors.UnaryBreakerInterceptor(),
|
||||||
}
|
}
|
||||||
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
|
unaryInterceptors = append(unaryInterceptors, s.unaryInterceptors...)
|
||||||
streamInterceptors := []grpc.StreamServerInterceptor{
|
streamInterceptors := []grpc.StreamServerInterceptor{
|
||||||
serverinterceptors.StreamCrashInterceptor,
|
serverinterceptors.StreamCrashInterceptor,
|
||||||
|
serverinterceptors.StreamBreakerInterceptor,
|
||||||
}
|
}
|
||||||
streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
|
streamInterceptors = append(streamInterceptors, s.streamInterceptors...)
|
||||||
options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),
|
options := append(s.options, WithUnaryServerInterceptors(unaryInterceptors...),
|
||||||
|
|||||||
33
zrpc/internal/serverinterceptors/breakerinterceptor.go
Normal file
33
zrpc/internal/serverinterceptors/breakerinterceptor.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package serverinterceptors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/tal-tech/go-zero/core/breaker"
|
||||||
|
"github.com/tal-tech/go-zero/zrpc/internal/codes"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StreamBreakerInterceptor is an interceptor that acts as a circuit breaker.
|
||||||
|
func StreamBreakerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo,
|
||||||
|
handler grpc.StreamHandler) (err error) {
|
||||||
|
breakerName := info.FullMethod
|
||||||
|
return breaker.DoWithAcceptable(breakerName, func() error {
|
||||||
|
return handler(srv, stream)
|
||||||
|
}, codes.Acceptable)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnaryBreakerInterceptor is an interceptor that acts as a circuit breaker.
|
||||||
|
func UnaryBreakerInterceptor() grpc.UnaryServerInterceptor {
|
||||||
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
||||||
|
handler grpc.UnaryHandler) (resp interface{}, err error) {
|
||||||
|
breakerName := info.FullMethod
|
||||||
|
err = breaker.DoWithAcceptable(breakerName, func() error {
|
||||||
|
var err error
|
||||||
|
resp, err = handler(ctx, req)
|
||||||
|
return err
|
||||||
|
}, codes.Acceptable)
|
||||||
|
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
}
|
||||||
31
zrpc/internal/serverinterceptors/breakerinterceptor_test.go
Normal file
31
zrpc/internal/serverinterceptors/breakerinterceptor_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package serverinterceptors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/codes"
|
||||||
|
"google.golang.org/grpc/status"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStreamBreakerInterceptor(t *testing.T) {
|
||||||
|
err := StreamBreakerInterceptor(nil, nil, &grpc.StreamServerInfo{
|
||||||
|
FullMethod: "any",
|
||||||
|
}, func(
|
||||||
|
srv interface{}, stream grpc.ServerStream) error {
|
||||||
|
return status.New(codes.DeadlineExceeded, "any").Err()
|
||||||
|
})
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnaryBreakerInterceptor(t *testing.T) {
|
||||||
|
interceptor := UnaryBreakerInterceptor()
|
||||||
|
_, err := interceptor(nil, nil, &grpc.UnaryServerInfo{
|
||||||
|
FullMethod: "any",
|
||||||
|
}, func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return nil, status.New(codes.DeadlineExceeded, "any").Err()
|
||||||
|
})
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user