zoraxy-waf/health.go

40 lines
939 B
Go

package main
import (
"log"
"net/http"
"time"
)
// startHealthCheck runs a periodic health check against the WAF.
// It updates the circuit breaker based on WAF reachability.
func startHealthCheck() {
go func() {
for {
cfg := getConfig()
interval := time.Duration(cfg.HealthInterval) * time.Second
if interval < 1*time.Second {
interval = 5 * time.Second
}
client := &http.Client{Timeout: 2 * time.Second}
resp, err := client.Head(cfg.WAFURL + "/")
if err != nil {
log.Printf("WAF health check FAILED: %v", err)
breaker.RecordFailure()
} else {
resp.Body.Close()
// Any response (2xx, 3xx, 4xx) means WAF is reachable.
// Only 5xx or connection errors indicate the WAF is unhealthy.
if resp.StatusCode >= 500 {
log.Printf("WAF health check: status %d", resp.StatusCode)
breaker.RecordFailure()
} else {
breaker.RecordSuccess()
}
}
time.Sleep(interval)
}
}()
}