Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: trigger breaker on underlying service timeout #4112

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions zrpc/internal/serverinterceptors/breakerinterceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"

"github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/errorx"
"github.com/zeromicro/go-zero/zrpc/internal/codes"
"google.golang.org/grpc"
gcodes "google.golang.org/grpc/codes"
Expand All @@ -13,11 +14,13 @@ import (

// StreamBreakerInterceptor is an interceptor that acts as a circuit breaker.
func StreamBreakerInterceptor(svr any, stream grpc.ServerStream, info *grpc.StreamServerInfo,
handler grpc.StreamHandler) (err error) {
handler grpc.StreamHandler) error {
breakerName := info.FullMethod
return breaker.DoWithAcceptable(breakerName, func() error {
err := breaker.DoWithAcceptable(breakerName, func() error {
return handler(svr, stream)
}, codes.Acceptable)
}, serverSideAcceptable)

return convertError(err)
}

// UnaryBreakerInterceptor is an interceptor that acts as a circuit breaker.
Expand All @@ -28,10 +31,28 @@ func UnaryBreakerInterceptor(ctx context.Context, req any, info *grpc.UnaryServe
var err error
resp, err = handler(ctx, req)
return err
}, codes.Acceptable)
}, serverSideAcceptable)

return resp, convertError(err)
}

func convertError(err error) error {
if err == nil {
return nil
}

// we don't convert context.DeadlineExceeded to status error,
// because grpc will convert it and return to the client.
if errors.Is(err, breaker.ErrServiceUnavailable) {
err = status.Error(gcodes.Unavailable, err.Error())
return status.Error(gcodes.Unavailable, err.Error())
}

return resp, err
return err
}

func serverSideAcceptable(err error) bool {
if errorx.In(err, context.DeadlineExceeded, breaker.ErrServiceUnavailable) {
return false
}
return codes.Acceptable(err)
}
42 changes: 42 additions & 0 deletions zrpc/internal/serverinterceptors/breakerinterceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,53 @@ func TestUnaryBreakerInterceptor(t *testing.T) {
assert.NotNil(t, err)
}

func TestUnaryBreakerInterceptorOK(t *testing.T) {
_, err := UnaryBreakerInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
FullMethod: "any",
}, func(_ context.Context, _ any) (any, error) {
return nil, nil
})
assert.NoError(t, err)
}

func TestUnaryBreakerInterceptor_Unavailable(t *testing.T) {
_, err := UnaryBreakerInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
FullMethod: "any",
}, func(_ context.Context, _ any) (any, error) {
return nil, breaker.ErrServiceUnavailable
})
assert.NotNil(t, err)
code := status.Code(err)
assert.Equal(t, codes.Unavailable, code)
}

func TestUnaryBreakerInterceptor_Deadline(t *testing.T) {
for i := 0; i < 1000; i++ {
_, err := UnaryBreakerInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
FullMethod: "any",
}, func(_ context.Context, _ any) (any, error) {
return nil, context.DeadlineExceeded
})
switch status.Code(err) {
case codes.Unavailable:
default:
assert.Equal(t, context.DeadlineExceeded, err)
}
}

var dropped bool
for i := 0; i < 100; i++ {
_, err := UnaryBreakerInterceptor(context.Background(), nil, &grpc.UnaryServerInfo{
FullMethod: "any",
}, func(_ context.Context, _ any) (any, error) {
return nil, context.DeadlineExceeded
})
switch status.Code(err) {
case codes.Unavailable:
dropped = true
default:
assert.Equal(t, context.DeadlineExceeded, err)
}
}
assert.True(t, dropped)
}