Skip to content

Commit

Permalink
add unhealthy hook
Browse files Browse the repository at this point in the history
  • Loading branch information
rpatali committed Feb 8, 2021
1 parent fea477f commit 1714d23
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions runtime/gateway.go
Expand Up @@ -127,6 +127,7 @@ type Gateway struct {
tracerCloser io.Closer

requestUUIDHeaderKey string
isUnhealthy bool
}

// DefaultDependencies are the common dependencies for all modules
Expand Down Expand Up @@ -378,6 +379,14 @@ func (gateway *Gateway) handleHealthRequest(
req *ServerHTTPRequest,
res *ServerHTTPResponse,
) {
if gateway.isUnhealthy {
message := "Unhealthy, from " + gateway.ServiceName
bytes := []byte(
"{\"ok\":false,\"message\":\"" + message + "\"}\n",
)
res.WriteJSONBytes(503, nil, bytes)
return
}
message := "Healthy, from " + gateway.ServiceName
bytes := []byte(
"{\"ok\":true,\"message\":\"" + message + "\"}\n",
Expand All @@ -388,6 +397,9 @@ func (gateway *Gateway) handleHealthRequest(

// Shutdown starts the graceful shutdown, blocks until it is complete
func (gateway *Gateway) Shutdown() {
// stop accepting incoming requests as soon as shutdown signal is received.
gateway.isUnhealthy = true

var swg sync.WaitGroup
ctx, cancel := context.WithTimeout(context.Background(), gateway.ShutdownTimeout())
defer cancel()
Expand Down

0 comments on commit 1714d23

Please sign in to comment.