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

93 lines
2.9 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"
"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-01-11 18:39:42 +08:00
"runtime/debug"
2023-01-11 14:29:37 +08:00
)
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-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-03 17:42:26 +08:00
if opts := md.Get(constant.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-07 16:57:49 +08:00
if opts := md.Get(constant.OpUserID); len(opts) == 1 {
2023-01-11 14:29:37 +08:00
opUserID = opts[0]
}
2023-01-30 11:10:26 +08:00
ctx = tracelog.NewRpcCtx(ctx, funcName, operationID)
2023-01-12 15:55:44 +08:00
defer log.ShowLog(ctx)
2023-01-30 11:10:26 +08:00
tracelog.SetCtxInfo(ctx, funcName, err, "opUserID", opUserID, "rpcReq", rpcString(req))
2023-01-11 14:29:37 +08:00
resp, err = handler(ctx, req)
if err != nil {
2023-01-30 11:10:26 +08:00
tracelog.SetCtxInfo(ctx, funcName, err)
2023-03-07 16:57:49 +08:00
return nil, rpcErrorToCode(err).Err()
2023-01-11 14:29:37 +08:00
}
2023-01-30 11:10:26 +08:00
tracelog.SetCtxInfo(ctx, funcName, nil, "rpcResp", rpcString(resp))
2023-01-11 14:29:37 +08:00
return
}
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-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)
}
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-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-07 16:57:49 +08:00
return errs.NewCodeError(errs.DefaultOtherError, err.Error()).Wrap()
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
}