Health Checks del Proxy
Los sondeos TCP periódicos detectan upstreams no saludables. Después de N fallos consecutivos (umbral), el upstream se marca como no saludable y el tráfico se enruta a otro lado. Al recuperarse, se rehabilita automáticamente.
Código
// nyx-proxy health checks — TCP probe upstreams periodically
// Health checks nyx-proxy — probe TCP de upstreams
var failures: int = 0
var threshold: int = 3
var healthy: bool = true
// Try to connect to the upstream — returns true if reachable
fn probe_upstream(host: String, port: int) -> bool {
let fd: int = tcp_connect(host, port)
if fd < 0 { return false }
tcp_close(fd)
return true
}
fn check_loop(host: String, port: int) -> int {
// A typical health check loop (runs in a separate thread)
var iterations: int = 0
while iterations < 3 {
let ok: bool = probe_upstream(host, port)
if ok {
if not healthy {
print("upstream RECOVERED: " + host + ":" + int_to_string(port))
healthy = true
}
failures = 0
} else {
failures = failures + 1
print("probe failed (" + int_to_string(failures) + "/" + int_to_string(threshold) + ")")
if failures >= threshold and healthy {
print("upstream UNHEALTHY: " + host + ":" + int_to_string(port))
healthy = false
}
}
sleep(1000)
iterations = iterations + 1
}
return 0
}
fn main() -> int {
// Check a local upstream (likely fails if nothing listening on 3001)
check_loop("127.0.0.1", 3001)
print("final state: healthy=" + int_to_string(healthy))
return 0
}
Salida
probe failed (1/3) probe failed (2/3) probe failed (3/3) upstream UNHEALTHY: 127.0.0.1:3001 final state: healthy=0
Explicación
Un proxy es tan confiable como el backend más débil en el que todavía confía. Los health checks separan "¿este upstream es alcanzable?" de "¿hay un usuario esperando por él ahora mismo?" mediante sondeos asíncronos — un hilo dedicado abre una conexión TCP cada N segundos y actualiza una bandera de salud compartida. El umbral (aquí 3) evita el flapping: un único fallo de red no debería sacar a un backend de la rotación, pero tres seguidos probablemente signifiquen que algo realmente está mal. La recuperación es simétrica — un sondeo exitoso vuelve a poner healthy en true — así que los reinicios se autorreparan sin intervención del operador. En producción, nyx-proxy corre uno de estos loops por cada upstream y el forwarder consulta el mapa de salud en cada request.