refactor: improve route healthcheck

This commit is contained in:
Jonas Kaninda
2024-11-12 14:31:18 +01:00
parent 3c7a55c5e4
commit 9eadd08a1f
5 changed files with 83 additions and 99 deletions

View File

@@ -20,7 +20,6 @@ import (
"encoding/json" "encoding/json"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/jkaninda/goma-gateway/pkg/logger" "github.com/jkaninda/goma-gateway/pkg/logger"
"github.com/jkaninda/goma-gateway/util"
"net/http" "net/http"
"sync" "sync"
) )
@@ -70,30 +69,20 @@ func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(heathRoute.Routes)) wg.Add(len(heathRoute.Routes))
var routes []HealthCheckRouteResponse var routes []HealthCheckRouteResponse
for _, route := range heathRoute.Routes { for _, health := range healthCheckRoutes(heathRoute.Routes) {
go func() { go func() {
defer wg.Done() defer wg.Done()
if route.HealthCheck.Path != "" { err := health.Check()
timeout, _ := util.ParseDuration(route.HealthCheck.Timeout) if err != nil {
health := Health{ if heathRoute.DisableRouteHealthCheckError {
URL: route.Destination + route.HealthCheck.Path, routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "unhealthy", Error: "Route healthcheck errors disabled"})
TimeOut: timeout,
HealthyStatuses: route.HealthCheck.HealthyStatuses,
}
err := health.Check()
if err != nil {
if heathRoute.DisableRouteHealthCheckError {
routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "unhealthy", Error: "Route healthcheck errors disabled"})
}
routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "unhealthy", Error: "Error: " + err.Error()})
} else {
logger.Debug("Route %s is healthy", route.Name)
routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "healthy", Error: ""})
} }
routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "unhealthy", Error: "Error: " + err.Error()})
} else { } else {
logger.Debug("Route %s's healthCheck is undefined", route.Name) logger.Debug("Route %s is healthy", health.Name)
routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "undefined", Error: ""}) routes = append(routes, HealthCheckRouteResponse{Name: health.Name, Status: "healthy", Error: ""})
} }
}() }()
} }

View File

