mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
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>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user