优化发送响应关于时间的处理

This commit is contained in:
白茶清欢 2023-08-10 12:13:49 +08:00
parent ec91bb7a44
commit 5e1aaaf64e
2 changed files with 3 additions and 3 deletions

View File

@ -29,7 +29,7 @@ func InitRequest() gin.HandlerFunc {
requestID := getRequestID(ctx, traceID) requestID := getRequestID(ctx, traceID)
ctx.Set(define.TraceIDField, traceID) ctx.Set(define.TraceIDField, traceID)
ctx.Set(define.RequestIDField, requestID) ctx.Set(define.RequestIDField, requestID)
ctx.Set(define.StartRequestTimeField, time.Now().UnixNano()) ctx.Set(define.StartRequestTimeField, time.Now())
ctx.Next() ctx.Next()
} }
} }

View File

@ -44,14 +44,14 @@ func Send(ctx *gin.Context, code interface{}, httpCode int, data interface{}) {
} }
// 设置数据已发送的标识 // 设置数据已发送的标识
defer ctx.Set(hasSendResponseFlag, true) defer ctx.Set(hasSendResponseFlag, true)
finishRequestTime := time.Now().UnixNano() finishRequestTime := time.Now()
responseData := map[string]interface{}{ responseData := map[string]interface{}{
define.ResponseCodeField: code, define.ResponseCodeField: code,
define.ResponseMessageField: exception.GetMessage(code), define.ResponseMessageField: exception.GetMessage(code),
define.ResponseTraceIDField: ctx.GetString(define.TraceIDField), define.ResponseTraceIDField: ctx.GetString(define.TraceIDField),
define.ResponseRequestIDField: ctx.GetString(define.RequestIDField), define.ResponseRequestIDField: ctx.GetString(define.RequestIDField),
define.ResponseDataField: data, define.ResponseDataField: data,
define.HandleRequestCostField: (finishRequestTime - ctx.GetInt64(define.StartRequestTimeField)) / 1e6, define.HandleRequestCostField: finishRequestTime.UnixMilli() - ctx.GetTime(define.StartRequestTimeField).UnixMilli(),
} }
// 记录响应数据 // 记录响应数据
recordData, _ := json.Marshal(responseData) recordData, _ := json.Marshal(responseData)