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

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 23 24 25 26 27 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
)

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
)

func GetPluginsOfStrategy(strategyId string) ([]*entity.PluginHandlerExce, bool) {
	locker.RLock()
	defer locker.RUnlock()
	p, has := pluginsOfStrategy[strategyId]
	if !has {
		return nil, false
	}
	return p, true
}

func reset(plugins map[string][]*entity.PluginHandlerExce) {
	//def:=plugin_manager.GetDefaultPlugins()
	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
		}
		excer, err := handle.Factory.Create(p.PluginConfig, node_common.ClusterName(), p.UpdateTag, p.StrategyID, 0)
		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)
}