forked from Meituan-Dianping/cat-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
45 lines (40 loc) · 862 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
package cat
import (
"strconv"
"strings"
)
type serverAddress struct {
host string
port int
}
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
}
}