stategy-plugin.go 1.9 KB
Newer Older
Y
Your Name 已提交
1
package strategypluginmanager
E
eoLinker API Management 已提交
2 3

import (
黄孟柱 已提交
4
	"github.com/eolinker/goku-api-gateway/goku-node/node-common"
E
eoLinker API Management 已提交
5 6 7
	"sort"
	"sync"

黄孟柱 已提交
8 9 10 11
	plugin_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/plugin-manager"
	"github.com/eolinker/goku-api-gateway/goku-node/manager/updater"
	dao_strategy "github.com/eolinker/goku-api-gateway/server/dao/node-mysql/dao-strategy"
	entity "github.com/eolinker/goku-api-gateway/server/entity/node-entity"
E
eoLinker API Management 已提交
12 13 14 15 16 17 18 19 20 21 22
)

func init() {
	updater.Add(loadStategyPlugin, 7, "goku_conn_plugin_strategy", "goku_gateway_strategy", "goku_plugin")
}

var (
	pluginsOfStrategy = make(map[string][]*entity.PluginHandlerExce)
	locker            sync.RWMutex
)

Y
Your Name 已提交
23 24
//GetPluginsOfStrategy 获取策略插件
func GetPluginsOfStrategy(strategyID string) ([]*entity.PluginHandlerExce, bool) {
E
eoLinker API Management 已提交
25 26
	locker.RLock()
	defer locker.RUnlock()
Y
Your Name 已提交
27
	p, has := pluginsOfStrategy[strategyID]
E
eoLinker API Management 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	if !has {
		return nil, false
	}
	return p, true
}

func reset(plugins map[string][]*entity.PluginHandlerExce) {
	for name := range plugins {
		//plugins[name] = append(list,def...)
		sort.Sort(sort.Reverse(entity.PluginSlice(plugins[name])))
	}
	locker.Lock()
	defer locker.Unlock()

	pluginsOfStrategy = plugins

}

func loadStategyPlugin() {
	plugins, err := dao_strategy.GetAllStrategyPluginList()
	if err != nil {
		return
	}
	phl := make(map[string][]*entity.PluginHandlerExce)
	for _, p := range plugins {
		if _, ok := phl[p.StrategyID]; !ok {
			phl[p.StrategyID] = make([]*entity.PluginHandlerExce, 0)
		}
		handle := plugin_manager.GetPluginHandle(p.PluginName)
		if handle == nil {
			continue
		}
Y
Your Name 已提交
60
		excer, err := handle.Factory.Create(p.PluginConfig, nodecommon.ClusterName(), p.UpdateTag, p.StrategyID, 0)
E
eoLinker API Management 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74
		if err != nil {

			continue
		}
		handleExec := &entity.PluginHandlerExce{
			PluginObj: excer,
			Name:      p.PluginName,
			Priority:  handle.Info.Priority,
			IsStop:    handle.Info.IsStop,
		}
		phl[p.StrategyID] = append(phl[p.StrategyID], handleExec)
	}
	reset(phl)
}