-
Notifications
You must be signed in to change notification settings - Fork 11
feat: use racing, retrying dialer when attempting to connect #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
goller
wants to merge
1
commit into
main
Choose a base branch
from
feat/racing-dialer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // DialContextFunc is a function that dials a context, network, and address. | ||
| type DialContextFunc func(ctx context.Context, network, address string) (net.Conn, error) | ||
|
|
||
| // RetryDialUntilSuccess will retry every `retryTimeout` until it succeeds. | ||
| func RetryDialUntilSuccess(retryTimeout time.Duration) DialContextFunc { | ||
| return func(ctx context.Context, network, address string) (net.Conn, error) { | ||
| for { | ||
| dialer := &net.Dialer{ | ||
| Timeout: retryTimeout, | ||
| KeepAlive: 30 * time.Second, // Similar to the default HTTP dialer. | ||
| } | ||
| c, err := dialer.DialContext(ctx, network, address) | ||
| if err != nil { | ||
| if errors.Is(err, context.DeadlineExceeded) { | ||
| continue | ||
| } | ||
| if errors.Is(err, os.ErrDeadlineExceeded) { | ||
| continue | ||
| } | ||
| // Testing hook. | ||
| if testing.Testing() && strings.Contains(err.Error(), "connection refused") { | ||
| continue | ||
| } | ||
| } | ||
| return c, err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // DialNoTimeout will block with no timeout or until the context is canceled. | ||
| func DialNoTimeout() DialContextFunc { | ||
| dialer := &net.Dialer{} | ||
| return dialer.DialContext | ||
| } | ||
|
|
||
| // DefaultHTTPDialer has the same options as the default HTTP dialer. | ||
| func DefaultHTTPDialer() DialContextFunc { | ||
| dialer := &net.Dialer{ | ||
| Timeout: 30 * time.Second, | ||
| KeepAlive: 30 * time.Second, | ||
| } | ||
| return dialer.DialContext | ||
| } | ||
|
|
||
| // RacingDialer is a custom dialer that attempts to connect to a given address. | ||
| // | ||
| // It uses two different dialers. | ||
| // The dialer connects first is returned, and the other is canceled. | ||
| // | ||
| // The first has a short timeout (200 ms) and continues to retry until it succeeds. | ||
| // The second dialer has no timeout and will block until it either succeeds or fails. | ||
| // | ||
| // We are doing this because we see connection timeouts perhaps caused by some competing network routes. | ||
| // Our workaround is to use a short timeout dialer that will retry until it succeeds. | ||
|
Comment on lines
+59
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We dont need a retry limit bc the second one will eventually fail or succeed? Do we want to set some maximumum limit or log the timing so if this does end up being quite long we can see what the hold up is? |
||
| func RacingDialer(dialers ...DialContextFunc) DialContextFunc { | ||
| if len(dialers) == 0 { | ||
| return DialNoTimeout() | ||
| } | ||
|
|
||
| return func(ctx context.Context, network, address string) (net.Conn, error) { | ||
| ctx, cancel := context.WithCancel(ctx) | ||
| defer cancel() | ||
|
|
||
| type dialResult struct { | ||
| conn net.Conn | ||
| err error | ||
| } | ||
| resultCh := make(chan dialResult, len(dialers)) | ||
| for _, dialer := range dialers { | ||
| go func(d DialContextFunc) { | ||
| c, err := d(ctx, network, address) | ||
| resultCh <- dialResult{conn: c, err: err} | ||
| }(dialer) | ||
| } | ||
|
|
||
| var connError error | ||
| for range len(dialers) { | ||
| res := <-resultCh | ||
| if res.err == nil { | ||
| cancel() | ||
| return res.conn, nil | ||
| } else { | ||
| connError = res.err | ||
| } | ||
| } | ||
| return nil, connError | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestRetryDialUntilSuccess(t *testing.T) { | ||
| // "reserve" a port. | ||
| ln, err := net.Listen("tcp", ":0") | ||
| if err != nil { | ||
| t.Fatalf("failed to listen on random port: %v", err) | ||
| } | ||
| port := ln.Addr().(*net.TCPAddr).Port | ||
| _ = ln.Close() | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| defer cancel() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| // Wait a a bit so that the dialer will retry a few times. | ||
| time.Sleep(50 * time.Millisecond) | ||
|
|
||
| // Start a server to listen on the reserved port. | ||
| listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | ||
| if err != nil { | ||
| cancel() | ||
| return | ||
| } | ||
| t.Log("listener", listener.Addr()) | ||
| defer listener.Close() | ||
| conn, err := listener.Accept() | ||
| if err != nil { | ||
| cancel() | ||
| return | ||
| } | ||
| defer conn.Close() | ||
| }() | ||
|
|
||
| dialer := RetryDialUntilSuccess(10 * time.Millisecond) | ||
| conn, err := dialer(ctx, "tcp", fmt.Sprintf("localhost:%d", port)) | ||
| if err != nil { | ||
| t.Fatalf("failed to dial: %v", err) | ||
| } | ||
| conn.Close() | ||
| wg.Wait() | ||
| } | ||
|
|
||
| func TestRacingDialer(t *testing.T) { | ||
| // "reserve" a port. | ||
| ln, err := net.Listen("tcp", ":0") | ||
| if err != nil { | ||
| t.Fatalf("failed to listen on random port: %v", err) | ||
| } | ||
| port := ln.Addr().(*net.TCPAddr).Port | ||
| _ = ln.Close() | ||
|
|
||
| ctx, cancel := context.WithCancel(t.Context()) | ||
| defer cancel() | ||
|
|
||
| var wg sync.WaitGroup | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| // Wait a a bit so that the dialer will retry a few times. | ||
| time.Sleep(50 * time.Millisecond) | ||
|
|
||
| // Start a server to listen on the reserved port. | ||
| listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) | ||
| if err != nil { | ||
| cancel() | ||
| return | ||
| } | ||
| t.Log("listener", listener.Addr()) | ||
| defer listener.Close() | ||
| conn, err := listener.Accept() | ||
| if err != nil { | ||
| cancel() | ||
| return | ||
| } | ||
| defer conn.Close() | ||
| }() | ||
|
|
||
| dialer := RacingDialer(DialNoTimeout(), RetryDialUntilSuccess(10*time.Millisecond)) | ||
| conn, err := dialer(ctx, "tcp", fmt.Sprintf("localhost:%d", port)) | ||
| if err != nil { | ||
| t.Fatalf("failed to dial: %v", err) | ||
| } | ||
| conn.Close() | ||
| wg.Wait() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
understanding check: these are the only errors we're retrying for and others will just get returned below?