fix(health): stop startup wait at its timeout

The startup probe slept a full backoff step even when the deadline was closer. A 31 second timeout ended after about 41 seconds. The probe now sleeps no longer than the time left.
This commit is contained in:
2026-11-03 00:23:56 +00:00
parent ea30694dc9
commit 3e187790f2
2 changed files with 33 additions and 1 deletions
+1 -1
View File
@@ -118,7 +118,7 @@ func (bhm *BackendHealthManager) WaitForBackendReady(timeout time.Duration) erro
}
// Exponential backoff (1.5x growth, capped at maxDelay), no jitter
time.Sleep(currentDelay)
time.Sleep(min(currentDelay, time.Until(deadline)))
currentDelay = time.Duration(float64(currentDelay) * 1.5)
if currentDelay > maxDelay {
currentDelay = maxDelay
+32
View File
@@ -0,0 +1,32 @@
package main
import (
"net"
"testing"
"time"
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
"github.com/valyala/fasthttp"
)
// Pins theorems/Theorems/ReadyBackoff.lean: the startup wait never sleeps past its timeout.
func TestWaitForBackendReady_HonoursTimeout(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
addr := ln.Addr().String()
_ = ln.Close() // closed port: every probe is refused immediately
bhm := NewBackendHealthManager(&fasthttp.Client{}, "http://"+addr, "", libpack_logger.New())
defer bhm.cancel()
const timeout = 3 * time.Second
start := time.Now()
if err := bhm.WaitForBackendReady(timeout); err == nil {
t.Fatal("want error for unreachable backend")
}
if elapsed := time.Since(start); elapsed > timeout+500*time.Millisecond {
t.Fatalf("waited %v, want <= %v", elapsed, timeout+500*time.Millisecond)
}
}