/
group.go
166 lines (148 loc) · 3.42 KB
/
group.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package conf
import (
"fmt"
"log"
"math/rand"
"mi_com_tool_dataset/util"
"strconv"
"strings"
"github.com/Maxgis/tree"
"github.com/go-ini/ini"
)
var (
Tags = make(map[string]*Group)
DEFAULE_TIMEOUT uint64 = 10000
DEFAULT_PROTOCOL = "http"
DEFAULT_MAXRETRY uint = 1
)
type Group struct {
Timeout uint64
Protocol string
Balance string
MaxRetry uint
Host string
Machines *[]*Machine
}
type Machine struct {
Host string
Port int
Enable bool
FailTime int
}
func init() {
file := util.GetCurrentDirectory() + "/conf/group.ini"
var err error
cfg, err = ini.LooseLoad("filename", file)
if err != nil {
log.Fatal(err)
}
sections := cfg.Sections()
for _, section := range sections {
Tags[section.Name()] = initGroup(section)
//log.Printf("%#v\n", Tags[section.Name()])
if Tags[section.Name()].Machines == nil {
continue
}
for _, machine := range *Tags[section.Name()].Machines {
log.Printf("%#v\n", machine)
}
}
tree.Print(Tags)
}
func initGroup(section *ini.Section) *Group {
timeout := getTimeout(section)
protocol := getProtocol(section)
maxRetry := getMaxRetry(section)
host := getHost(section)
machines := getMachines(section)
return &Group{
Timeout: timeout,
Protocol: protocol,
MaxRetry: maxRetry,
Host: host,
Machines: machines,
}
}
func getTimeout(section *ini.Section) uint64 {
var err error
var timeoutKey *ini.Key
timeoutKey, err = section.GetKey("timeout")
if err != nil {
return DEFAULE_TIMEOUT
}
return timeoutKey.MustUint64(DEFAULE_TIMEOUT)
}
func getProtocol(section *ini.Section) string {
var err error
var protocolKey *ini.Key
protocolKey, err = section.GetKey("protocol")
if err != nil {
return DEFAULT_PROTOCOL
}
protocol := protocolKey.String()
if protocol == "" {
return DEFAULT_PROTOCOL
}
return protocol
}
func getMaxRetry(section *ini.Section) uint {
var err error
var maxRetryKey *ini.Key
maxRetryKey, err = section.GetKey("max_retry")
if err != nil {
return DEFAULT_MAXRETRY
}
maxRetry := maxRetryKey.MustUint(DEFAULT_MAXRETRY)
return maxRetry
}
func getHost(section *ini.Section) string {
var err error
var hostKey *ini.Key
hostKey, err = section.GetKey("host")
if err != nil {
return ""
}
return hostKey.MustString("")
}
func getMachines(section *ini.Section) *[]*Machine {
var err error
var machinesKey *ini.Key
machinesKey, err = section.GetKey("machines")
if err != nil {
return nil
}
machinesStr := machinesKey.MustString("")
machinesArr := strings.Split(machinesStr, ",")
length := len(machinesArr)
if length == 0 {
return nil
}
machines := make([]*Machine, length)
for i, machineStr := range machinesArr {
machineInfo := strings.Split(machineStr, ":")
var port int
if len(machineInfo) > 1 {
port, err = strconv.Atoi(machineInfo[1])
}
machines[i] = &Machine{Host: machineInfo[0], Port: port, Enable: true}
}
return &machines
}
func BuildURI(tagName string, path string) string {
group := Tags[tagName]
machine := getMachine(group.Balance, group.Machines)
return fmt.Sprintf("%s://%s:%d%s", group.Protocol, machine.Host, machine.Port, path)
}
func getMachine(balance string, machines *[]*Machine) *Machine {
switch balance {
case "random":
return getRandomMachine(machines)
default:
return getRandomMachine(machines)
}
}
func getRandomMachine(machines *[]*Machine) *Machine {
length := len((*machines))
index := rand.Intn(length)
return (*machines)[index]
}