forked from yeabow/cat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
46 lines (41 loc) · 937 Bytes
/
common.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
package cat
import (
"strconv"
"strings"
)
type serverAddress struct {
Host string `json:"host"`
Port int `json:"port"`
HttpPort int `json:"http_port"`
}
func resolveServerAddresses(router string) (addresses []serverAddress) {
for _, segment := range strings.Split(router, ";") {
if len(segment) == 0 {
continue
}
fragments := strings.Split(segment, ":")
if len(fragments) != 2 {
logger.Warning("%s isn't a valid server address.", segment)
continue
}
if port, err := strconv.Atoi(fragments[1]); err != nil {
logger.Warning("%s isn't a valid server address.", segment)
} else {
addresses = append(addresses, serverAddress{
Host: fragments[0],
Port: port,
})
}
}
return
}
func compareServerAddress(a, b *serverAddress) bool {
if a == nil || b == nil {
return false
}
if strings.Compare(a.Host, b.Host) == 0 {
return a.Port == b.Port
} else {
return false
}
}