-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When both IPv4 and IPv6 is available, race dial to all IPs and keep the fastest one. Unlike happy eyeball, we don't give a head start to IPv6 as we always want the absolute best. Fixes #8
- Loading branch information
Showing
7 changed files
with
85 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,48 @@ | ||
package endpoint | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
type parallelDialer struct { | ||
net.Dialer | ||
} | ||
|
||
func (d *parallelDialer) DialParallel(ctx context.Context, network string, addrs []string) (net.Conn, error) { | ||
if len(addrs) == 1 { | ||
return d.DialContext(ctx, network, addrs[0]) | ||
} | ||
returned := make(chan struct{}) | ||
defer close(returned) | ||
|
||
type dialResult struct { | ||
net.Conn | ||
error | ||
} | ||
results := make(chan dialResult) | ||
|
||
racer := func(addr string) { | ||
c, err := d.DialContext(ctx, network, addr) | ||
select { | ||
case results <- dialResult{Conn: c, error: err}: | ||
case <-returned: | ||
if c != nil { | ||
c.Close() | ||
} | ||
} | ||
} | ||
|
||
for _, addr := range addrs { | ||
go racer(addr) | ||
} | ||
|
||
var err error | ||
for res := range results { | ||
if res.error == nil { | ||
return res.Conn, nil | ||
} | ||
err = res.error | ||
} | ||
return nil, err | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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