@@ -24,7 +24,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"slices" "slices"
"time"
) )
func (health Health) Check() error { func (health Health) Check() error {
@@ -35,14 +34,14 @@ func (health Health) Check() error {
// Create a new request for the route // Create a new request for the route
healthReq, err := http.NewRequest("GET", healthCheckURL.String(), nil) healthReq, err := http.NewRequest("GET", healthCheckURL.String(), nil)
if err != nil { if err != nil {
return fmt.Errorf("error creating HealthCheck request: %v ", err) return fmt.Errorf("error route %s: creating HealthCheck request: %v ", health.Name, err)
} }
// Perform the request to the route's healthcheck // Perform the request to the route's healthcheck
client := &http.Client{Timeout: health.TimeOut} client := &http.Client{Timeout: health.TimeOut}
healthResp, err := client.Do(healthReq) healthResp, err := client.Do(healthReq)
if err != nil { if err != nil {
logger.Error("Error performing HealthCheck request: %v ", err) logger.Error("Error route %s: performing HealthCheck request: %v ", health.Name, err)
return fmt.Errorf("error performing HealthCheck request: %v ", err) return fmt.Errorf("Error route %s: performing HealthCheck request: %v ", health.Name, err)
} }
defer func(Body io.ReadCloser) { defer func(Body io.ReadCloser) {
err := Body.Close() err := Body.Close()
@@ -51,90 +50,50 @@ func (health Health) Check() error {
}(healthResp.Body) }(healthResp.Body)
if len(health.HealthyStatuses) > 0 { if len(health.HealthyStatuses) > 0 {
if !slices.Contains(health.HealthyStatuses, healthResp.StatusCode) { if !slices.Contains(health.HealthyStatuses, healthResp.StatusCode) {
logger.Error("Error: health check failed with status code %d", healthResp.StatusCode) logger.Error("Error: Route %s: health check failed with status code %d", health.Name, healthResp.StatusCode)
return fmt.Errorf("health check failed with status code %v", healthResp.StatusCode) return fmt.Errorf("route %s health check failed with status code %d", health.Name, healthResp.StatusCode)
} }
} else { } else {
if healthResp.StatusCode >= 400 { if healthResp.StatusCode >= 400 {
logger.Error("Error: health check failed with status code %d", healthResp.StatusCode) logger.Error("Error: Route %s: health check failed with status code %d", health.Name, healthResp.StatusCode)
return fmt.Errorf("health check failed with status code %v", healthResp.StatusCode) return fmt.Errorf("route %s: health check failed with status code %d", health.Name, healthResp.StatusCode)
} }
} }
return nil return nil
} }
func routesHealthCheck(routes []Route) { func routesHealthCheck(routes []Route) {
for _, route := range routes { for _, health := range healthCheckRoutes(routes) {
if len(route.HealthCheck.Path) > 0 { go func() {
go func() { err := health.createHealthCheckJob()
interval := "30s" if err != nil {
timeout, _ := util.ParseDuration("") logger.Error("Error creating healthcheck job: %v ", err)
if len(route.HealthCheck.Interval) > 0 { return
interval = route.HealthCheck.Interval }
}
expression := fmt.Sprintf("@every %s", interval)
if !util.IsValidCronExpression(expression) {
logger.Error("Health check interval is invalid: %s", interval)
logger.Info("Route health check ignored")
return
}
if len(route.HealthCheck.Timeout) > 0 {
d1, err1 := util.ParseDuration(route.HealthCheck.Timeout)
if err1 != nil {
logger.Error("Health check timeout is invalid: %s", route.HealthCheck.Timeout)
return
}
timeout = d1
} }()
if n := len(route.Backends); len(route.Backends) > 0 {
for index, backend := range route.Backends {
if n > 1 {
go func() {
err := createHealthCheckJob(fmt.Sprintf("%s [%d]", route.Name, index), expression, backend+route.HealthCheck.Path, timeout, route.HealthCheck.HealthyStatuses)
if err != nil {
logger.Error("Error creating healthcheck job: %v ", err)
return
}
}()
} else {
err := createHealthCheckJob(fmt.Sprintf("%s [%d]", route.Name, index), expression, backend+route.HealthCheck.Path, timeout, route.HealthCheck.HealthyStatuses)
if err != nil {
logger.Error("Error creating healthcheck job: %v ", err)
return
}
}
}
} else {
err := createHealthCheckJob(route.Name, expression, route.Destination+route.HealthCheck.Path, timeout, route.HealthCheck.HealthyStatuses)
if err != nil {
logger.Error("Error creating cron expression: %v ", err)
return
}
}
}()
}
} }
} }
func createHealthCheckJob(name, expression string, healthURL string, timeout time.Duration, healthyStatuses []int) error { func (health Health) createHealthCheckJob() error {
interval := "30s"
if len(health.Interval) > 0 {
interval = health.Interval
}
expression := fmt.Sprintf("@every %s", interval)
if !util.IsValidCronExpression(expression) {
logger.Error("Health check interval is invalid: %s", interval)
logger.Info("Route health check ignored")
return fmt.Errorf("health check interval is invalid: %s", interval)
}
// Create a new cron instance // Create a new cron instance
c := cron.New() c := cron.New()
_, err := c.AddFunc(expression, func() { _, err := c.AddFunc(expression, func() {
health := Health{
URL: healthURL,
TimeOut: timeout,
HealthyStatuses: healthyStatuses,
}
err := health.Check() err := health.Check()
if err != nil { if err != nil {
logger.Error("Route %s is unhealthy: error %v", name, err.Error()) logger.Error("Route %s is unhealthy: error %v", health.Name, err.Error())
return return
} }
logger.Info("Route %s is healthy", name) logger.Info("Route %s is healthy", health.Name)
}) })
if err != nil { if err != nil {
return err return err
@@ -144,10 +103,3 @@ func createHealthCheckJob(name, expression string, healthURL string, timeout tim
defer c.Stop() defer c.Stop()
select {} select {}
} }
type HealthCheck struct {
url string
interval string
timeout string
healthyStatuses []int
}

View File

@@ -17,6 +17,7 @@ import (
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/table"
"github.com/jkaninda/goma-gateway/pkg/logger" "github.com/jkaninda/goma-gateway/pkg/logger"
"github.com/jkaninda/goma-gateway/util"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"net/http" "net/http"
"time" "time"
@@ -100,3 +101,43 @@ func createJWT(email, jwtSecret string) (string, error) {
return signedToken, nil return signedToken, nil
} }
// healthCheckRoutes creates []Health
func healthCheckRoutes(routes []Route) []Health {
var healthRoutes []Health
for _, route := range routes {
if len(route.HealthCheck.Path) > 0 {
timeout, _ := util.ParseDuration("")
if len(route.HealthCheck.Timeout) > 0 {
d1, err1 := util.ParseDuration(route.HealthCheck.Timeout)
if err1 != nil {
logger.Error("Health check timeout is invalid: %s", route.HealthCheck.Timeout)
}
timeout = d1
}
if len(route.Backends) > 0 {
for index, backend := range route.Backends {
health := Health{
Name: fmt.Sprintf("%s - [%d]", route.Name, index),
URL: backend + route.HealthCheck.Path,
TimeOut: timeout,
HealthyStatuses: route.HealthCheck.HealthyStatuses,
}
healthRoutes = append(healthRoutes, health)
}
} else {
health := Health{
Name: route.Name,
URL: route.Destination + route.HealthCheck.Path,
TimeOut: timeout,
HealthyStatuses: route.HealthCheck.HealthyStatuses,
}
healthRoutes = append(healthRoutes, health)
}
} else {
logger.Debug("Route %s's healthCheck is undefined", route.Name)
}
}
return healthRoutes
}

View File

@@ -147,11 +147,11 @@ type Route struct {
// //
// Methods allowed method // Methods allowed method
Methods []string `yaml:"methods"` Methods []string `yaml:"methods"`
// HealthCheck Defines the backend is health
HealthCheck RouteHealthCheck `yaml:"healthCheck"`
// Destination Defines backend URL // Destination Defines backend URL
Destination string `yaml:"destination"` Destination string `yaml:"destination"`
Backends []string `yaml:"backends"` Backends []string `yaml:"backends"`
// HealthCheck Defines the backend is health
HealthCheck RouteHealthCheck `yaml:"healthCheck"`
// Cors contains the route cors headers // Cors contains the route cors headers
Cors Cors `yaml:"cors"` Cors Cors `yaml:"cors"`
RateLimit int `yaml:"rateLimit"` RateLimit int `yaml:"rateLimit"`
@@ -279,7 +279,9 @@ type JWTSecret struct {
// Health represents the health check content for a route // Health represents the health check content for a route
type Health struct { type Health struct {
Name string
URL string URL string
TimeOut time.Duration TimeOut time.Duration
Interval string
HealthyStatuses []int HealthyStatuses []int
} }

View File

@@ -12,4 +12,4 @@ const OAuth = "oauth" // OAuth authentication middleware
// Round-robin counter // Round-robin counter
var counter uint32 var counter uint32
var routes *[]Route var Routes *[]Route