From 942cd1dfedfe8eed50f2b6c9e36bf3e776bd0225 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, 13 Nov 2024 17:54:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8F=92=E4=BB=B6=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=8F=8A=E6=8F=92=E4=BB=B6=E7=BB=93=E6=9E=9C=E5=AE=9A?= =?UTF-8?q?=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/context.go | 31 ++++++++++++++++++++++++------- define/plugin.go | 24 ++++++++++++++++++++++++ plugins/abstract.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 define/plugin.go create mode 100644 plugins/abstract.go diff --git a/define/context.go b/define/context.go index 7c470b2..078b72a 100644 --- a/define/context.go +++ b/define/context.go @@ -21,13 +21,30 @@ import ( // // Date : 17:19 2024/11/11 type RequestContext struct { - ctx context.Context `json:"-"` // gin 上下文 - runtimeInstance *trace.Runtime `json:"-"` // 链路统一追踪实例 - lock *sync.RWMutex `json:"-"` // 数据锁 - TraceID string `json:"trace_id"` // 全链路追踪的trace_id - GatewayUrlConfig *ApiConfig `json:"gateway_url_config"` // 网关接口的配置 - RequestInfo *RequestInfo `json:"request_info"` // 网关请求信息 - BackendApiResultTable map[string]*httpclientDefine.Response `json:"backend_api_result_table"` // 后端接口返回数据详细信息: 接口别名 => 请求结果 + 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 // 插件执行结果记录 +} + +// SetPluginResult 设置插件执行结果 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:53 2024/11/13 +func (rc *RequestContext) SetPluginResult(r *PluginResult) { + rc.Lock() + defer rc.Unlock() + + // 一次请求中, 同一个插件可能多次执行 + if v, exist := rc.pluginResultTable[r.ID]; !exist || nil == v { + rc.pluginResultTable[r.ID] = make([]*PluginResult, 0) + } + rc.pluginResultTable[r.ID] = append(rc.pluginResultTable[r.ID], r) } func (rc *RequestContext) Lock() { diff --git a/define/plugin.go b/define/plugin.go new file mode 100644 index 0000000..c340a0d --- /dev/null +++ b/define/plugin.go @@ -0,0 +1,24 @@ +// Package define ... +// +// Description : define ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-11-13 17:26 +package define + +// PluginResult 插件执行结果 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:28 2024/11/13 +type PluginResult struct { + ID string `json:"id"` // 插件ID + StartTime int64 `json:"start_time"` // 开始执行时间: ms + FinishTime int64 `json:"finish_time"` // 完成执行时间: ms + IsSuccess bool `json:"is_success"` // 是否成功 + SkipFailure bool `json:"skip_failure"` // 执行失败跳过异常继续执行 + Data map[string]any `json:"data"` // 输出的数据 + ErrMsg string `json:"err_msg"` // 失败原因 + Config map[string]any `json:"config"` // 插件的配置数据 +} diff --git a/plugins/abstract.go b/plugins/abstract.go new file mode 100644 index 0000000..19a7e19 --- /dev/null +++ b/plugins/abstract.go @@ -0,0 +1,30 @@ +// Package plugins ... +// +// Description : plugins ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-11-13 16:21 +package plugins + +import ( + "git.zhangdeman.cn/gateway/core/define" +) + +// IPlugin 插件全局接口约束 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:22 2024/11/13 +type IPlugin interface { + // ID 插件ID, 全局唯一, 不允许重复 + ID() string + // Description 插件描述,描述插件的作用 + Description() string + // Config 读取插件的配置 + Config() (map[string]any, error) + // SkipFailure 插件执行失败, 是否跳过 + SkipFailure() bool + // Logic 插件执行的逻辑 + Logic(rc *define.RequestContext) *define.PluginResult +}