aboutsummaryrefslogtreecommitdiff
path: root/plugin/health
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2019-05-04 21:06:04 +0100
committerGravatar Yong Tang <yong.tang.github@outlook.com> 2019-05-04 16:06:04 -0400
commit890cdb5cab8bb898dd048d853791ec82e28003ad (patch)
tree00e613e16fbdbdc6fba7489192b735cc98714a4b /plugin/health
parente178291ed6a9eae5d24bae132b0f4c2f4d75f662 (diff)
downloadcoredns-890cdb5cab8bb898dd048d853791ec82e28003ad.tar.gz
coredns-890cdb5cab8bb898dd048d853791ec82e28003ad.tar.zst
coredns-890cdb5cab8bb898dd048d853791ec82e28003ad.zip
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: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'plugin/health')
-rw-r--r--plugin/health/README.md2
-rw-r--r--plugin/health/health.go19
-rw-r--r--plugin/health/health_test.go10
-rw-r--r--plugin/health/overloaded.go2
-rw-r--r--plugin/health/setup.go7
-rw-r--r--plugin/health/setup_test.go2
6 files changed, 14 insertions, 28 deletions
diff --git a/plugin/health/README.md b/plugin/health/README.md
index 908d34f4f..a62a55e66 100644
--- a/plugin/health/README.md
+++ b/plugin/health/README.md
@@ -6,7 +6,7 @@
## 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 .
## Syntax
diff --git a/plugin/health/health.go b/plugin/health/health.go
index 895704409..eef45d2cf 100644
--- a/plugin/health/health.go
+++ b/plugin/health/health.go
@@ -12,7 +12,7 @@ import (
var log = clog.NewWithPlugin("health")
-// Health implements healthchecks by polling plugins.
+// Health implements healthchecks by exporting a HTTP endpoint.
type health struct {
Addr string
lameduck time.Duration
@@ -24,14 +24,9 @@ type health struct {
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 {
if h.Addr == "" {
- h.Addr = defAddr
+ h.Addr = ":8080"
}
ln, err := net.Listen("tcp", h.Addr)
@@ -43,10 +38,10 @@ func (h *health) OnStartup() error {
h.mux = http.NewServeMux()
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.
w.WriteHeader(http.StatusOK)
- io.WriteString(w, ok)
+ io.WriteString(w, "OK")
return
})
@@ -74,9 +69,3 @@ func (h *health) OnFinalShutdown() error {
close(h.stop)
return nil
}
-
-const (
- ok = "OK"
- defAddr = ":8080"
- path = "/health"
-)
diff --git a/plugin/health/health_test.go b/plugin/health/health_test.go
index 21433c975..7c7ad43a1 100644
--- a/plugin/health/health_test.go
+++ b/plugin/health/health_test.go
@@ -9,15 +9,14 @@ import (
)
func TestHealth(t *testing.T) {
- h := newHealth(":0")
+ h := &health{Addr: ":0", stop: make(chan bool)}
if err := h.OnStartup(); err != nil {
t.Fatalf("Unable to startup the health server: %v", err)
}
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(), path)
+ address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), "/health")
response, err := http.Get(address)
if err != nil {
@@ -32,14 +31,13 @@ func TestHealth(t *testing.T) {
}
response.Body.Close()
- if string(content) != ok {
+ if string(content) != "OK" {
t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
}
}
func TestHealthLameduck(t *testing.T) {
- h := newHealth(":0")
- h.lameduck = 250 * time.Millisecond
+ h := &health{Addr: ":0", stop: make(chan bool), lameduck: 250 * time.Millisecond}
if err := h.OnStartup(); err != nil {
t.Fatalf("Unable to startup the health server: %v", err)
diff --git a/plugin/health/overloaded.go b/plugin/health/overloaded.go
index 06b0b65e1..04d6a5f26 100644
--- a/plugin/health/overloaded.go
+++ b/plugin/health/overloaded.go
@@ -18,6 +18,7 @@ func (h *health) overloaded() {
}
url := "http://" + h.Addr
tick := time.NewTicker(1 * time.Second)
+ defer tick.Stop()
for {
select {
@@ -32,7 +33,6 @@ func (h *health) overloaded() {
HealthDuration.Observe(time.Since(start).Seconds())
case <-h.stop:
- tick.Stop()
return
}
}
diff --git a/plugin/health/setup.go b/plugin/health/setup.go
index 19aeba58d..0e51ec5ea 100644
--- a/plugin/health/setup.go
+++ b/plugin/health/setup.go
@@ -19,13 +19,12 @@ func init() {
}
func setup(c *caddy.Controller) error {
- addr, lame, err := healthParse(c)
+ addr, lame, err := parse(c)
if err != nil {
return plugin.Error("health", err)
}
- h := newHealth(addr)
- h.lameduck = lame
+ h := &health{Addr: addr, stop: make(chan bool), lameduck: lame}
c.OnStartup(func() error {
metrics.MustRegister(c, HealthDuration)
@@ -40,7 +39,7 @@ func setup(c *caddy.Controller) error {
return nil
}
-func healthParse(c *caddy.Controller) (string, time.Duration, error) {
+func parse(c *caddy.Controller) (string, time.Duration, error) {
addr := ""
dur := time.Duration(0)
for c.Next() {
diff --git a/plugin/health/setup_test.go b/plugin/health/setup_test.go
index 4db6fc770..35dc15509 100644
--- a/plugin/health/setup_test.go
+++ b/plugin/health/setup_test.go
@@ -30,7 +30,7 @@ func TestSetupHealth(t *testing.T) {
for i, test := range tests {
c := caddy.NewTestController("dns", test.input)
- _, _, err := healthParse(c)
+ _, _, err := parse(c)
if test.shouldErr && err == nil {
t.Errorf("Test %d: Expected error but found none for input %s", i, test.input)