network: Added reusable FastestHostPort() helper #6426

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
3 participants
Contributor

dimitern commented Oct 11, 2016

Takes a timeout and a slice of []network.HostPort entires.
Uses parallel.Try internally to connect to all of them
concurrently, with the given timeout. Needed to overcome
the limitation of the openssh client hanging when trying
to connect to an unreachable or blackhole routes.

Prerequisite to fix http://pad.lv/1616098.

Are the tests supposed to mock entries like bad.example.com, et al?

network/hostport.go
+// successfully connected to - the one with the lowest latency - or an error if
+// none of the given hostPorts can be reached.
+//
+// Timeout should be short, e.g. between 100ms and 1000ms.
@frobware

frobware Oct 11, 2016

Contributor

From the PR I guess nothing is using this but I wonder if the exemplary timeout value of 1s is too short on a heavily loaded system.

@dimitern

dimitern Oct 11, 2016

Contributor

👍 Changed to 3s

network/hostport.go
+ conn, _ := result.(net.Conn) // cannot fail
+ defer conn.Close()
+
+ fastest := conn.RemoteAddr().String()
@frobware

frobware Oct 11, 2016

Contributor

Just return ParseHostPort(conn.RemoteAddr().String()), the variable assignment is just noise. The function name (should) capture the intent.

@dimitern

dimitern Oct 11, 2016

Contributor

👍 Done

+func (*HostPortSuite) TestFastestHostPortAllUnreachable(c *gc.C) {
+ // All of the endpoints below should either block indefinitely (without a
+ // timeout) or fail immediately.
+ unreachableHPs := network.NewHostPorts(49151, // IANA reserved port (unreachable)
@frobware

frobware Oct 11, 2016

Contributor

Is 49151 unreachable on windows? ;)

@dimitern

dimitern Oct 11, 2016

Contributor

It is AFAICS on win2012r2 I have locally.

network/hostport_test.go
+ // timeout) or fail immediately.
+ unreachableHPs := network.NewHostPorts(49151, // IANA reserved port (unreachable)
+ "255.1.2.3", // IPv4 route unreachable
+ "bad.example.com", // unresolvable
@frobware

frobware Oct 11, 2016

Contributor

On my system:

$ dig bad.example.com +short
92.242.132.15

Are you expecting this to not resolve?

@dimitern

dimitern Oct 11, 2016

Contributor

👍 Fair enough - changed to "bad host name", which definitely doesn't resolve :)

@frobware

frobware Oct 11, 2016

Contributor

$ dig "bad host name" +short
92.242.132.15

As discussed I think we should only consider localhost in the unit tests.

@dimitern

dimitern Oct 11, 2016

Contributor

Dropped this as discussed.

@@ -247,7 +247,7 @@ func EnsureFirstHostPort(first HostPort, hps []HostPort) []HostPort {
// successfully connected to - the one with the lowest latency - or an error if
// none of the given hostPorts can be reached.
//
-// Timeout should be short, e.g. between 100ms and 1000ms.
+// Timeout should be short, e.g. between 100ms and 3s.
func FastestHostPort(timeout time.Duration, hostPorts ...HostPort) (*HostPort, error) {
@frobware

frobware Oct 11, 2016

Contributor

Why not inject the amount of parallelism?

+ endpoints := set.NewStrings(HostPortsToStrings(hostPorts)...)
+ for _, endpoint := range endpoints.Values() {
+ try.Start(func(stop <-chan struct{}) (io.Closer, error) {
+ return net.DialTimeout("tcp", endpoint, timeout)
@frobware

frobware Oct 11, 2016

Contributor

If this successfully connects (and others), when are they closed? The way I read the function is that we return the fastest port to accept but, once done, we don't actually need or want the connection.

Contributor

dimitern commented Oct 14, 2016

Closing this - will re-propose against develop after the changes I made.

@dimitern dimitern closed this Oct 14, 2016

@dimitern dimitern deleted the dimitern:lp-1616098-ssh-all-ips branch Oct 18, 2016

jujubot added a commit that referenced this pull request Oct 18, 2016

Merge pull request #6454 from dimitern/network-fastesthostport
network: Added ReachableHostPort() and a few helpers

Based on PR #6426, but using a different, hopefuly simpler  approach
with full test coverage. None of the added helpers are used yet, but
will be in a follow-up.

Prerequiste to fix http://pad.lv/1616098.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment