strategy-api-plugin.go 2.8 KB
Newer Older
Y
Your Name 已提交
1
package strategyapipluginmanager
E
eoLinker API Management 已提交
2 3 4

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

黄孟柱 已提交
10 11 12 13 14
	plugin_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/plugin-manager"
	strategy_plugin_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/strategy-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 已提交
15 16 17
)

func init() {
Y
Your Name 已提交
18
	updater.Add(loadStategyAPIPlugin, 8, "goku_conn_plugin_strategy", "goku_conn_plugin_api", "goku_conn_strategy_api", "goku_gateway_strategy", "goku_gateway_api", "goku_plugin")
E
eoLinker API Management 已提交
19 20 21
}

var (
Y
Your Name 已提交
22
	pluginsOfAPI = make(map[string][]*entity.PluginHandlerExce)
E
eoLinker API Management 已提交
23 24 25
	locker       sync.RWMutex
)

Y
Your Name 已提交
26
func get(strategyID string, apiID int) ([]*entity.PluginHandlerExce, bool) {
E
eoLinker API Management 已提交
27 28
	locker.RLock()
	defer locker.RUnlock()
Y
Your Name 已提交
29 30
	key := strategyID + ":" + strconv.Itoa(apiID)
	p, has := pluginsOfAPI[key]
E
eoLinker API Management 已提交
31 32 33
	return p, has

}
Y
Your Name 已提交
34 35 36 37

//GetPluginsOfAPI 获取接口插件
func GetPluginsOfAPI(strategyID string, apiID int) ([]*entity.PluginHandlerExce, bool) {
	p, has := get(strategyID, apiID)
E
eoLinker API Management 已提交
38
	if !has {
Y
Your Name 已提交
39
		return strategy_plugin_manager.GetPluginsOfStrategy(strategyID)
E
eoLinker API Management 已提交
40 41 42 43 44 45 46 47 48 49 50 51
	}
	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()

Y
Your Name 已提交
52
	pluginsOfAPI = plugins
E
eoLinker API Management 已提交
53 54 55

}

Y
Your Name 已提交
56 57
func loadStategyAPIPlugin() {
	plugins, err := dao_strategy.GetAPIPlugin()
E
eoLinker API Management 已提交
58 59 60 61 62 63
	if err != nil {
		return
	}

	phsa := make(map[string]map[int][]*entity.PluginHandlerExce)
	for _, p := range plugins {
Y
Your Name 已提交
64
		apiID, _ := strconv.Atoi(p.APIId)
E
eoLinker API Management 已提交
65 66 67 68 69 70 71 72 73 74 75 76

		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
		}

Y
Your Name 已提交
77
		obj, err := handle.Factory.Create(p.PluginConfig, nodecommon.ClusterName(), p.UpdateTag, p.StrategyID, apiID)
E
eoLinker API Management 已提交
78 79 80 81 82 83 84 85 86
		if err != nil {
			continue
		}
		handleExec := &entity.PluginHandlerExce{
			PluginObj: obj,
			Priority:  handle.Info.Priority,
			Name:      p.PluginName,
			IsStop:    handle.Info.IsStop,
		}
Y
Your Name 已提交
87
		list, has := phsa[p.StrategyID][apiID]
E
eoLinker API Management 已提交
88 89
		if !has {
			list = make([]*entity.PluginHandlerExce, 0)
Y
Your Name 已提交
90
			phsa[p.StrategyID][apiID] = list
E
eoLinker API Management 已提交
91
		}
Y
Your Name 已提交
92
		phsa[p.StrategyID][apiID] = append(list, handleExec)
E
eoLinker API Management 已提交
93 94 95 96

	}

	phl := make(map[string][]*entity.PluginHandlerExce)
Y
Your Name 已提交
97 98
	for strategyID, pla := range phsa {
		pls, ok := strategy_plugin_manager.GetPluginsOfStrategy(strategyID)
E
eoLinker API Management 已提交
99

Y
Your Name 已提交
100 101
		for apiID, list := range pla {
			key := fmt.Sprintf("%s:%d", strategyID, apiID)
E
eoLinker API Management 已提交
102 103 104 105 106 107 108 109 110 111 112

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

		}
	}

	reset(phl)
}