github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/api/health_check_handler.go (about)

     1  //  Copyright (c) 2017-2018 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package api
    16  
    17  import (
    18  	"github.com/uber/aresdb/utils"
    19  	"io"
    20  	"net/http"
    21  	"sync"
    22  )
    23  
    24  // HealthCheckHandler http handler for health check.
    25  type HealthCheckHandler struct {
    26  	sync.RWMutex
    27  	// This flag controls whether returns 200 health or 503 service unavaible in health check handler.
    28  	// Useful when server is lagging behind too much so developper manually call an API in debug handler
    29  	// to disable the health check.
    30  	disable bool
    31  }
    32  
    33  // NewHealthCheckHandler return a new http handler for health check.
    34  func NewHealthCheckHandler() *HealthCheckHandler {
    35  	return &HealthCheckHandler{}
    36  }
    37  
    38  // HealthCheck is the HealthCheck endpoint.
    39  func (handler *HealthCheckHandler) HealthCheck(w http.ResponseWriter, r *http.Request) {
    40  	handler.RLock()
    41  	disabled := handler.disable
    42  	handler.RUnlock()
    43  	if disabled {
    44  		RespondBytesWithCode(w, http.StatusServiceUnavailable, []byte("Health check disabled"))
    45  	} else {
    46  		io.WriteString(w, "OK")
    47  	}
    48  }
    49  
    50  // Version is the Version check endpoint.
    51  func (handler *HealthCheckHandler) Version(w http.ResponseWriter, r *http.Request) {
    52  	io.WriteString(w, utils.GetConfig().Version)
    53  }