forked from martensson/vaban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.go
73 lines (68 loc) · 1.56 KB
/
ping.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
package main
import (
"log"
"net"
"net/http"
"strings"
"sync"
"github.com/julienschmidt/httprouter"
)
func Pinger(server string, secret string) string {
conn, err := net.Dial("tcp", server)
if err != nil {
return err.Error()
}
defer conn.Close()
err = varnishAuth(server, secret, conn)
if err != nil {
return err.Error()
}
_, err = conn.Write([]byte("ping\n"))
if err != nil {
return err.Error()
}
pong := make([]byte, 32)
_, err = conn.Read(pong)
if err != nil {
return err.Error()
}
status := string(pong)[13:32]
status = strings.Trim(status, " ")
return status
}
func GetPing(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
service := ps.ByName("service")
if s, ok := services[service]; ok {
// We need the WaitGroup for some awesome Go concurrency of our BANs
var wg sync.WaitGroup
messages := Messages{}
for _, server := range s.Hosts {
// Increment the WaitGroup counter.
wg.Add(1)
go func(server string) {
// Decrement the counter when the goroutine completes.
defer wg.Done()
message := Message{}
message.Msg = Pinger(server, s.Secret)
messages[server] = message
}(server)
}
// Wait for all PINGs to complete.
wg.Wait()
err := r.JSON(w, http.StatusOK, messages)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write([]byte(err.Error()))
if err != nil {
log.Println(err)
}
}
} else {
w.WriteHeader(http.StatusNotFound)
_, err := w.Write([]byte("Service could not be found."))
if err != nil {
log.Println(err)
}
return
}
}