Skip to content

Commit

Permalink
health: make a few more types public (#104)
Browse files Browse the repository at this point in the history
Description: #102 allowed for some customization. This PR makes the types public so that other servers can use this implementation.

Signed-off-by: Jose Nino <[email protected]>
  • Loading branch information
junr03 authored Oct 29, 2019
1 parent 3a85594 commit 8d92350
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/server/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import (
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)

type healthChecker struct {
type HealthChecker struct {
grpc *health.Server
ok uint32
name string
}

func NewHealthChecker(grpcHealthServer *health.Server, name string) *healthChecker {
ret := &healthChecker{}
func NewHealthChecker(grpcHealthServer *health.Server, name string) *HealthChecker {
ret := &HealthChecker{}
ret.ok = 1
ret.name = name

Expand All @@ -37,7 +37,7 @@ func NewHealthChecker(grpcHealthServer *health.Server, name string) *healthCheck
return ret
}

func (hc *healthChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (hc *HealthChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ok := atomic.LoadUint32(&hc.ok)
if ok == 1 {
w.Write([]byte("OK"))
Expand All @@ -46,7 +46,11 @@ func (hc *healthChecker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

func (hc *healthChecker) Fail() {
func (hc *HealthChecker) Fail() {
atomic.StoreUint32(&hc.ok, 0)
hc.grpc.SetServingStatus(hc.name, healthpb.HealthCheckResponse_NOT_SERVING)
}

func (hc *HealthChecker) Server() *health.Server {
return hc.grpc
}
4 changes: 2 additions & 2 deletions src/server/server_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type server struct {
scope stats.Scope
runtime loader.IFace
debugListener serverDebugListener
health *healthChecker
health *HealthChecker
}

func (server *server) AddDebugHttpEndpoint(path string, help string, handler http.HandlerFunc) {
Expand Down Expand Up @@ -144,7 +144,7 @@ func newServer(name string, opts ...settings.Option) *server {
// setup healthcheck path
ret.health = NewHealthChecker(health.NewServer(), "ratelimit")
ret.router.Path("/healthcheck").Handler(ret.health)
healthpb.RegisterHealthServer(ret.grpcServer, ret.health.grpc)
healthpb.RegisterHealthServer(ret.grpcServer, ret.health.Server())

// setup default debug listener
ret.debugListener.debugMux = http.NewServeMux()
Expand Down

0 comments on commit 8d92350

Please sign in to comment.