gateway/rpc 框架问题记录
小于 1 分钟
服务端 panic, 客户端会收到详细的错误信息暴露了服务端
解决方案:
- 去掉 grpc 内置的 recover interceptor, 改为自定义的 recover interceptor
代码如下:
package middleware
import (
"context"
"runtime/debug"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// StreamRecoverInterceptor catches panics in processing stream requests and recovers.
func StreamRecoverInterceptor(svr any, stream grpc.ServerStream, _ *grpc.StreamServerInfo,
handler grpc.StreamHandler) (err error) {
defer handleCrash(func(r any) {
err = toPanicError(r)
})
return handler(svr, stream)
}
// UnaryRecoverInterceptor catches panics in processing unary requests and recovers.
func UnaryRecoverInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp any, err error) {
defer handleCrash(func(r any) {
err = toPanicError(r)
})
return handler(ctx, req)
}
func handleCrash(handler func(any)) {
if r := recover(); r != nil {
handler(r)
}
}
func toPanicError(r any) error {
logx.Errorf("%+v\n\n%s", r, debug.Stack())
return status.Errorf(codes.Internal, "Service temporarily unavailable")
}