activator: probe a waking instance's health from 5 ms, doubling to 50 ms
The fixed 150 ms gap between health probes cost a wake 75 ms on average after the app was already listening. A refused dial is cheap, so the gap now starts at 5 ms and doubles to a 50 ms ceiling.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+45
−2
internal/activator/backend_exec.go+18 −1internal/activator/backend_unit.go+3 −1internal/activator/healthpoll_test.go+24 −0
CI cancelled — superseded by a newer push — run details
diff --git a/internal/activator/backend_exec.go b/internal/activator/backend_exec.go| index 738ff0ccc..b6f250852 100644 |
| --- a/internal/activator/backend_exec.go |
| +++ b/internal/activator/backend_exec.go |
| @@ -3239,6 +3239,7 @@ func waitHealthy(socket string, timeout time.Duration) error { |
| deadline := time.Now().Add(timeout) |
| client := socketClient(socket) |
| var last error |
| + var poll time.Duration |
| for time.Now().Before(deadline) { |
| resp, err := client.Get("http://carlos/") |
| if err == nil { |
| @@ -3250,11 +3251,27 @@ func waitHealthy(socket string, timeout time.Duration) error { |
| } else { |
| last = err |
| } |
| - time.Sleep(150 * time.Millisecond) |
| + poll = nextHealthPoll(poll) |
| + time.Sleep(poll) |
| } |
| return fmt.Errorf("instance not healthy within %s: %v", timeout, last) |
| } |
| +// The wait between health probes. A request is held open for the whole of |
| +// a wake, and a fixed 150 ms gap cost it 75 ms on average after the app was |
| +// already listening. A refused dial costs microseconds, so the wait starts |
| +// short and doubles to a ceiling that keeps a slow boot from being probed |
| +// in a busy loop. |
| +const ( |
| + healthPollMin = 5 * time.Millisecond |
| + healthPollMax = 50 * time.Millisecond |
| +) |
| + |
| +// nextHealthPoll is the wait after one of d; zero gives the first. |
| +func nextHealthPoll(d time.Duration) time.Duration { |
| + return min(max(2*d, healthPollMin), healthPollMax) |
| +} |
| + |
| // waitHealthyCtx is waitHealthy bounded by ctx as well as its own timeout, so |
| // an aborted wake doesn't keep probing a socket nobody wants anymore. |
| func waitHealthyCtx(ctx context.Context, socket string, timeout time.Duration) error { |
diff --git a/internal/activator/backend_unit.go b/internal/activator/backend_unit.go| index b970b8c0b..e6b64abb3 100644 |
| --- a/internal/activator/backend_unit.go |
| +++ b/internal/activator/backend_unit.go |
| @@ -251,6 +251,7 @@ func waitHealthyAddr(addr string, timeout time.Duration) error { |
| deadline := time.Now().Add(timeout) |
| client := &http.Client{Timeout: 3 * time.Second} |
| var last error |
| + var poll time.Duration |
| for time.Now().Before(deadline) { |
| resp, err := client.Get("http://" + addr + "/") |
| if err == nil { |
| @@ -262,7 +263,8 @@ func waitHealthyAddr(addr string, timeout time.Duration) error { |
| } else { |
| last = err |
| } |
| - time.Sleep(150 * time.Millisecond) |
| + poll = nextHealthPoll(poll) |
| + time.Sleep(poll) |
| } |
| return fmt.Errorf("instance not healthy within %s: %v", timeout, last) |
| } |
diff --git a/internal/activator/healthpoll_test.go b/internal/activator/healthpoll_test.go| new file mode 100644 |
| index 000000000..0ad398ecc |
| --- /dev/null |
| +++ b/internal/activator/healthpoll_test.go |
| @@ -0,0 +1,24 @@ |
| +package activator |
| + |
| +import ( |
| + "testing" |
| + "time" |
| +) |
| + |
| +// The wait between health probes starts short, so a fast app is noticed |
| +// within milliseconds of binding, and doubles to a ceiling, so a slow one |
| +// is not probed in a busy loop. |
| +func TestHealthPollBacksOffFromShortToACeiling(t *testing.T) { |
| + var got []time.Duration |
| + d := time.Duration(0) |
| + for range 7 { |
| + d = nextHealthPoll(d) |
| + got = append(got, d) |
| + } |
| + want := []time.Duration{5, 10, 20, 40, 50, 50, 50} |
| + for i := range want { |
| + if got[i] != want[i]*time.Millisecond { |
| + t.Fatalf("delays = %v, want %v ms", got, want) |
| + } |
| + } |
| +} |