优化 GinContext 定义
This commit is contained in:
@ -10,9 +10,9 @@ package request
|
||||
import (
|
||||
"fmt"
|
||||
networkUtil "git.zhangdeman.cn/zhangdeman/network/util"
|
||||
"git.zhangdeman.cn/zhangdeman/trace"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"github.com/gin-gonic/gin"
|
||||
"git.zhangdeman.cn/zhangdeman/trace"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
@ -63,13 +63,16 @@ func getRequestID(ctx *gin.Context, traceID string) string {
|
||||
func NewContext(ginContext *gin.Context) *Context {
|
||||
// 生成traceID
|
||||
traceID := getRequestID(ginContext, "")
|
||||
hostname, _ := os.Hostname()
|
||||
return &Context{
|
||||
Context: ginContext,
|
||||
StartRequestTime: time.Now(),
|
||||
FinishRequestTime: time.Time(int64(0)),
|
||||
HandlerAfterResponse: make([]gin.HandlerFunc, 0),
|
||||
CustomData: make(map[string]any),
|
||||
lock: &sync.RWMutex{},
|
||||
customData: make(map[string]any),
|
||||
Hostname: hostname,
|
||||
HostIp: networkUtil.IP.GetHostIP(),
|
||||
customLock: &sync.RWMutex{},
|
||||
TraceID: traceID,
|
||||
Trace: trace.NewRuntime(traceID, 0),
|
||||
}
|
||||
@ -82,14 +85,16 @@ func NewContext(ginContext *gin.Context) *Context {
|
||||
// Date : 11:45 2025/2/28
|
||||
type Context struct {
|
||||
*gin.Context `json:"-"`
|
||||
StartRequestTime time.Time // 开始请求时间(全局)
|
||||
FinishRequestTime time.Time // 结束请求时间(全局)
|
||||
TraceID string `json:"trace_id"` // 请求trace_id
|
||||
ParentAppName string `json:"parent_app_name"` // 父级应用名称
|
||||
HandlerAfterResponse []gin.HandlerFunc `json:"-"` // 响应数据之后需要执行的逻辑
|
||||
CustomData map[string]any // 业务上下文自定义数据, 可以合 ginCtx.Value 作区分
|
||||
lock *sync.RWMutex
|
||||
Trace *trace.Runtime `json:"-"` // 追踪实例
|
||||
StartRequestTime time.Time `json:"start_request_time"` // 开始请求时间(全局)
|
||||
FinishRequestTime time.Time `json:"finish_request_time"` // 结束请求时间(全局)
|
||||
TraceID string `json:"trace_id"` // 请求trace_id
|
||||
ParentAppName string `json:"parent_app_name"` // 父级应用名称
|
||||
HandlerAfterResponse []gin.HandlerFunc `json:"-"` // 响应数据之后需要执行的逻辑
|
||||
Hostname string `json:"hostname"` // 服务器名称
|
||||
HostIp string `json:"host_ip"` // 服务器IP
|
||||
customData map[string]any `json:"-"` // 业务上下文自定义数据, 可以合 ginCtx.Value 作区分
|
||||
customLock *sync.RWMutex `json:"-"` // 数据锁
|
||||
Trace *trace.Runtime `json:"-"` // 追踪实例
|
||||
}
|
||||
|
||||
// SetCustom 设置数据
|
||||
@ -98,79 +103,80 @@ type Context struct {
|
||||
//
|
||||
// Date : 11:58 2025/2/28
|
||||
func (ctx *Context) SetCustom(key string, val any) {
|
||||
ctx.lock.Lock()
|
||||
defer ctx.lock.Unlock()
|
||||
if nil == ctx.CustomData {
|
||||
ctx.CustomData = make(map[string]any)
|
||||
ctx.customLock.Lock()
|
||||
defer ctx.customLock.Unlock()
|
||||
if nil == ctx.customData {
|
||||
ctx.customData = make(map[string]any)
|
||||
}
|
||||
ctx.CustomData[key] = val
|
||||
ctx.Set(key, val)
|
||||
ctx.customData[key] = val
|
||||
}
|
||||
|
||||
// CustomDataValue 获取customData
|
||||
// customDataValue 获取customData
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:03 2025/2/28
|
||||
func (ctx *Context) CustomDataValue(key string) any {
|
||||
ctx.lock.RLock()
|
||||
defer ctx.lock.RUnlock()
|
||||
if val, exist := ctx.CustomData[key]; exist {
|
||||
func (ctx *Context) customDataValue(key string) any {
|
||||
ctx.customLock.RLock()
|
||||
defer ctx.customLock.RUnlock()
|
||||
if val, exist := ctx.customData[key]; exist {
|
||||
return val
|
||||
} else {
|
||||
return nil
|
||||
return ctx.Value(key)
|
||||
}
|
||||
}
|
||||
|
||||
// CustomDataExist 判断
|
||||
// customDataExist 判断
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:04 2025/2/28
|
||||
func (ctx *Context) CustomDataExist(key string) any {
|
||||
ctx.lock.RLock()
|
||||
defer ctx.lock.RUnlock()
|
||||
_, exist := ctx.CustomData[key]
|
||||
func (ctx *Context) customDataExist(key string) any {
|
||||
ctx.customLock.RLock()
|
||||
defer ctx.customLock.RUnlock()
|
||||
_, exist := ctx.customData[key]
|
||||
return exist
|
||||
}
|
||||
|
||||
// CustomDataAppend 向数组数据中追加元素
|
||||
// customDataAppend 向数组数据中追加元素
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:07 2025/2/28
|
||||
func (ctx *Context) CustomDataAppend(key string, appendValue any) {
|
||||
ctx.lock.Lock()
|
||||
defer ctx.lock.Unlock()
|
||||
val, exist := ctx.CustomData[key]
|
||||
func (ctx *Context) customDataAppend(key string, appendValue any) {
|
||||
ctx.customLock.Lock()
|
||||
defer ctx.customLock.Unlock()
|
||||
val, exist := ctx.customData[key]
|
||||
if !exist || nil == val {
|
||||
ctx.CustomData[key] = []any{appendValue}
|
||||
ctx.customData[key] = []any{appendValue}
|
||||
return
|
||||
}
|
||||
if valArr, ok := val.([]any); ok {
|
||||
valArr = append(valArr, appendValue)
|
||||
ctx.CustomData[key] = valArr
|
||||
ctx.customData[key] = valArr
|
||||
}
|
||||
// 非数组, 忽略追加行为
|
||||
}
|
||||
|
||||
// CustomDataAppendProperty 向一个map中追加/更新属性
|
||||
// customDataAppendProperty 向一个map中追加/更新属性
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:11 2025/2/28
|
||||
func (ctx *Context) CustomDataAppendProperty(key string, appendKey string, appendValue any) {
|
||||
ctx.lock.Lock()
|
||||
defer ctx.lock.Unlock()
|
||||
val, exist := ctx.CustomData[key]
|
||||
func (ctx *Context) customDataAppendProperty(key string, appendKey string, appendValue any) {
|
||||
ctx.customLock.Lock()
|
||||
defer ctx.customLock.Unlock()
|
||||
val, exist := ctx.customData[key]
|
||||
if !exist || nil == val {
|
||||
ctx.CustomData[key] = map[string]any{
|
||||
ctx.customData[key] = map[string]any{
|
||||
appendKey: appendValue,
|
||||
}
|
||||
return
|
||||
}
|
||||
if valMap, ok := val.(map[string]any); ok {
|
||||
valMap[appendKey] = appendValue
|
||||
ctx.CustomData[key] = valMap
|
||||
ctx.customData[key] = valMap
|
||||
}
|
||||
// 非map, 忽略追加行为
|
||||
}
|
||||
|
Reference in New Issue
Block a user