micro/web/health.go

108 lines
2.3 KiB
Go
Raw Permalink Normal View History

2022-04-05 11:15:32 +00:00
package web
import (
"fmt"
"net/http"
"time"
2022-04-06 10:46:02 +00:00
"github.com/go-chi/render"
2022-04-05 11:15:32 +00:00
log "scm.yoorie.de/go-lib/gelf"
)
/*
healthChecker Class
*/
type healthChecker struct {
message string
healthy bool
lastChecked time.Time
2022-04-06 10:46:02 +00:00
startTime time.Time
2022-04-05 11:15:32 +00:00
period int
ticker *time.Ticker
done chan bool
//This is the healtchcheck you will have to provide.
checkFunc func() (bool, string)
}
2022-04-06 10:46:02 +00:00
type HealthData struct {
Message string `json:"message,omitempty"`
LastChecked time.Time `json:"lastChecked"`
}
2022-04-05 11:15:32 +00:00
func newHealthChecker(checkFunction func() (bool, string)) *healthChecker {
hc := &healthChecker{}
hc.checkFunc = checkFunction
2022-04-06 10:46:02 +00:00
hc.startTime = time.Now()
2022-04-05 11:15:32 +00:00
return hc
}
func (h *healthChecker) start(checkPeriodInSeconds int) {
h.period = checkPeriodInSeconds
h.message = "service starting"
h.healthy = false
h.doCheck()
h.ticker = time.NewTicker(time.Second * time.Duration(h.period))
h.done = make(chan bool)
go func() {
for {
select {
case <-h.done:
return
case <-h.ticker.C:
h.doCheck()
}
}
}()
}
func (h *healthChecker) stop() {
h.ticker.Stop()
h.done <- true
log.Debug("Health checker stopped.")
}
2023-03-07 07:17:18 +00:00
// internal function to process the health check
2022-04-05 11:15:32 +00:00
func (h *healthChecker) doCheck() {
var msg string
h.healthy, msg = h.checkFunc()
if !h.healthy {
h.message = msg
} else {
h.message = ""
}
h.lastChecked = time.Now()
}
/*
GetHealthyEndpoint is this service healthy
*/
2022-04-06 10:46:02 +00:00
func (h *healthChecker) healthyEndpoint(responseWriter http.ResponseWriter, request *http.Request) {
2022-04-05 11:15:32 +00:00
t := time.Now()
2022-04-06 10:46:02 +00:00
status := http.StatusOK
healtData := &HealthData{
Message: "service up and running",
LastChecked: h.lastChecked,
}
2022-04-05 11:15:32 +00:00
if t.Sub(h.lastChecked) > (time.Second * time.Duration(2*h.period)) {
h.healthy = false
h.message = "Healthcheck not running"
}
2022-04-06 10:46:02 +00:00
if !h.healthy {
status = http.StatusServiceUnavailable
healtData.Message = fmt.Sprintf("service is unavailable: %s", h.message)
2022-04-05 11:15:32 +00:00
}
2022-04-06 10:46:02 +00:00
responseWriter.WriteHeader(status)
render.JSON(responseWriter, request, healtData)
2022-04-05 11:15:32 +00:00
}
/*
GetReadinessEndpoint is this service ready for taking requests
*/
2022-04-06 10:46:02 +00:00
func (h *healthChecker) readinessEndpoint(responseWriter http.ResponseWriter, request *http.Request) {
render.JSON(responseWriter, request, &HealthData{
Message: "service is ready",
LastChecked: h.startTime,
})
2022-04-05 11:15:32 +00:00
}