-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 31 additions & 0 deletions
31
zrpc/internal/serverinterceptors/breakerinterceptor_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |