You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/mw/rpc.go

93 lines
2.9 KiB

2 years ago
package mw
2 years ago
import (
2 years ago
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/tracelog"
2 years ago
"OpenIM/pkg/errs"
2 years ago
"context"
2 years ago
"fmt"
2 years ago
"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"
2 years ago
"runtime/debug"
2 years ago
)
2 years ago
func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
2 years ago
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()))
}
}()
2 years ago
funcName := info.FullMethod
2 years ago
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.New(codes.InvalidArgument, "missing metadata").Err()
}
2 years ago
if opts := md.Get(constant.OperationID); len(opts) != 1 || opts[0] == "" {
2 years ago
return nil, status.New(codes.InvalidArgument, "operationID error").Err()
} else {
operationID = opts[0]
}
var opUserID string
2 years ago
if opts := md.Get(constant.OpUserID); len(opts) == 1 {
2 years ago
opUserID = opts[0]
}
2 years ago
ctx = tracelog.NewRpcCtx(ctx, funcName, operationID)
2 years ago
defer log.ShowLog(ctx)
2 years ago
tracelog.SetCtxInfo(ctx, funcName, err, "opUserID", opUserID, "rpcReq", rpcString(req))
2 years ago
resp, err = handler(ctx, req)
if err != nil {
2 years ago
tracelog.SetCtxInfo(ctx, funcName, err)
2 years ago
return nil, rpcErrorToCode(err).Err()
2 years ago
}
2 years ago
tracelog.SetCtxInfo(ctx, funcName, nil, "rpcResp", rpcString(resp))
2 years ago
return
}
2 years ago
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")
2 years ago
}
2 years ago
operationID, ok := ctx.Value(constant.OperationID).(string)
2 years ago
if !ok {
2 years ago
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)
}
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
if err == nil {
return nil
2 years ago
}
2 years ago
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
2 years ago
if !ok {
2 years ago
return errs.NewCodeError(errs.DefaultOtherError, err.Error()).Wrap()
2 years ago
}
2 years ago
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)
2 years ago
}