Skip to content

Commit

Permalink
feat: trigger breaker on underlying service timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
kevwan committed Apr 30, 2024
1 parent 04116f6 commit 7580fed
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
31 changes: 25 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,26 @@ 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
}

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)
}
9 changes: 9 additions & 0 deletions zrpc/internal/serverinterceptors/breakerinterceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ 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",
Expand Down

0 comments on commit 7580fed

Please sign in to comment.