From 0a27704b58bb811a609be573ba1aaf8ac9c74cd7 Mon Sep 17 00:00:00 2001 From: Jose Nino Date: Fri, 25 Oct 2019 15:57:55 -0700 Subject: [PATCH] healthcheck: allow customizable healthcheck name Signed-off-by: Jose Nino --- src/server/health.go | 10 ++++++---- src/server/server_impl.go | 6 +++--- test/server/health_test.go | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/server/health.go b/src/server/health.go index 8e3d6ff2..b00084ee 100644 --- a/src/server/health.go +++ b/src/server/health.go @@ -14,14 +14,16 @@ import ( type healthChecker struct { grpc *health.Server ok uint32 + name string } -func NewHealthChecker(grpcHealthServer *health.Server) *healthChecker { +func NewHealthChecker(grpcHealthServer *health.Server, name string) *healthChecker { ret := &healthChecker{} ret.ok = 1 + ret.name = name ret.grpc = grpcHealthServer - ret.grpc.SetServingStatus("ratelimit", healthpb.HealthCheckResponse_SERVING) + ret.grpc.SetServingStatus(ret.name, healthpb.HealthCheckResponse_SERVING) sigterm := make(chan os.Signal, 1) signal.Notify(sigterm, syscall.SIGTERM) @@ -29,7 +31,7 @@ func NewHealthChecker(grpcHealthServer *health.Server) *healthChecker { go func() { <-sigterm atomic.StoreUint32(&ret.ok, 0) - ret.grpc.SetServingStatus("ratelimit", healthpb.HealthCheckResponse_NOT_SERVING) + ret.grpc.SetServingStatus(ret.name, healthpb.HealthCheckResponse_NOT_SERVING) }() return ret @@ -46,5 +48,5 @@ func (hc *healthChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (hc *healthChecker) Fail() { atomic.StoreUint32(&hc.ok, 0) - hc.grpc.SetServingStatus("ratelimit", healthpb.HealthCheckResponse_NOT_SERVING) + hc.grpc.SetServingStatus(hc.name, healthpb.HealthCheckResponse_NOT_SERVING) } diff --git a/src/server/server_impl.go b/src/server/server_impl.go index 6f067c84..d18ba83e 100644 --- a/src/server/server_impl.go +++ b/src/server/server_impl.go @@ -15,9 +15,9 @@ import ( "net" "github.com/gorilla/mux" - "github.com/kavu/go_reuseport" + reuseport "github.com/kavu/go_reuseport" "github.com/lyft/goruntime/loader" - "github.com/lyft/gostats" + stats "github.com/lyft/gostats" "github.com/lyft/ratelimit/src/settings" logger "github.com/sirupsen/logrus" "google.golang.org/grpc" @@ -142,7 +142,7 @@ func newServer(name string, opts ...settings.Option) *server { ret.router = mux.NewRouter() // setup healthcheck path - ret.health = NewHealthChecker(health.NewServer()) + ret.health = NewHealthChecker(health.NewServer(), "ratelimit") ret.router.Path("/healthcheck").Handler(ret.health) healthpb.RegisterHealthServer(ret.grpcServer, ret.health.grpc) diff --git a/test/server/health_test.go b/test/server/health_test.go index f8f0d76f..21030ddd 100644 --- a/test/server/health_test.go +++ b/test/server/health_test.go @@ -19,7 +19,7 @@ func TestHealthCheck(t *testing.T) { recorder := httptest.NewRecorder() - hc := server.NewHealthChecker(health.NewServer()) + hc := server.NewHealthChecker(health.NewServer(), "ratelimit") r, _ := http.NewRequest("GET", "http://1.2.3.4/healthcheck", nil) hc.ServeHTTP(recorder, r) @@ -49,7 +49,7 @@ func TestGrpcHealthCheck(t *testing.T) { defer signal.Reset(syscall.SIGTERM) grpcHealthServer := health.NewServer() - hc := server.NewHealthChecker(grpcHealthServer) + hc := server.NewHealthChecker(grpcHealthServer, "ratelimit") healthpb.RegisterHealthServer(grpc.NewServer(), grpcHealthServer) req := &healthpb.HealthCheckRequest{