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_client_interceptor.go

94 lines
3.0 KiB

2 years ago
package mw
import (
"context"
"errors"
2 years ago
"fmt"
2 years ago
"strings"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/proto/errinfo"
2 years ago
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
func GrpcClient() grpc.DialOption {
2 years ago
return grpc.WithUnaryInterceptor(RpcClientInterceptor)
2 years ago
}
2 years ago
func RpcClientInterceptor(ctx context.Context, method string, req, resp interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error) {
2 years ago
if ctx == nil {
return errs.ErrInternalServer.Wrap("call rpc request context is nil")
}
2 years ago
log.ZInfo(ctx, "rpc client req", "funcName", method, "req", rpcString(req))
2 years ago
ctx, err = getRpcContext(ctx, method)
if err != nil {
return err
}
err = invoker(ctx, method, req, resp, cc, opts...)
if err == nil {
2 years ago
// log.ZInfo(ctx, "rpc client resp", "funcName", method, "resp", rpcString(resp))
2 years ago
return nil
}
2 years ago
// log.ZError(ctx, "rpc resp error", err)
2 years ago
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
if !ok {
return errs.ErrInternalServer.Wrap(err.Error())
}
sta := rpcErr.GRPCStatus()
if sta.Code() == 0 {
return errs.NewCodeError(errs.ServerInternalError, err.Error()).Wrap()
}
if details := sta.Details(); len(details) > 0 {
errInfo, ok := details[0].(*errinfo.ErrorInfo)
if ok {
s := strings.Join(errInfo.Warp, "->") + errInfo.Cause
return errs.NewCodeError(int(sta.Code()), sta.Message()).WithDetail(s).Wrap()
}
}
return errs.NewCodeError(int(sta.Code()), sta.Message()).Wrap()
}
func getRpcContext(ctx context.Context, method string) (context.Context, error) {
2 years ago
md := metadata.Pairs()
if keys, _ := ctx.Value(constant.RpcCustomHeader).([]string); len(keys) > 0 {
2 years ago
for _, key := range keys {
val, ok := ctx.Value(key).([]string)
if !ok {
2 years ago
return nil, errs.ErrInternalServer.Wrap(fmt.Sprintf("ctx missing key %s", key))
2 years ago
}
if len(val) == 0 {
2 years ago
return nil, errs.ErrInternalServer.Wrap(fmt.Sprintf("ctx key %s value is empty", key))
}
2 years ago
md.Set(key, val...)
}
md.Set(constant.RpcCustomHeader, keys...)
2 years ago
}
2 years ago
operationID, ok := ctx.Value(constant.OperationID).(string)
if !ok {
2 years ago
log.ZWarn(ctx, "ctx missing operationID", errors.New("ctx missing operationID"), "funcName", method)
2 years ago
return nil, errs.ErrArgs.Wrap("ctx missing operationID")
2 years ago
}
2 years ago
md.Set(constant.OperationID, operationID)
2 years ago
var checkArgs []string
checkArgs = append(checkArgs, constant.OperationID, operationID)
2 years ago
opUserID, ok := ctx.Value(constant.OpUserID).(string)
if ok {
2 years ago
md.Set(constant.OpUserID, opUserID)
2 years ago
checkArgs = append(checkArgs, constant.OpUserID, opUserID)
2 years ago
}
2 years ago
opUserIDPlatformID, ok := ctx.Value(constant.OpUserPlatform).(string)
if ok {
2 years ago
md.Set(constant.OpUserPlatform, opUserIDPlatformID)
2 years ago
}
2 years ago
connID, ok := ctx.Value(constant.ConnID).(string)
if ok {
md.Set(constant.ConnID, connID)
2 years ago
}
2 years ago
md.Set(constant.CheckKey, genReqKey(checkArgs))
return metadata.NewOutgoingContext(ctx, md), nil
2 years ago
}