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

126 lines
4.0 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/constant"
"OpenIM/pkg/common/log"
2023-03-14 14:31:05 +08:00
"OpenIM/pkg/common/mw/specialerror"
2023-02-23 19:15:30 +08:00
"OpenIM/pkg/common/tracelog"
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-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 {
log.NewError(operationID, info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r, "stack:", string(debug.Stack()))
}
}()
2023-03-13 10:22:12 +08:00
log.Info("", "rpc come here,in rpc call")
2023-01-12 10:50:35 +08:00
funcName := info.FullMethod
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-14 14:31:05 +08:00
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Req", rpcString(req))
2023-03-09 16:36:47 +08:00
ctx = tracelog.SetFuncInfos(ctx, funcName, operationID)
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)
if err != nil {
2023-03-14 14:31:05 +08:00
log.Info(operationID, "rpc error:", err.Error())
unwrap := errs.Unwrap(err)
codeErr := specialerror.ErrCode(unwrap)
if codeErr == nil {
log.Error(operationID, "rpc InternalServer:", err.Error())
codeErr = errs.ErrInternalServer
}
if unwrap != err {
log.Info(operationID, "rpc error stack:", fmt.Sprintf("%+v", err))
}
code := codeErr.Code()
if code <= 0 || code > math.MaxUint32 {
log.Error(operationID, "rpc UnknownCode:", code, "err:", err.Error())
code = errs.UnknownCode
}
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
if errs.Unwrap(err) != err {
stack := fmt.Sprintf("%+v", err)
log.Info(operationID, "rpc stack:", stack)
if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
grpcStatus = details
}
}
return nil, grpcStatus.Err()
2023-01-11 14:29:37 +08:00
}
2023-03-14 14:31:05 +08:00
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Resp", rpcString(resp))
return resp, nil
2023-01-11 14:29:37 +08:00
}
2023-03-07 16:57:49 +08:00
func rpcClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
if ctx == nil {
return errs.ErrInternalServer.Wrap("call rpc request context is nil")
2023-01-12 10:52:41 +08:00
}
2023-03-03 17:42:26 +08:00
operationID, ok := ctx.Value(constant.OperationID).(string)
2023-01-12 16:17:52 +08:00
if !ok {
2023-03-13 13:59:39 +08:00
log.Error("1111", "ctx missing operationID")
2023-03-07 16:57:49 +08:00
return errs.ErrArgs.Wrap("ctx missing operationID")
}
md := metadata.Pairs(constant.OperationID, operationID)
opUserID, ok := ctx.Value(constant.OpUserID).(string)
if ok {
md.Append(constant.OpUserID, opUserID)
}
2023-03-13 10:22:12 +08:00
log.Info("", "rpc come here before")
2023-03-07 16:57:49 +08:00
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
if err == nil {
return nil
2023-01-12 16:17:52 +08:00
}
2023-03-13 10:34:42 +08:00
log.Info("", "rpc come here err", err.Error())
2023-03-10 20:16:27 +08:00
2023-03-07 16:57:49 +08:00
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
2023-01-12 16:17:52 +08:00
if !ok {
2023-03-14 14:31:05 +08:00
return errs.ErrInternalServer.Wrap(err.Error())
2023-01-12 16:17:52 +08:00
}
2023-03-07 16:57:49 +08:00
sta := rpcErr.GRPCStatus()
if sta.Code() == 0 {
return errs.NewCodeError(errs.DefaultOtherError, err.Error()).Wrap()
}
details := sta.Details()
if len(details) == 0 {
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
}
if v, ok := details[0].(*wrapperspb.StringValue); ok {
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap(v.String())
}
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
}
func GrpcServer() grpc.ServerOption {
return grpc.UnaryInterceptor(rpcServerInterceptor)
}
func GrpcClient() grpc.DialOption {
return grpc.WithUnaryInterceptor(rpcClientInterceptor)
2023-01-12 16:17:52 +08:00
}