From 778a098bdc5d26137dec64fc19f6500719ad9af7 Mon Sep 17 00:00:00 2001 From: Jonas Kaninda Date: Thu, 31 Oct 2024 07:02:51 +0100 Subject: [PATCH] chore: add concurrent route check Requests --- internal/logger/logger.go | 2 +- pkg/handler.go | 40 ++++++++++++++++++++------------------- pkg/healthCheck.go | 3 +++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 04d4426..3fdc4fa 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -52,7 +52,7 @@ func Warn(msg string, args ...interface{}) { // Error error message func Error(msg string, args ...interface{}) { - log.SetOutput(getStd(util.GetStringEnv("GOMA_ERROR_LOG", "/dev/stdout"))) + log.SetOutput(getStd(util.GetStringEnv("GOMA_ERROR_LOG", "/dev/stderr"))) formattedMessage := fmt.Sprintf(msg, args...) if len(args) == 0 { log.Printf("ERROR: %s\n", msg) diff --git a/pkg/handler.go b/pkg/handler.go index 9ff4816..146db77 100644 --- a/pkg/handler.go +++ b/pkg/handler.go @@ -20,6 +20,7 @@ import ( "github.com/gorilla/mux" "github.com/jkaninda/goma-gateway/internal/logger" "net/http" + "sync" ) // CORSHandler handles CORS headers for incoming requests @@ -69,33 +70,34 @@ func ProxyErrorHandler(w http.ResponseWriter, r *http.Request, err error) { // HealthCheckHandler handles health check of routes func (heathRoute HealthCheckRoute) HealthCheckHandler(w http.ResponseWriter, r *http.Request) { logger.Info("%s %s %s %s", r.Method, r.RemoteAddr, r.URL, r.UserAgent()) + wg := sync.WaitGroup{} + wg.Add(len(heathRoute.Routes)) var routes []HealthCheckRouteResponse for _, route := range heathRoute.Routes { - if route.HealthCheck != "" { - err := HealthCheck(route.Destination + route.HealthCheck) - if err != nil { - logger.Error("Route %s: %v", route.Name, err) - if heathRoute.DisableRouteHealthCheckError { - routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "unhealthy", Error: "Route healthcheck errors disabled"}) - continue + go func() { + if route.HealthCheck != "" { + err := HealthCheck(route.Destination + route.HealthCheck) + 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.Info("Route %s is healthy", route.Name) + routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "healthy", Error: ""}) } - routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "unhealthy", Error: err.Error()}) - continue } else { - logger.Info("Route %s is healthy", route.Name) - routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "healthy", Error: ""}) - continue + logger.Warn("Route %s's healthCheck is undefined", route.Name) + routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "undefined", Error: ""}) } - } else { - logger.Warn("Route %s's healthCheck is undefined", route.Name) - routes = append(routes, HealthCheckRouteResponse{Name: route.Name, Status: "undefined", Error: ""}) - continue + defer wg.Done() + }() - } } + wg.Wait() // Wait for all requests to complete response := HealthCheckResponse{ - Status: "healthy", - Routes: routes, + Status: "healthy", //Goma proxy + Routes: routes, // Routes health check } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) diff --git a/pkg/healthCheck.go b/pkg/healthCheck.go index ae071ee..1a66735 100644 --- a/pkg/healthCheck.go +++ b/pkg/healthCheck.go @@ -17,6 +17,7 @@ limitations under the License. */ import ( "fmt" + "github.com/jkaninda/goma-gateway/internal/logger" "io" "net/http" "net/url" @@ -52,6 +53,7 @@ func HealthCheck(healthURL string) error { client := &http.Client{} healthResp, err := client.Do(healthReq) if err != nil { + logger.Error("Error performing HealthCheck request: %v ", err) return fmt.Errorf("error performing HealthCheck request: %v ", err) } defer func(Body io.ReadCloser) { @@ -61,6 +63,7 @@ func HealthCheck(healthURL string) error { }(healthResp.Body) if healthResp.StatusCode >= 400 { + logger.Debug("Error performing HealthCheck request: %v ", err) return fmt.Errorf("health check failed with status code %v", healthResp.StatusCode) } return nil