staticSources.go 3.1 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package static

import (
	"errors"
	"fmt"
	"time"

Y
Your Name 已提交
8 9 10
	"github.com/eolinker/goku-api-gateway/config"
	"github.com/eolinker/goku-api-gateway/goku-service/health"

E
eoLinker API Management 已提交
11 12 13
	"strconv"
	"strings"
	"unicode"
Y
Your Name 已提交
14 15

	"github.com/eolinker/goku-api-gateway/goku-service/common"
E
eoLinker API Management 已提交
16
)
Y
Your Name 已提交
17

Y
Your Name 已提交
18
//ErrorNoInstance errorNoInstance
E
eoLinker API Management 已提交
19
var ErrorNoInstance = errors.New("no instance")
Y
Your Name 已提交
20

Y
Your Name 已提交
21
//Sources source
E
eoLinker API Management 已提交
22 23 24
type Sources struct {
	name string

Y
Your Name 已提交
25
	discovery          *Discovery
E
eoLinker API Management 已提交
26
	healthCheckHandler health.CheckHandler
Y
Your Name 已提交
27
	instanceFactory    *common.InstanceFactory
E
eoLinker API Management 已提交
28 29
}

Y
Your Name 已提交
30 31
//SetHealthConfig setHealthConfig
func (s *Sources) SetHealthConfig(conf *config.HealthCheckConfig) {
Y
Your Name 已提交
32
	if conf == nil || !conf.IsHealthCheck {
E
eoLinker API Management 已提交
33 34 35 36 37
		s.Close()
		return
	}

	s.healthCheckHandler.Open(
Y
Your Name 已提交
38
		conf.URL,
E
eoLinker API Management 已提交
39 40 41 42 43
		conf.StatusCode,
		conf.Second,
		time.Duration(conf.TimeOutMill)*time.Millisecond)
}

Y
Your Name 已提交
44
//Close close
E
eoLinker API Management 已提交
45 46
func (s *Sources) Close() {
	instances := s.healthCheckHandler.Close()
Y
Your Name 已提交
47 48
	for _, instance := range instances {
		instance.ChangeStatus(common.InstanceChecking, common.InstanceRun)
E
eoLinker API Management 已提交
49 50 51
	}
}

Y
Your Name 已提交
52
//CheckDriver checkDriver
E
eoLinker API Management 已提交
53 54 55 56 57
func (s *Sources) CheckDriver(driverName string) bool {

	return driverName == DriverName
}

Y
Your Name 已提交
58
//SetDriverConfig setDriverConfig
Y
Your Name 已提交
59
func (s *Sources) SetDriverConfig(config string) error {
E
eoLinker API Management 已提交
60 61 62
	return nil
}

Y
Your Name 已提交
63
//GetApp getApp
Y
Your Name 已提交
64 65 66 67
func (s *Sources) GetApp(app string) (*common.Service, health.CheckHandler, bool) {
	service, e := s.decode(app)
	if e != nil {
		return nil, nil, false
E
eoLinker API Management 已提交
68
	}
Y
Your Name 已提交
69
	return service, s.healthCheckHandler, true
E
eoLinker API Management 已提交
70 71
}

Y
Your Name 已提交
72
//NewStaticSources 创建Sources
E
eoLinker API Management 已提交
73 74 75
func NewStaticSources(name string) *Sources {
	return &Sources{

Y
Your Name 已提交
76
		name:               name,
Y
Your Name 已提交
77
		discovery:          new(Discovery),
Y
Your Name 已提交
78 79
		healthCheckHandler: &health.CheckBox{},
		instanceFactory:    common.NewInstanceFactory(),
E
eoLinker API Management 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	}
}

func fields(str string) []string {

	words := strings.FieldsFunc(strings.Join(strings.Split(str, ";"), " ; "), func(r rune) bool {
		if unicode.IsSpace(r) {
			return true
		}

		return false
	})
	return words
}

Y
Your Name 已提交
95
func (s *Sources) decode(config string) (*common.Service, error) {
E
eoLinker API Management 已提交
96 97 98

	words := fields(config)

Y
Your Name 已提交
99 100
	instances := make([]*common.Instance, 0, 5)
	nodes := make([]*Node, 0, 5)
Y
Your Name 已提交
101
	var node *Node
E
eoLinker API Management 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	index := 0
	for _, word := range words {
		if word == ";" {
			index = 0
			node = nil
			continue
		}
		l := len(word)
		value := word
		if word[l-1] == ';' {
			value = word[:l-1]
		}
		switch index {
		case 0:
			{
				node = new(Node)
Y
Your Name 已提交
118 119 120
				vs := strings.Split(value, ":")
				if len(vs) > 2 {
					return nil, fmt.Errorf("decode ip:port failt for[%s]", value)
E
eoLinker API Management 已提交
121
				}
Y
Your Name 已提交
122 123 124
				node.IP = vs[0]
				if len(vs) == 2 {
					node.Port, _ = strconv.Atoi(vs[1])
E
eoLinker API Management 已提交
125
				}
Y
Your Name 已提交
126
				nodes = append(nodes, node)
E
eoLinker API Management 已提交
127 128 129 130 131 132

			}
		case 1:
			{
				weight, err := strconv.Atoi(value)
				if err != nil {
Y
Your Name 已提交
133
					return nil, err
E
eoLinker API Management 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
				}
				node.Weight = weight
			}
		case 2:
			{
				//node.Status = common.ParseStatus(value)
			}
		}
		if node.Weight == 0 {
			node.Weight = 1
		}
		if word[l-1] == ';' {
			index = 0
			node = nil
		} else {
			index++
		}
	}

	if len(nodes) > 0 {
Y
Your Name 已提交
154 155
		for _, n := range nodes {
			instance := s.instanceFactory.General(n.IP, n.Port, n.Weight)
E
eoLinker API Management 已提交
156 157
			instances = append(instances, instance)
		}
Y
Your Name 已提交
158 159
		s := common.NewService("static_upstream", instances)
		return s, nil
E
eoLinker API Management 已提交
160
	}
Y
Your Name 已提交
161
	return nil, ErrorNoInstance
E
eoLinker API Management 已提交
162 163

}