Toggle /ping to artificially return unhealthy response on SIGTERM during requestAcceptGraceTimeout interval

This commit is contained in:
ravilr 2018-03-22 10:18:03 -07:00 committed by Traefiker Bot
parent 9699dc2a85
commit 5792a19b97
5 changed files with 45 additions and 4 deletions

View file

@ -3,19 +3,38 @@ package ping
import (
"fmt"
"net/http"
"sync"
"github.com/containous/mux"
)
//Handler expose ping routes
// Handler expose ping routes
type Handler struct {
EntryPoint string `description:"Ping entryPoint" export:"true"`
EntryPoint string `description:"Ping entryPoint" export:"true"`
terminating bool
lock sync.RWMutex
}
// SetTerminating causes the ping endpoint to serve non 200 responses.
func (g *Handler) SetTerminating() {
g.lock.Lock()
defer g.lock.Unlock()
g.terminating = true
}
// AddRoutes add ping routes on a router
func (g Handler) AddRoutes(router *mux.Router) {
func (g *Handler) AddRoutes(router *mux.Router) {
router.Methods(http.MethodGet, http.MethodHead).Path("/ping").
HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
fmt.Fprint(response, "OK")
g.lock.RLock()
defer g.lock.RUnlock()
statusCode := http.StatusOK
if g.terminating {
statusCode = http.StatusServiceUnavailable
}
response.WriteHeader(statusCode)
fmt.Fprint(response, http.StatusText(statusCode))
})
}