strategy-api-plugin.go 2.7 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
package strategy_api_plugin_manager

import (
	"fmt"
	"github.com/eolinker/goku/goku-node/node-common"
	"sort"
	"strconv"
	"sync"

	plugin_manager "github.com/eolinker/goku/goku-node/manager/plugin-manager"
	strategy_plugin_manager "github.com/eolinker/goku/goku-node/manager/strategy-plugin-manager"
	"github.com/eolinker/goku/goku-node/manager/updater"
	dao_strategy "github.com/eolinker/goku/server/dao/node-mysql/dao-strategy"
	entity "github.com/eolinker/goku/server/entity/node-entity"
)

func init() {
	updater.Add(loadStategyApiPlugin, 8, "goku_conn_plugin_strategy", "goku_conn_plugin_api", "goku_conn_strategy_api", "goku_gateway_strategy", "goku_gateway_api", "goku_plugin")
}

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

func get(strategyId string, apiId int) ([]*entity.PluginHandlerExce, bool) {
	locker.RLock()
	defer locker.RUnlock()
	key := strategyId + ":" + strconv.Itoa(apiId)
	p, has := pluginsOfApi[key]
	return p, has

}
func GetPluginsOfApi(strategyId string, apiId int) ([]*entity.PluginHandlerExce, bool) {
	p, has := get(strategyId, apiId)
	if !has {
		return strategy_plugin_manager.GetPluginsOfStrategy(strategyId)
	}
	return p, true
}

func reset(plugins map[string][]*entity.PluginHandlerExce) {
	for name := range plugins {
		sort.Sort(sort.Reverse(entity.PluginSlice(plugins[name])))
	}

	locker.Lock()
	defer locker.Unlock()

	pluginsOfApi = plugins

}

func loadStategyApiPlugin() {
	plugins, err := dao_strategy.GetApiPlugin()
	if err != nil {
		return
	}

	phsa := make(map[string]map[int][]*entity.PluginHandlerExce)
	for _, p := range plugins {
		apiId, _ := strconv.Atoi(p.ApiId)

		phs, has := phsa[p.StrategyID]
		if !has {
			phs = make(map[int][]*entity.PluginHandlerExce)
		}
		phsa[p.StrategyID] = phs

		handle := plugin_manager.GetPluginHandle(p.PluginName)
		if handle == nil {
			continue
		}

		obj, err := handle.Factory.Create(p.PluginConfig, node_common.ClusterName(), p.UpdateTag, p.StrategyID, apiId)
		if err != nil {
			continue
		}
		handleExec := &entity.PluginHandlerExce{
			PluginObj: obj,
			Priority:  handle.Info.Priority,
			Name:      p.PluginName,
			IsStop:    handle.Info.IsStop,
		}
		list, has := phsa[p.StrategyID][apiId]
		if !has {
			list = make([]*entity.PluginHandlerExce, 0)
			phsa[p.StrategyID][apiId] = list
		}
		phsa[p.StrategyID][apiId] = append(list, handleExec)

	}

	phl := make(map[string][]*entity.PluginHandlerExce)
	for strategyId, pla := range phsa {
		pls, ok := strategy_plugin_manager.GetPluginsOfStrategy(strategyId)

		for apiId, list := range pla {
			key := fmt.Sprintf("%s:%d", strategyId, apiId)

			if ok {
				list = append(list, pls...)
			}
			phl[key] = list

		}
	}

	reset(phl)
}