/
main.go
81 lines (67 loc) · 1.84 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/musicglue/doozer"
"net"
"strings"
"time"
)
type empty struct{}
var (
port = flag.String("port", "8046", "The port to test the server on")
servers = flag.String("servers", "localhost,localhost,localhost", "The servers to test, either strings or IPs")
protocol = flag.String("protocol", "udp", "Use TCP or UDP")
ownIp = flag.String("ownip", "127.0.0.1", "The originating IP of the interface you are connecting from.")
timeout = flag.Int("timeout", 1000, "Timeout in Milliseconds before dialling the next server.")
clustername = flag.String("cluster", "local", "Name of the cluster, local is default")
output string
)
func appendIfMissing(slice []string, s string) (output []string) {
for _, ele := range slice {
if ele == s {
return slice
}
}
return append(slice, s)
}
func checkServer(name string) {
fmt.Println(name)
return
}
func main() {
flag.Parse()
serverList := strings.Split(*servers, ",")
res := make([]string, 0, 0)
sem := make(chan empty, len(serverList))
for ind, server := range serverList {
go func(ind int, name string) {
address := name + ":" + *port
_, err := net.DialTimeout(*protocol, address, time.Duration(*timeout)*time.Millisecond)
if err != nil {
} else {
res = appendIfMissing(res, address)
}
sem <- empty{}
}(ind, server)
}
for i := 0; i < len(serverList); i++ {
<-sem
}
doozer, derr := doozer.Dial(res[0])
if derr != nil {
panic(derr)
// HANDLE THIS PROPERLY
}
// Get latest Revision of DB
_, rerr := doozer.Rev()
// HANDLE THE ERROR
if rerr != nil {
panic("Shit, it's an Errrrrrorrrr!")
// Do some proper error handling here I suppose...
}
host, _, _ := net.SplitHostPort(res[0])
resolved, _ := net.LookupIP(host)
output := net.JoinHostPort(resolved[0].String(), *port)
fmt.Print(output)
}