Files
fn_registry/functions/infra/health_check_http.go
T
egutierrez 1d45f232c6 feat: funciones infra — Docker, deploy, build y health check
Funciones impuras para gestión de contenedores: docker_build_image,
docker_compose_up/down, docker_volume_create/list/remove,
generate_dockerfile, write_dockerfile, go_build_binary, health_check_http,
deploy_app y stop_app. Todas con tests unitarios donde aplica.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 14:24:12 +02:00

39 lines
1008 B
Go

package infra
import (
"fmt"
"net/http"
"time"
)
// HealthCheckHTTP hace polling HTTP GET a url hasta recibir status 200
// o hasta que pasen timeoutSecs segundos. Entre intentos espera intervalMs milisegundos.
func HealthCheckHTTP(url string, timeoutSecs, intervalMs int) error {
deadline := time.Now().Add(time.Duration(timeoutSecs) * time.Second)
interval := time.Duration(intervalMs) * time.Millisecond
client := &http.Client{
Timeout: time.Duration(intervalMs)*time.Millisecond + 500*time.Millisecond,
}
var lastErr error
for time.Now().Before(deadline) {
resp, err := client.Get(url)
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
lastErr = fmt.Errorf("status %d", resp.StatusCode)
} else {
lastErr = err
}
time.Sleep(interval)
}
if lastErr != nil {
return fmt.Errorf("health check %s timeout after %ds: %w", url, timeoutSecs, lastErr)
}
return fmt.Errorf("health check %s timeout after %ds", url, timeoutSecs)
}