增加插件接口及插件结果定义
This commit is contained in:
parent
8022a8533f
commit
942cd1dfed
@ -21,13 +21,30 @@ import (
|
|||||||
//
|
//
|
||||||
// Date : 17:19 2024/11/11
|
// Date : 17:19 2024/11/11
|
||||||
type RequestContext struct {
|
type RequestContext struct {
|
||||||
ctx context.Context `json:"-"` // gin 上下文
|
ctx context.Context // ctx 上下文
|
||||||
runtimeInstance *trace.Runtime `json:"-"` // 链路统一追踪实例
|
runtimeInstance *trace.Runtime // 链路统一追踪实例
|
||||||
lock *sync.RWMutex `json:"-"` // 数据锁
|
lock *sync.RWMutex // 数据锁
|
||||||
TraceID string `json:"trace_id"` // 全链路追踪的trace_id
|
traceID string // 全链路追踪的trace_id
|
||||||
GatewayUrlConfig *ApiConfig `json:"gateway_url_config"` // 网关接口的配置
|
gatewayUrlConfig *ApiConfig // 网关接口的配置
|
||||||
RequestInfo *RequestInfo `json:"request_info"` // 网关请求信息
|
requestInfo *RequestInfo // 网关请求信息
|
||||||
BackendApiResultTable map[string]*httpclientDefine.Response `json:"backend_api_result_table"` // 后端接口返回数据详细信息: 接口别名 => 请求结果
|
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() {
|
func (rc *RequestContext) Lock() {
|
||||||
|
24
define/plugin.go
Normal file
24
define/plugin.go
Normal file
@ -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"` // 插件的配置数据
|
||||||
|
}
|
30
plugins/abstract.go
Normal file
30
plugins/abstract.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user