Skip to content

Commit

Permalink
healthcheck is highly configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
wsalles committed Dec 2, 2021
1 parent 2c13be2 commit 21d4db6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
12 changes: 8 additions & 4 deletions pkg/common/helpers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ func ArrayContainsString(list []string, key string) bool {
return false
}

// FirstStringOfArray is a handle to get the first position of the array
func FirstStringOfArray(list []string) string {
// FirstStringOfArrayWithFallback is a handle to get the first position of the array with fallback
func FirstStringOfArrayWithFallback(list []string, fallback ...string) string {
if len(list) > 0 {
return list[0]
} else if len(fallback) > 0 {
return fallback[0]
}
return ""
}

// LastStringOfArray is a handle to get the last position of the array
func LastStringOfArray(list []string) string {
// LastStringOfArrayWithFallback is a handle to get the last position of the array with fallback
func LastStringOfArrayWithFallback(list []string, fallback ...string) string {
if len(list) > 0 {
return list[len(list)-1]
} else if len(fallback) > 0 {
return fallback[0]
}
return ""
}
29 changes: 14 additions & 15 deletions pkg/common/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (
"net/http"
"os"
"os/signal"
"sync/atomic"
"syscall"

"github.com/gin-gonic/gin"
"github.com/ydataai/go-core/pkg/common/helpers"
"github.com/ydataai/go-core/pkg/common/logging"
)

// Server defines a struct for server
type Server struct {
logger logging.Logger
Router *gin.Engine
httpServer *http.Server
configuration HTTPServerConfiguration
readyzAvailable atomic.Value
logger logging.Logger
Router *gin.Engine
httpServer *http.Server
configuration HTTPServerConfiguration
readyzFunc func() bool
}

// NewServer initializes a server
Expand Down Expand Up @@ -89,19 +89,18 @@ func (s *Server) RunSecurely(ctx context.Context) {
}

// AddHealthz creates a route to LivenessProbe
func (s *Server) AddHealthz() {
s.Router.GET(s.configuration.HealthzEndpoint, s.healthz())
func (s *Server) AddHealthz(urls ...string) {
url := helpers.FirstStringOfArrayWithFallback(urls, s.configuration.HealthzEndpoint)

s.Router.GET(url, s.healthz())
}

// AddReadyz creates a route to ReadinessProbe
func (s *Server) AddReadyz() {
func (s *Server) AddReadyz(status func() bool, urls ...string) {
// Readyz probe is negative by default
s.readyzAvailable.Store(false)
s.readyzFunc = status

s.Router.GET(s.configuration.ReadyzEndpoint, s.readyz())
}
url := helpers.FirstStringOfArrayWithFallback(urls, s.configuration.ReadyzEndpoint)

// SetReadyzState enables/disables readyz
func (s *Server) SetReadyzState(state bool) {
s.readyzAvailable.Store(state)
s.Router.GET(url, s.readyz())
}
2 changes: 1 addition & 1 deletion pkg/common/server/server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (s *Server) healthz() gin.HandlerFunc {
// readyz is a readiness probe
func (s *Server) readyz() gin.HandlerFunc {
return func(ctx *gin.Context) {
if !s.readyzAvailable.Load().(bool) {
if !s.readyzFunc() {
http.Error(ctx.Writer, http.StatusText(http.StatusServiceUnavailable), http.StatusServiceUnavailable)
return
}
Expand Down

0 comments on commit 21d4db6

Please sign in to comment.