zoraxy-waf/health.go

38 lines
825 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()
if resp.StatusCode < 500 {
breaker.RecordSuccess()
} else {
log.Printf("WAF health check: status %d", resp.StatusCode)
breaker.RecordFailure()
}
}
time.Sleep(interval)
}
}()
}