forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck.go
84 lines (70 loc) · 2.36 KB
/
healthcheck.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
package server
import (
"io"
"net/http"
)
// HealthCheckHandler is an interface used by SimpleServer and RPCServer
// to allow users to customize their service's health check. Start will be
// called just before server start up and the given ActivityMonitor should
// offer insite to the # of requests in flight, if needed.
// Stop will be called once the servers receive a kill signal.
type HealthCheckHandler interface {
http.Handler
Path() string
Start(*ActivityMonitor) error
Stop() error
}
// SimpleHealthCheck is a basic HealthCheckHandler implementation
// that _always_ returns with an "ok" status and shuts down immediately.
type SimpleHealthCheck struct {
path string
}
// NewSimpleHealthCheck will return a new SimpleHealthCheck instance.
func NewSimpleHealthCheck(path string) *SimpleHealthCheck {
return &SimpleHealthCheck{path: path}
}
// Path will return the configured status path to server on.
func (s *SimpleHealthCheck) Path() string {
return s.path
}
// Start will do nothing.
func (s *SimpleHealthCheck) Start(monitor *ActivityMonitor) error {
return nil
}
// Stop will do nothing and return nil.
func (s *SimpleHealthCheck) Stop() error {
return nil
}
// ServeHTTP will always respond with "ok-"+server.Name.
func (s *SimpleHealthCheck) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if _, err := io.WriteString(w, "ok-"+Name); err != nil {
LogWithFields(r).Warn("unable to write healthcheck response: ", err)
}
}
// NewCustomHealthCheck will return a new CustomHealthCheck with the given
// path and handler.
func NewCustomHealthCheck(path string, handler http.Handler) *CustomHealthCheck {
return &CustomHealthCheck{path, handler}
}
// CustomHealthCheck is a HealthCheckHandler that uses
// a custom http.Handler provided to the server via `config.CustomHealthCheckHandler`.
type CustomHealthCheck struct {
path string
handler http.Handler
}
// Path will return the configured status path to server on.
func (c *CustomHealthCheck) Path() string {
return c.path
}
// Start will do nothing.
func (c *CustomHealthCheck) Start(monitor *ActivityMonitor) error {
return nil
}
// Stop will do nothing and return nil.
func (c *CustomHealthCheck) Stop() error {
return nil
}
// ServeHTTP will allow the custom handler to manage the request and response.
func (c *CustomHealthCheck) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.handler.ServeHTTP(w, r)
}