From ad874d13f61672a442a6ec87408e83315fde17e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 20 Nov 2024 15:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0requestcontext=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/context.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/define/context.go b/define/context.go index 3d17a37..4c4e3e0 100644 --- a/define/context.go +++ b/define/context.go @@ -9,26 +9,42 @@ package define import ( "context" - httpclientDefine "git.zhangdeman.cn/gateway/httpclient/define" validatorDefine "git.zhangdeman.cn/gateway/validator/define" "git.zhangdeman.cn/zhangdeman/trace" "sync" ) +// NewRequestContext 获取context实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 15:44 2024/11/20 +func NewRequestContext(ctx context.Context, traceID string) *RequestContext { + if nil == ctx { + ctx = context.Background() + } + return &RequestContext{ + ctx: ctx, + runtimeInstance: trace.NewRuntime(traceID, 1), + lock: &sync.RWMutex{}, + traceID: traceID, + pluginResultTable: make(map[string][]*PluginResult), + data: make(map[string]any), + } +} + // RequestContext 请求配置 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:19 2024/11/11 type RequestContext struct { - ctx context.Context // ctx 上下文 - runtimeInstance *trace.Runtime // 链路统一追踪实例 - lock *sync.RWMutex // 数据锁 - traceID string // 全链路追踪的trace_id - gatewayUrlConfig *ApiConfig // 网关接口的配置 - requestInfo *RequestInfo // 网关请求信息 - backendApiResultTable map[string]*httpclientDefine.Response // 后端接口返回数据详细信息: 接口别名 => 请求结果 - pluginResultTable map[string][]*PluginResult // 插件执行结果记录 + ctx context.Context // ctx 上下文 + runtimeInstance *trace.Runtime // 链路统一追踪实例 + lock *sync.RWMutex // 数据锁 + traceID string // 全链路追踪的trace_id + pluginResultTable map[string][]*PluginResult // 插件执行结果记录 + data map[string]any // 自定义设置的数据 } // SetPluginResult 设置插件执行结果