/
health.go
85 lines (67 loc) · 1.56 KB
/
health.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"time"
)
type health struct {
status error
when time.Time
}
func serveHealth(app *application, addr, path string) {
const me = "serveHealth"
var lastQueue string
var lastStatus health
var lastFind time.Time
const cacheTTL = 10 * time.Second
http.HandleFunc(path, func(w http.ResponseWriter, _ /*r*/ *http.Request) {
var h health
var queue string
elap := time.Since(lastFind)
cache := elap < cacheTTL
if cache {
// use cached
h = lastStatus
queue = lastQueue
} else {
// scan health
h, queue = findError(app)
// save in cache
lastQueue = queue
lastStatus = h
lastFind = time.Now()
}
if h.status == nil {
//
// healthy
//
io.WriteString(w, fmt.Sprintf("200 server ok (cached:%t)\n", cache))
return
}
//
// unhealthy
//
msg := fmt.Sprintf("500 server failing: queue:%s error:%v when:%v (cached:%t)\n",
queue, h.status, h.when, cache)
http.Error(w, msg, 500)
})
log.Printf("%s: starting health server at: %s %s", me, addr, path)
listenAndServe(addr, nil)
}
func findError(app *application) (health, string) {
for _, q := range app.queues {
status := q.getStatus()
//log.Printf("findError: %s: %v", q.conf.ID, status)
if status.status != nil {
return status, q.conf.ID // found error
}
}
return health{}, "" // no error
}
func listenAndServe(addr string, handler http.Handler) {
server := &http.Server{Addr: addr, Handler: handler}
err := server.ListenAndServe()
log.Fatalf("listenAndServe: addr=%s error: %v", addr, err)
}