-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_config.go
103 lines (94 loc) · 2.3 KB
/
check_config.go
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
package load_balance
import (
"fmt"
"net"
"reflect"
"sort"
"time"
)
const (
//default check setting
DefaultCheckMethod = 0
DefaultCheckTimeout = 2
DefaultCheckMaxErrNum = 2
DefaultCheckInterval = 5
)
type LoadBalanceCheckConf struct {
observers []Observer
confIpWeight map[string]string
activeList []string
format string
}
func (s *LoadBalanceCheckConf) Attach(o Observer) {
s.observers = append(s.observers, o)
}
func (s *LoadBalanceCheckConf) NotifyAllObservers() {
for _, obs := range s.observers {
obs.Update()
}
}
func (s *LoadBalanceCheckConf) GetConf() []string {
confList := []string{}
for _, ip := range s.activeList {
weight, ok := s.confIpWeight[ip]
if !ok {
weight = "50" //默认weight
}
confList = append(confList, fmt.Sprintf(s.format, ip)+","+weight)
}
return confList
}
// 更新配置时,通知监听者也更新
func (s *LoadBalanceCheckConf) WatchConf() {
fmt.Println("watchConf")
go func() {
confIpErrNum := map[string]int{}
for {
changedList := []string{}
for item, _ := range s.confIpWeight {
conn, err := net.DialTimeout("tcp", item, time.Duration(DefaultCheckTimeout)*time.Second)
//todo http statuscode
if err == nil {
conn.Close()
if _, ok := confIpErrNum[item]; ok {
confIpErrNum[item] = 0
}
}
if err != nil {
if _, ok := confIpErrNum[item]; ok {
confIpErrNum[item] += 1
} else {
confIpErrNum[item] = 1
}
}
if confIpErrNum[item] < DefaultCheckMaxErrNum {
changedList = append(changedList, item)
}
}
sort.Strings(changedList)
sort.Strings(s.activeList)
if !reflect.DeepEqual(changedList, s.activeList) {
s.UpdateConf(changedList)
}
time.Sleep(time.Duration(DefaultCheckInterval) * time.Second)
}
}()
}
// 更新配置时,通知监听者也更新
func (s *LoadBalanceCheckConf) UpdateConf(conf []string) {
fmt.Println("UpdateConf", conf)
s.activeList = conf
for _, obs := range s.observers {
obs.Update()
}
}
func NewLoadBalanceCheckConf(format string, conf map[string]string) (*LoadBalanceCheckConf, error) {
aList := []string{}
//默认初始化
for item, _ := range conf {
aList = append(aList, item)
}
mConf := &LoadBalanceCheckConf{format: format, activeList: aList, confIpWeight: conf}
mConf.WatchConf()
return mConf, nil
}