修复插件并发执行的BUG

This commit is contained in:
白茶清欢 2024-11-14 17:19:28 +08:00
parent 507f1f5090
commit 7e30b1f8b9

View File

@ -12,6 +12,7 @@ import (
"git.zhangdeman.cn/gateway/core/define" "git.zhangdeman.cn/gateway/core/define"
"git.zhangdeman.cn/gateway/core/plugins/abstract" "git.zhangdeman.cn/gateway/core/plugins/abstract"
"git.zhangdeman.cn/zhangdeman/trace" "git.zhangdeman.cn/zhangdeman/trace"
"strings"
"sync" "sync"
"time" "time"
) )
@ -81,12 +82,15 @@ func NewPluginResult(rc *define.RequestContext) *define.PluginResult {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:07 2024/11/13 // Date : 18:07 2024/11/13
func Dispatch(rc *define.RequestContext, pluginIDList []string) (map[string]abstract.IPlugin, error) { func Dispatch(rc *define.RequestContext, pluginIDList []string) error {
l := &sync.RWMutex{} l := &sync.RWMutex{}
errList := []string{} errList := []string{}
wg := &sync.WaitGroup{}
for _, itemPluginID := range pluginIDList { for _, itemPluginID := range pluginIDList {
wg.Add(1)
go func(pluginID string) { go func(pluginID string) {
defer func() { defer func() {
wg.Done()
if r := recover(); r != nil { if r := recover(); r != nil {
l.Lock() l.Lock()
errList = append(errList, r.(error).Error()) errList = append(errList, r.(error).Error())
@ -114,9 +118,14 @@ func Dispatch(rc *define.RequestContext, pluginIDList []string) (map[string]abst
l.Unlock() l.Unlock()
return return
} }
rc.SetPluginResult(pluginResult)
}(itemPluginID) }(itemPluginID)
} }
return nil, nil wg.Wait()
if len(errList) > 0 {
return errors.New(strings.Join(errList, "|"))
}
return nil
} }
// InitDefault 初始化注册默认的插件 // InitDefault 初始化注册默认的插件