|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +// DialContextFunc is a function that dials a context, network, and address. |
| 14 | +type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error) |
| 15 | + |
| 16 | +// RetryDialUntilSuccess will retry every `retryTimeout` until it succeeds. |
| 17 | +func RetryDialUntilSuccess(retryTimeout time.Duration) DialContextFunc { |
| 18 | + return func(ctx context.Context, network, address string) (net.Conn, error) { |
| 19 | + for { |
| 20 | + dialer := &net.Dialer{ |
| 21 | + Timeout: retryTimeout, |
| 22 | + KeepAlive: 30 * time.Second, // Similar to the default HTTP dialer. |
| 23 | + } |
| 24 | + c, err := dialer.DialContext(ctx, network, address) |
| 25 | + if err != nil { |
| 26 | + if errors.Is(err, context.DeadlineExceeded) { |
| 27 | + continue |
| 28 | + } |
| 29 | + if errors.Is(err, os.ErrDeadlineExceeded) { |
| 30 | + continue |
| 31 | + } |
| 32 | + // Testing hook. |
| 33 | + if testing.Testing() && strings.Contains(err.Error(), "connection refused") { |
| 34 | + continue |
| 35 | + } |
| 36 | + } |
| 37 | + return c, err |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// DialNoTimeout will block with no timeout or until the context is canceled. |
| 43 | +func DialNoTimeout() DialContextFunc { |
| 44 | + dialer := &net.Dialer{} |
| 45 | + return dialer.DialContext |
| 46 | +} |
| 47 | + |
| 48 | +// DefaultHTTPDialer has the same options as the default HTTP dialer. |
| 49 | +func DefaultHTTPDialer() DialContextFunc { |
| 50 | + dialer := &net.Dialer{ |
| 51 | + Timeout: 30 * time.Second, |
| 52 | + KeepAlive: 30 * time.Second, |
| 53 | + } |
| 54 | + return dialer.DialContext |
| 55 | +} |
| 56 | + |
| 57 | +// RacingDialer is a custom dialer that attempts to connect to a given address. |
| 58 | +// |
| 59 | +// It uses two different dialers. |
| 60 | +// The dialer connects first is returned, and the other is canceled. |
| 61 | +// |
| 62 | +// The first has a short timeout (200 ms) and continues to retry until it succeeds. |
| 63 | +// The second dialer has no timeout and will block until it either succeeds or fails. |
| 64 | +// |
| 65 | +// We are doing this because we see connection timeouts perhaps caused by some competing network routes. |
| 66 | +// Our workaround is to use a short timeout dialer that will retry until it succeeds. |
| 67 | +func RacingDialer(dialers ...DialContextFunc) DialContextFunc { |
| 68 | + if len(dialers) == 0 { |
| 69 | + return DialNoTimeout() |
| 70 | + } |
| 71 | + |
| 72 | + return func(ctx context.Context, network, address string) (net.Conn, error) { |
| 73 | + ctx, cancel := context.WithCancel(ctx) |
| 74 | + defer cancel() |
| 75 | + |
| 76 | + type dialResult struct { |
| 77 | + conn net.Conn |
| 78 | + err error |
| 79 | + } |
| 80 | + resultCh := make(chan dialResult, len(dialers)) |
| 81 | + for _, dialer := range dialers { |
| 82 | + go func(d DialContextFunc) { |
| 83 | + c, err := d(ctx, network, address) |
| 84 | + resultCh <- dialResult{conn: c, err: err} |
| 85 | + }(dialer) |
| 86 | + } |
| 87 | + |
| 88 | + var connError error |
| 89 | + for range len(dialers) { |
| 90 | + res := <-resultCh |
| 91 | + if res.err == nil { |
| 92 | + cancel() |
| 93 | + return res.conn, nil |
| 94 | + } else { |
| 95 | + connError = res.err |
| 96 | + } |
| 97 | + } |
| 98 | + return nil, connError |
| 99 | + } |
| 100 | +} |
0 commit comments