proxy.go 1.9 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
package plugin_flow

import (
	log "github.com/eolinker/goku/goku-log"
	"github.com/eolinker/goku/goku-node/common"
	plugin_manager "github.com/eolinker/goku/goku-node/manager/plugin-manager"
	entity "github.com/eolinker/goku/server/entity/node-entity"

	"reflect"
	"time"
)

// 执行插件的Proxy函数
func ProxyFunc(ctx *common.Context, handleFunc []*entity.PluginHandlerExce) (bool, int) {
	requestId := ctx.RequestId()
	defer func(ctx *common.Context) {
		log.Debug(requestId," Proxy plugin default: begin")
		for _, handler := range plugin_manager.GetDefaultPlugins() {
			if handler.PluginObj.Proxy == nil || reflect.ValueOf(handler.PluginObj.Proxy).IsNil() {
				continue
			}
			ctx.SetPlugin(handler.Name)
			log.Debug(requestId," Proxy plugin :",handler.Name," start")
			now:=time.Now()
			_, err:= handler.PluginObj.Proxy.Proxy(ctx)
			log.Debug(requestId," Proxy plugin :",handler.Name," Duration:",time.Since(now))
			log.Debug(requestId," Proxy plugin :",handler.Name," end")

			if err != nil {
 				log.Warn(requestId," Proxy plugin:",handler.Name," error:",err.Error())
			}
		}
		log.Debug(requestId," Proxy plugin default: begin")
	}(ctx)
	lastIndex := 0
	log.Debug(requestId," Proxy plugin : begin")
	for index, handler := range handleFunc {
		lastIndex = index
		if handler.PluginObj.Proxy == nil || reflect.ValueOf(handler.PluginObj.Proxy).IsNil() {
			continue
		}

		ctx.SetPlugin(handler.Name)
		log.Debug(requestId," Proxy plugin :",handler.Name," start")
		now:=time.Now()
		flag, err := handler.PluginObj.Proxy.Proxy(ctx)
		log.Debug(requestId," Proxy plugin :",handler.Name," Duration:",time.Since(now))
		log.Debug(requestId," Proxy plugin :",handler.Name," end")

		if err != nil {
			log.Warn(requestId," Proxy plugin :",handler.Name," error: ",err.Error())
		}
		if flag == false && handler.IsStop == true {

			return false, lastIndex
		}
	}
	log.Debug(requestId," Proxy plugin : end")
	return true, lastIndex
}