chore: add concurrent route check Requests
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user