/
main.go
144 lines (131 loc) · 3.17 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
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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var (
heartbeatMessage string
heartbeatDuration time.Duration
reconnectWait time.Duration
maxFailedAttempts int
port int
host string
uri string
)
const (
StatusStarting = "starting"
StatusConnectFailed = "connect failed"
StatusOk = "operational"
StatusConnecting = "connecting"
StatusWaitingForResponse = "waiting for response"
)
func init() {
flag.StringVar(&heartbeatMessage, "heartbeat-message", "ping", "Hearbeat message")
flag.DurationVar(&heartbeatDuration, "interval", time.Millisecond*500, "Check interval")
flag.DurationVar(&reconnectWait, "reconnect-wait", time.Millisecond*350, "Interval between reconnections")
flag.IntVar(&maxFailedAttempts, "attempts", 3, "Maximum failed hearbeat attempts")
flag.IntVar(&port, "port", 8080, "Listen port")
flag.StringVar(&host, "host", "", "Listen host")
flag.StringVar(&uri, "uri", "ws://localhost:9050", "Check URI")
}
type Application struct {
conn *websocket.Conn
ok bool
status string
attempt int
message []byte
}
func (a *Application) handler(w http.ResponseWriter, r *http.Request) {
if a.ok {
fmt.Fprintln(w, "OK", a.status)
} else {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, "FAILED", a.status)
}
}
func (a *Application) connect() error {
a.ok = false
a.status = StatusConnecting
log.Println("connecting to", uri)
conn, _, err := websocket.DefaultDialer.Dial(uri, nil)
if err != nil {
log.Println("connection failed", err)
a.status = StatusConnectFailed
return err
}
log.Println("connected")
a.status = StatusWaitingForResponse
a.conn = conn
return nil
}
func (a *Application) reconnect() {
toWait := reconnectWait
a.ok = false
for {
if err := a.connect(); err != nil {
log.Println("failed to connect", err)
} else {
break
}
time.Sleep(toWait)
}
}
func (a *Application) checkUntilFail() {
ticker := time.NewTicker(heartbeatDuration)
a.attempt = 0
for _ = range ticker.C {
a.attempt++
if a.attempt > maxFailedAttempts {
ticker.Stop()
return
}
if err := a.conn.WriteMessage(websocket.TextMessage, a.message); err != nil {
log.Println("WriteMessage", err)
continue
}
if err := a.conn.SetReadDeadline(time.Now().Add(heartbeatDuration * 2)); err != nil {
log.Println("SetReadDeadline", err)
continue
}
t, m, err := a.conn.ReadMessage()
if err != nil {
log.Println("ReadMessage", err)
continue
}
if t != websocket.TextMessage {
log.Println("bad message type", t)
continue
}
if bytes.NewBuffer(m).String() != heartbeatMessage {
log.Println("bad heartbeat message")
continue
}
a.status = StatusOk
a.ok = true
a.attempt = 0
}
}
func (a *Application) loop() {
for {
a.reconnect()
a.checkUntilFail()
}
}
func (a *Application) start() error {
listenOn := fmt.Sprintf("%s:%d", host, port)
http.HandleFunc("/", a.handler)
a.status = StatusStarting
a.message = []byte(heartbeatMessage)
go a.loop()
return http.ListenAndServe(listenOn, nil)
}
func main() {
flag.Parse()
a := new(Application)
log.Fatal(a.start())
}