carlos / platform Public

edge-roll-blip: unix dials share the local hold loop and window

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev af1172ddac710dfe287ce2c325fdfc0917e1907c parent 8152875
3 files changed, +80 −31
  • internal/edge/edge.go +1 −1
  • internal/edge/genproxy.go +40 −30
  • internal/edge/genproxy_test.go +39 −0
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 2c0e65445..9a11f73b8 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -3528,7 +3528,7 @@ func backhaulProxy(host, eip, box, auth, doorAuth string, assetFill bool, tr htt
// dialUnixWithRetry masks the brief gap during an instance restart: while the
// old process has exited and the new one hasn't rebound the socket, dials fail
-// with ENOENT/ECONNREFUSED. Retry those for a short window so no in-flight
+// with ENOENT/ECONNREFUSED. Retry those for localDialHold so no in-flight
// request is dropped; any other error fails fast. The window must exceed a
// deploy's socket-absent gap — the instance drain (10s) plus rebind — so an
// in-flight request is never 502'd mid-deploy; a sustained-down backend hangs
diff --git a/internal/edge/genproxy.go b/internal/edge/genproxy.go
index f876386b2..3c2779c9b 100644
--- a/internal/edge/genproxy.go
+++ b/internal/edge/genproxy.go
@@ -37,30 +37,19 @@ var errNoGeneration = fmt.Errorf("edge: instance has no current generation: %w",
// resolve is asked before the first dial and again before every retry, so a
// dial that races a switch lands on the generation that is current by then.
func dialUnixRetrying(ctx context.Context, d *net.Dialer, resolve func() (string, bool)) (net.Conn, error) {
- const window = 13 * time.Second
- deadline := time.Now().Add(window)
- for {
- var (
- conn net.Conn
- err error
- )
- if sock, ok := resolve(); ok {
- conn, err = unixdial.Checked(ctx, d, sock)
- } else {
- err = errNoGeneration
- }
- if err == nil {
- return conn, nil
- }
- if ctx.Err() != nil || !isRetryableDialErr(err) || time.Now().After(deadline) {
- return nil, err
- }
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- case <-time.After(100 * time.Millisecond):
+ return dialUnixUntil(ctx, time.Now().Add(localDialHold), d, resolve)
+}
+
+// dialUnixUntil is dialUnixRetrying with the hold's deadline given, so a
+// test can shorten it without a package global.
+func dialUnixUntil(ctx context.Context, deadline time.Time, d *net.Dialer, resolve func() (string, bool)) (net.Conn, error) {
+ return retryLocalDial(ctx, deadline, func(actx context.Context) (net.Conn, error) {
+ sock, ok := resolve()
+ if !ok {
+ return nil, errNoGeneration
}
- }
+ return unixdial.Checked(actx, d, sock)
+ })
}
// genMovedError is a generation pool's dial giving up: its own socket is
@@ -83,10 +72,19 @@ func (e *genMovedError) Error() string {
// current or no generation is; once another one is current, own is gone for
// good and the dial returns genMovedError.
func dialGen(ctx context.Context, d *net.Dialer, src GenSource, host string, own GenInfo) (net.Conn, error) {
- const window = 13 * time.Second
- deadline := time.Now().Add(window)
+ return dialGenUntil(ctx, time.Now().Add(localDialHold), d, src, host, own)
+}
+
+// dialGenUntil is dialGen with the hold's deadline given. Like
+// retryLocalDial, each attempt runs on a context that ends at the deadline
+// and each wait is min(100ms, time left); an attempt the deadline cut
+// short gives errHoldExpired.
+func dialGenUntil(ctx context.Context, deadline time.Time, d *net.Dialer, src GenSource, host string, own GenInfo) (net.Conn, error) {
for {
- conn, err := unixdial.Checked(ctx, d, own.Socket)
+ actx, cancel := context.WithDeadline(ctx, deadline)
+ conn, err := unixdial.Checked(actx, d, own.Socket)
+ cut := actx.Err() != nil // read before cancel, which would also set it
+ cancel()
if err == nil {
// Spec §7.2 (rev 17): if the generation stops being current
// before the dial completes, the request is retried against the
@@ -105,8 +103,17 @@ func dialGen(ctx context.Context, d *net.Dialer, src GenSource, host string, own
return nil, &genMovedError{to: cur}
}
err = errNoGeneration
+ cut = false
}
- if ctx.Err() != nil || !isRetryableDialErr(err) || time.Now().After(deadline) {
+ if ctx.Err() != nil {
+ return nil, err
+ }
+ retryable := isRetryableDialErr(err)
+ if cut && !retryable {
+ return nil, errHoldExpired
+ }
+ left := time.Until(deadline)
+ if !retryable || left <= 0 {
return nil, err
}
if cur, ok := src.ServingGen(host); ok && (cur.N != own.N || cur.Socket != own.Socket) {
@@ -115,7 +122,10 @@ func dialGen(ctx context.Context, d *net.Dialer, src GenSource, host string, own
select {
case <-ctx.Done():
return nil, ctx.Err()
- case <-time.After(100 * time.Millisecond):
+ case <-time.After(min(localDialStep, left)):
+ }
+ if !time.Now().Before(deadline) {
+ return nil, err
}
}
}
@@ -306,10 +316,10 @@ func (t *genRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
}
}
-// waitServingGen waits, within the dial's 13 s retry window and ctx, for
+// waitServingGen waits, within the dial's localDialHold window and ctx, for
// host to have a current generation, asking every 100 ms.
func waitServingGen(ctx context.Context, src GenSource, host string) (GenInfo, bool) {
- deadline := time.Now().Add(13 * time.Second)
+ deadline := time.Now().Add(localDialHold)
for {
if g, ok := src.ServingGen(host); ok {
return g, true
diff --git a/internal/edge/genproxy_test.go b/internal/edge/genproxy_test.go
index 35ec93dbf..3157e7ccc 100644
--- a/internal/edge/genproxy_test.go
+++ b/internal/edge/genproxy_test.go
@@ -506,3 +506,42 @@ func TestGenRoundTripperGivesUpWhenEveryGenerationIsStale(t *testing.T) {
t.Fatalf("body closed %d times, want 1", got)
}
}
+
+// blockingDialer is a Dialer whose connect never completes: its control
+// hook parks until the attempt's context ends, as a stuck dial would.
+func blockingDialer() *net.Dialer {
+ return &net.Dialer{ControlContext: func(ctx context.Context, _, _ string, _ syscall.RawConn) error {
+ <-ctx.Done()
+ return ctx.Err()
+ }}
+}
+
+// An attempt is bounded by the one hold deadline: a dial that blocks gives
+// errHoldExpired at the deadline instead of outliving the hold.
+func TestDialUnixRetryingAttemptEndsAtTheDeadline(t *testing.T) {
+ dir := genSockDir(t)
+ resolve := func() (string, bool) { return filepath.Join(dir, "x.sock"), true }
+ start := time.Now()
+ _, err := dialUnixUntil(context.Background(), start.Add(300*time.Millisecond), blockingDialer(), resolve)
+ if !errors.Is(err, errHoldExpired) || !errors.Is(err, context.DeadlineExceeded) {
+ t.Fatalf("err = %v, want errHoldExpired wrapping DeadlineExceeded", err)
+ }
+ if took := time.Since(start); took < 250*time.Millisecond || took > 2*time.Second {
+ t.Fatalf("returned after %v, want about 300ms", took)
+ }
+}
+
+func TestDialGenAttemptEndsAtTheDeadline(t *testing.T) {
+ dir := genSockDir(t)
+ own := GenInfo{Socket: filepath.Join(dir, "x.sock"), N: 1}
+ src := newFakeGens(true)
+ src.switchTo("inst.example.com", own)
+ start := time.Now()
+ _, err := dialGenUntil(context.Background(), start.Add(300*time.Millisecond), blockingDialer(), src, "inst.example.com", own)
+ if !errors.Is(err, errHoldExpired) || !errors.Is(err, context.DeadlineExceeded) {
+ t.Fatalf("err = %v, want errHoldExpired wrapping DeadlineExceeded", err)
+ }
+ if took := time.Since(start); took < 250*time.Millisecond || took > 2*time.Second {
+ t.Fatalf("returned after %v, want about 300ms", took)
+ }
+}