feat(http): graceful drain on SIGTERM and drain log
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -15,17 +16,28 @@ type HealthCheck func(ctx context.Context) error
|
||||
|
||||
type HealthHandler struct {
|
||||
checks []HealthCheck
|
||||
ready *atomic.Bool
|
||||
}
|
||||
|
||||
func NewHealthHandler(checks ...HealthCheck) *HealthHandler {
|
||||
return &HealthHandler{checks: checks}
|
||||
}
|
||||
|
||||
func (h *HealthHandler) WithReadyFlag(v *atomic.Bool) *HealthHandler {
|
||||
h.ready = v
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Live(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"})
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Ready(c *gin.Context) {
|
||||
if h.ready != nil && !h.ready.Load() {
|
||||
respondError(c, &domain.UnavailableError{Reason: "shutting down"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user