From 74080b7225ce0ef8156a11341c6f7e5fdb2f8759 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Thu, 13 Aug 2015 15:13:42 -0700 Subject: [PATCH] Provide yes man endpoint for inflexible load balancers Certain load balancers, such as Amazon's Elastic Load Balancer, have a very limited notion of health. While a properly configured and operational registry should always return a 401 when hitting "/v2/", such load balancers cannot be configured to treat this response code as healthy. This changeset makes "/" always return a 200 response, unless the health checks have failed. Signed-off-by: Stephen J Day --- cmd/registry/main.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/registry/main.go b/cmd/registry/main.go index 2832c0d76..ec4dedd6f 100644 --- a/cmd/registry/main.go +++ b/cmd/registry/main.go @@ -72,8 +72,9 @@ func main() { app := handlers.NewApp(ctx, *config) app.RegisterHealthChecks() handler := configureReporting(app) - handler = panicHandler(handler) + handler = alive("/", handler) handler = health.Handler(handler) + handler = panicHandler(handler) handler = gorhandlers.CombinedLoggingHandler(os.Stdout, handler) if config.HTTP.Debug.Addr != "" { @@ -313,3 +314,20 @@ func panicHandler(handler http.Handler) http.Handler { handler.ServeHTTP(w, r) }) } + +// alive simply wraps the handler with a route that always returns an http 200 +// response when the path is matched. If the path is not matched, the request +// is passed to the provided handler. There is no guarantee of anything but +// that the server is up. Wrap with other handlers (such as health.Handler) +// for greater affect. +func alive(path string, handler http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == path { + w.Header().Set("Cache-Control", "no-cache") + w.WriteHeader(http.StatusOK) + return + } + + handler.ServeHTTP(w, r) + }) +}