diff --git a/internal/tunnel/run.go b/internal/tunnel/run.go index 6b3194d..f8d194b 100644 --- a/internal/tunnel/run.go +++ b/internal/tunnel/run.go @@ -53,7 +53,7 @@ func childEnv(home string) []string { func Start(ctx context.Context, o Options) (*Tunnel, error) { if o.Timeout == 0 { - o.Timeout = 30 * time.Second + o.Timeout = 60 * time.Second } if o.Verify == nil { o.Verify = verifyHealthz @@ -140,11 +140,12 @@ func verifyHealthz(ctx context.Context, url string) error { var publicDNS = []string{"1.1.1.1:53", "8.8.8.8:53"} +// Cada intento corta a los 5 s: si uno se cuelga mientras la dirección se propaga, el siguiente suele andar. var verifyClient = &http.Client{ - Timeout: 10 * time.Second, + Timeout: 5 * time.Second, Transport: &http.Transport{ DisableKeepAlives: true, - DialContext: (&net.Dialer{Timeout: 10 * time.Second, Resolver: &net.Resolver{ + DialContext: (&net.Dialer{Timeout: 5 * time.Second, Resolver: &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, system string) (net.Conn, error) { d := net.Dialer{Timeout: 2 * time.Second} diff --git a/internal/tunnel/run_test.go b/internal/tunnel/run_test.go index 8b39e52..f156dbd 100644 --- a/internal/tunnel/run_test.go +++ b/internal/tunnel/run_test.go @@ -3,6 +3,8 @@ package tunnel import ( "context" "errors" + "net/http" + "net/http/httptest" "os" "os/exec" "path/filepath" @@ -91,6 +93,21 @@ func TestStartVerifyFails(t *testing.T) { } } +// Una comprobación colgada no puede comerse el plazo entero: tiene que cortar rápido y reintentar. +func TestVerifyHealthzDoesNotHang(t *testing.T) { + block := make(chan struct{}) + srv := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { <-block })) + defer srv.Close() + defer close(block) + start := time.Now() + if err := verifyHealthz(context.Background(), srv.URL); err == nil { + t.Fatal("un servidor que no contesta pasó la verificación") + } + if d := time.Since(start); d > 6*time.Second { + t.Fatalf("la comprobación esperó %v; tiene que cortar antes para poder reintentar", d) + } +} + func TestChildDiesOnItsOwn(t *testing.T) { t.Setenv("FAKE_MODE", "die") tun, err := Start(context.Background(), Options{Bin: fakeBin, Port: 8080, Timeout: 5 * time.Second, Verify: okVerify})