Commit 890cdb5c authored by Miek Gieben's avatar Miek Gieben Committed by Yong Tang

plugin/health: cleanups (#2811)

Small, trivial cleanup: got triggered because I saw a comment on how
health plugins polls other plugins which isn't true.

* Remove useless newHealth function
* healthParse -> parse
* Remove useless constants

Net deletion of code.
Signed-off-by: default avatarMiek Gieben <miek@miek.nl>
parent e178291e
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
## Description ## Description
Enabled process wide health endpoint. When CoreDNS is up and running this returns a 200 OK http Enabled process wide health endpoint. When CoreDNS is up and running this returns a 200 OK HTTP
status code. The health is exported, by default, on port 8080/health . status code. The health is exported, by default, on port 8080/health .
## Syntax ## Syntax
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
var log = clog.NewWithPlugin("health") var log = clog.NewWithPlugin("health")
// Health implements healthchecks by polling plugins. // Health implements healthchecks by exporting a HTTP endpoint.
type health struct { type health struct {
Addr string Addr string
lameduck time.Duration lameduck time.Duration
...@@ -24,14 +24,9 @@ type health struct { ...@@ -24,14 +24,9 @@ type health struct {
stop chan bool stop chan bool
} }
// newHealth returns a new initialized health.
func newHealth(addr string) *health {
return &health{Addr: addr, stop: make(chan bool)}
}
func (h *health) OnStartup() error { func (h *health) OnStartup() error {
if h.Addr == "" { if h.Addr == "" {
h.Addr = defAddr h.Addr = ":8080"
} }
ln, err := net.Listen("tcp", h.Addr) ln, err := net.Listen("tcp", h.Addr)
...@@ -43,10 +38,10 @@ func (h *health) OnStartup() error { ...@@ -43,10 +38,10 @@ func (h *health) OnStartup() error {
h.mux = http.NewServeMux() h.mux = http.NewServeMux()
h.nlSetup = true h.nlSetup = true
h.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { h.mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
// We're always healthy. // We're always healthy.
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
io.WriteString(w, ok) io.WriteString(w, "OK")
return return
}) })
...@@ -74,9 +69,3 @@ func (h *health) OnFinalShutdown() error { ...@@ -74,9 +69,3 @@ func (h *health) OnFinalShutdown() error {
close(h.stop) close(h.stop)
return nil return nil
} }
const (
ok = "OK"
defAddr = ":8080"
path = "/health"
)
...@@ -9,15 +9,14 @@ import ( ...@@ -9,15 +9,14 @@ import (
) )
func TestHealth(t *testing.T) { func TestHealth(t *testing.T) {
h := newHealth(":0") h := &health{Addr: ":0", stop: make(chan bool)}
if err := h.OnStartup(); err != nil { if err := h.OnStartup(); err != nil {
t.Fatalf("Unable to startup the health server: %v", err) t.Fatalf("Unable to startup the health server: %v", err)
} }
defer h.OnFinalShutdown() defer h.OnFinalShutdown()
// Reconstruct the http address based on the port allocated by operating system. address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), "/health")
address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), path)
response, err := http.Get(address) response, err := http.Get(address)
if err != nil { if err != nil {
...@@ -32,14 +31,13 @@ func TestHealth(t *testing.T) { ...@@ -32,14 +31,13 @@ func TestHealth(t *testing.T) {
} }
response.Body.Close() response.Body.Close()
if string(content) != ok { if string(content) != "OK" {
t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content)) t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
} }
} }
func TestHealthLameduck(t *testing.T) { func TestHealthLameduck(t *testing.T) {
h := newHealth(":0") h := &health{Addr: ":0", stop: make(chan bool), lameduck: 250 * time.Millisecond}
h.lameduck = 250 * time.Millisecond
if err := h.OnStartup(); err != nil { if err := h.OnStartup(); err != nil {
t.Fatalf("Unable to startup the health server: %v", err) t.Fatalf("Unable to startup the health server: %v", err)
......
...@@ -18,6 +18,7 @@ func (h *health) overloaded() { ...@@ -18,6 +18,7 @@ func (h *health) overloaded() {
} }
url := "http://" + h.Addr url := "http://" + h.Addr
tick := time.NewTicker(1 * time.Second) tick := time.NewTicker(1 * time.Second)
defer tick.Stop()
for { for {
select { select {
...@@ -32,7 +33,6 @@ func (h *health) overloaded() { ...@@ -32,7 +33,6 @@ func (h *health) overloaded() {
HealthDuration.Observe(time.Since(start).Seconds()) HealthDuration.Observe(time.Since(start).Seconds())
case <-h.stop: case <-h.stop:
tick.Stop()
return return
} }
} }
......
...@@ -19,13 +19,12 @@ func init() { ...@@ -19,13 +19,12 @@ func init() {
} }
func setup(c *caddy.Controller) error { func setup(c *caddy.Controller) error {
addr, lame, err := healthParse(c) addr, lame, err := parse(c)
if err != nil { if err != nil {
return plugin.Error("health", err) return plugin.Error("health", err)
} }
h := newHealth(addr) h := &health{Addr: addr, stop: make(chan bool), lameduck: lame}
h.lameduck = lame
c.OnStartup(func() error { c.OnStartup(func() error {
metrics.MustRegister(c, HealthDuration) metrics.MustRegister(c, HealthDuration)
...@@ -40,7 +39,7 @@ func setup(c *caddy.Controller) error { ...@@ -40,7 +39,7 @@ func setup(c *caddy.Controller) error {
return nil return nil
} }
func healthParse(c *caddy.Controller) (string, time.Duration, error) { func parse(c *caddy.Controller) (string, time.Duration, error) {
addr := "" addr := ""
dur := time.Duration(0) dur := time.Duration(0)
for c.Next() { for c.Next() {
......
...@@ -30,7 +30,7 @@ func TestSetupHealth(t *testing.T) { ...@@ -30,7 +30,7 @@ func TestSetupHealth(t *testing.T) {
for i, test := range tests { for i, test := range tests {
c := caddy.NewTestController("dns", test.input) c := caddy.NewTestController("dns", test.input)
_, _, err := healthParse(c) _, _, err := parse(c)
if test.shouldErr && err == nil { if test.shouldErr && err == nil {
t.Errorf("Test %d: Expected error but found none for input %s", i, test.input) t.Errorf("Test %d: Expected error but found none for input %s", i, test.input)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment