This commit is contained in:
wangchuxiao
2023-01-30 11:10:26 +08:00
parent d6edb4aa8c
commit 21a1e7dff2
23 changed files with 173 additions and 285 deletions
+8 -8
View File
@@ -16,16 +16,16 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
rpcName = utils2.GetFuncName(1)
}
logFuncName := fmt.Sprintf("[ApiToRpc: %s]%s", utils2.GetFuncName(1), rpcFuncName)
ctx := trace_log.NewCtx1(c, rpcFuncName)
ctx := tracelog.NewCtx1(c, rpcFuncName)
defer log.ShowLog(ctx)
if err := c.BindJSON(apiReq); err != nil {
trace_log.WriteErrorResponse(ctx, "BindJSON", err)
tracelog.WriteErrorResponse(ctx, "BindJSON", err)
return
}
trace_log.SetCtxInfo(ctx, logFuncName, nil, "apiReq", apiReq)
tracelog.SetCtxInfo(ctx, logFuncName, nil, "apiReq", apiReq)
etcdConn, err := getcdv3.GetConn(ctx, rpcName)
if err != nil {
trace_log.WriteErrorResponse(ctx, "GetConn", err)
tracelog.WriteErrorResponse(ctx, "GetConn", err)
return
}
rpcClient := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{
@@ -33,22 +33,22 @@ func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcCl
})[0].MethodByName(rpcFuncName) // rpcClient func
rpcReqPtr := reflect.New(rpcClient.Type().In(1).Elem()) // *req
CopyAny(apiReq, rpcReqPtr.Interface())
trace_log.SetCtxInfo(ctx, logFuncName, nil, "opUserID", c.GetString("opUserID"), "callRpcReq", rpcString(rpcReqPtr.Elem().Interface()))
tracelog.SetCtxInfo(ctx, logFuncName, nil, "opUserID", c.GetString("opUserID"), "callRpcReq", rpcString(rpcReqPtr.Elem().Interface()))
respArr := rpcClient.Call([]reflect.Value{
reflect.ValueOf(context.Context(c)), // context.Context (ctx operationID. opUserID)
rpcReqPtr, // rpcClient apiReq
}) // respArr => (apiResp, error)
if !respArr[1].IsNil() { // rpcClient err != nil
err := respArr[1].Interface().(error)
trace_log.WriteErrorResponse(ctx, rpcFuncName, err, "callRpcResp", "error")
tracelog.WriteErrorResponse(ctx, rpcFuncName, err, "callRpcResp", "error")
return
}
rpcResp := respArr[0].Elem()
trace_log.SetCtxInfo(ctx, rpcFuncName, nil, "callRpcResp", rpcString(rpcResp.Interface()))
tracelog.SetCtxInfo(ctx, rpcFuncName, nil, "callRpcResp", rpcString(rpcResp.Interface()))
if apiResp != nil {
CopyAny(rpcResp.Interface(), apiResp)
}
trace_log.SetSuccess(ctx, rpcFuncName, apiResp)
tracelog.SetSuccess(ctx, rpcFuncName, apiResp)
}
func rpcString(v interface{}) string {