chore(gRPC): a new unary server interceptor for panic recovery (#703)

fix #691
This commit is contained in:
Bo-Yi Wu
2022-12-17 22:34:04 +08:00
committed by GitHub
parent 673f552d29
commit 6fa9769a31
3 changed files with 43 additions and 2 deletions

View File

@@ -2,7 +2,9 @@ package rpc
import (
"context"
"fmt"
"net"
"runtime/debug"
"strings"
"sync"
@@ -12,6 +14,10 @@ import (
"github.com/appleboy/gorush/notify"
"github.com/appleboy/gorush/rpc/proto"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"go.opencensus.io/plugin/ocgrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
@@ -121,7 +127,22 @@ func RunGRPCServer(ctx context.Context, cfg *config.ConfYaml) error {
return nil
}
s := grpc.NewServer()
recoveryOpt := grpc_recovery.WithRecoveryHandlerContext(
func(ctx context.Context, p interface{}) error {
fmt.Printf("[PANIC] %s\n%s", p, string(debug.Stack()))
return status.Error(codes.Internal, "system has been broken")
},
)
unaryInterceptors := []grpc.UnaryServerInterceptor{
grpc_prometheus.UnaryServerInterceptor,
grpc_recovery.UnaryServerInterceptor(recoveryOpt),
}
s := grpc.NewServer(
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)),
)
rpcSrv := NewServer(cfg)
proto.RegisterGorushServer(s, rpcSrv)
proto.RegisterHealthServer(s, rpcSrv)