refactor: improve route healthcheck

This commit is contained in:
Jonas Kaninda
2024-11-12 12:54:42 +01:00
parent e541d0066d
commit a99c40940e
2 changed files with 30 additions and 17 deletions

View File

@@ -27,12 +27,6 @@ import (
"time" "time"
) )
type Health struct {
URL string
TimeOut time.Duration
HealthyStatuses []int
}
func (health Health) Check() error { func (health Health) Check() error {
healthCheckURL, err := url.Parse(health.URL) healthCheckURL, err := url.Parse(health.URL)
if err != nil { if err != nil {
@@ -70,8 +64,8 @@ func (health Health) Check() error {
} }
func routesHealthCheck(routes []Route) { func routesHealthCheck(routes []Route) {
for _, route := range routes { for _, route := range routes {
go func() { if len(route.HealthCheck.Path) > 0 {
if len(route.HealthCheck.Path) > 0 { go func() {
interval := "30s" interval := "30s"
timeout, _ := util.ParseDuration("") timeout, _ := util.ParseDuration("")
if len(route.HealthCheck.Interval) > 0 { if len(route.HealthCheck.Interval) > 0 {
@@ -92,29 +86,40 @@ func routesHealthCheck(routes []Route) {
timeout = d1 timeout = d1
} }
if len(route.Backends) > 0 { if n := len(route.Backends); len(route.Backends) > 0 {
for index, backend := range route.Backends { for index, backend := range route.Backends {
err := createCron(fmt.Sprintf("%s [%d]", route.Name, index), expression, backend+route.HealthCheck.Path, timeout, route.HealthCheck.HealthyStatuses) if n > 1 {
if err != nil { go func() {
logger.Error("Error creating cron expression: %v ", err) err := createHealthCheckJob(fmt.Sprintf("%s [%d]", route.Name, index), expression, backend+route.HealthCheck.Path, timeout, route.HealthCheck.HealthyStatuses)
return 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 { } else {
err := createCron(route.Name, expression, route.Destination+route.HealthCheck.Path, timeout, route.HealthCheck.HealthyStatuses) err := createHealthCheckJob(route.Name, expression, route.Destination+route.HealthCheck.Path, timeout, route.HealthCheck.HealthyStatuses)
if err != nil { if err != nil {
logger.Error("Error creating cron expression: %v ", err) logger.Error("Error creating cron expression: %v ", err)
return return
} }
} }
} }()
}() }
} }
} }
func createCron(name, expression string, healthURL string, timeout time.Duration, healthyStatuses []int) error { func createHealthCheckJob(name, expression string, healthURL string, timeout time.Duration, healthyStatuses []int) error {
// Create a new cron instance // Create a new cron instance
c := cron.New() c := cron.New()

View File

@@ -20,6 +20,7 @@ package pkg
import ( import (
"context" "context"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"time"
) )
type Config struct { type Config struct {
@@ -275,3 +276,10 @@ type JWTSecret struct {
ISS string `yaml:"iss"` ISS string `yaml:"iss"`
Secret string `yaml:"secret"` Secret string `yaml:"secret"`
} }
// Health represents the health check content for a route
type Health struct {
URL string
TimeOut time.Duration
HealthyStatuses []int
}