Files
open-im-server/pkg/common/mw/rpc_server_interceptor.go
T

82 lines
2.4 KiB
Go
Raw Normal View History

2023-03-07 16:57:49 +08:00
package mw
2023-01-11 14:29:37 +08:00
import (
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/log"
2023-03-14 14:31:05 +08:00
"OpenIM/pkg/common/mw/specialerror"
2023-03-07 12:19:30 +08:00
"OpenIM/pkg/errs"
2023-01-11 14:29:37 +08:00
"context"
2023-01-11 18:39:42 +08:00
"fmt"
2023-01-11 14:29:37 +08:00
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/wrapperspb"
2023-03-14 14:31:05 +08:00
"math"
2023-01-11 18:39:42 +08:00
"runtime/debug"
2023-01-11 14:29:37 +08:00
)
2023-03-09 14:41:57 +08:00
const OperationID = "operationID"
const OpUserID = "opUserID"
2023-03-14 15:06:45 +08:00
func rpcString(v interface{}) string {
if s, ok := v.(interface{ String() string }); ok {
return s.String()
}
return fmt.Sprintf("%+v", v)
}
2023-03-07 16:57:49 +08:00
func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
2023-01-11 18:39:42 +08:00
var operationID string
defer func() {
if r := recover(); r != nil {
2023-03-14 15:35:58 +08:00
log.ZError(ctx, "rpc panic", nil, "FullMethod", info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r, string(debug.Stack()))
2023-01-11 18:39:42 +08:00
}
}()
2023-01-12 10:50:35 +08:00
funcName := info.FullMethod
2023-03-14 19:06:43 +08:00
log.ZInfo(ctx, "rpc req", "funcName", funcName, "req", rpcString(req))
2023-01-11 14:29:37 +08:00
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.New(codes.InvalidArgument, "missing metadata").Err()
}
2023-03-09 14:41:57 +08:00
if opts := md.Get(OperationID); len(opts) != 1 || opts[0] == "" {
2023-01-11 14:29:37 +08:00
return nil, status.New(codes.InvalidArgument, "operationID error").Err()
} else {
operationID = opts[0]
}
var opUserID string
2023-03-09 14:41:57 +08:00
if opts := md.Get(OpUserID); len(opts) == 1 {
2023-01-11 14:29:37 +08:00
opUserID = opts[0]
}
2023-03-13 14:10:51 +08:00
ctx = context.WithValue(ctx, OperationID, operationID)
ctx = context.WithValue(ctx, OpUserID, opUserID)
2023-01-11 14:29:37 +08:00
resp, err = handler(ctx, req)
2023-03-14 15:06:45 +08:00
if err == nil {
2023-03-14 18:13:35 +08:00
log.ZInfo(ctx, "server handle rpc success", "funcName", funcName, "resp", rpcString(resp))
2023-03-14 15:06:45 +08:00
return resp, nil
}
unwrap := errs.Unwrap(err)
codeErr := specialerror.ErrCode(unwrap)
if codeErr == nil {
log.ZError(ctx, "rpc InternalServer:", err, "req", req)
2023-03-14 15:06:45 +08:00
codeErr = errs.ErrInternalServer
}
code := codeErr.Code()
if code <= 0 || code > math.MaxUint32 {
log.ZError(ctx, "rpc UnknownError", err, "rpc UnknownCode:", code)
2023-03-14 15:06:45 +08:00
code = errs.ServerInternalError
}
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
2023-03-14 19:06:43 +08:00
if unwrap != err {
stack := fmt.Sprintf("%+v", err)
2023-03-14 15:06:45 +08:00
if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
grpcStatus = details
2023-03-14 14:31:05 +08:00
}
2023-01-11 14:29:37 +08:00
}
2023-03-14 19:06:43 +08:00
log.ZWarn(ctx, "rpc resp", err, "funcName", funcName)
2023-03-14 15:06:45 +08:00
return nil, grpcStatus.Err()
2023-01-11 14:29:37 +08:00
}
2023-03-07 16:57:49 +08:00
func GrpcServer() grpc.ServerOption {
return grpc.UnaryInterceptor(rpcServerInterceptor)
}