| 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 |