Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
network: Added ReachableHostPort() and a few helpers #6454
Conversation
|
Bogus job failure |
|
!!build!! |
|
!!build!! |
|
@dimitern I see the pre-check passed; what's wrong? |
|
@nskaggs it failed twice before that on lxc and once on trusty - with unrelated (build?) failures |
| +// UniqueHostPorts sends the unique entries in the given hostPorts slice on the | ||
| +// returned channel, closing the channel when done. It takes a stop channel, | ||
| +// which can be used to signal the sending loop to stop earlier. | ||
| +func UniqueHostPorts(stop <-chan struct{}, hostPorts []HostPort) <-chan HostPort { |
babbageclunk
Oct 17, 2016
Member
It doesn't seem like a host would ever have so many addresses that uniquifying them would need to be async (it'll be much much faster than a network hop anyway). This would be better just returning []HostPort and not using the goroutine.
dimitern
Oct 17, 2016
Contributor
| +// and clock are used to perform the dialing and measuring the time taken (which | ||
| +// is also set on error). Usually, a net.Dialer and clock.WallClock are passed | ||
| +// for dialer and clock, respectively. | ||
| +func DialHostPort(hostPort HostPort, dialer Dialer, clock clock.Clock) <-chan DialResult { |
babbageclunk
Oct 17, 2016
Member
As discussed below, this should take the destination channel as an argument.
| + | ||
| + startTime := clock.Now() | ||
| + | ||
| + conn, err := dialer.Dial("tcp", hostPort.NetAddr()) |
dimitern
Oct 17, 2016
Contributor
| + | ||
| + bestResult := DialResult{Duration: 24 * time.Hour} | ||
| + for hostPort := range UniqueHostPorts(stop, hostPorts) { | ||
| + lastResult := <-DialHostPort(hostPort, dialer, clock) |
babbageclunk
Oct 17, 2016
Member
This doesn't run the DialHostPorts in parallel - receiving from the channel immediately means that each one is done consecutively. You'd need to collect all of the channels up and select across them somehow. I think a better structure is to have one loop calling DialHostPort (passing in the same destination channel into each call), and then a second loop reading results from the channel.
added some commits
Oct 17, 2016
| - bestResult = lastResult | ||
| - logger.Debugf("best endpoint %q has duration %s", bestResult.Endpoint, bestResult.Duration) | ||
| - } | ||
| +// FastestHostPort dials the unique entries in the given hostPorts, in parallel, |
frobware
Oct 18, 2016
Contributor
"unique" is not needed here. It acts on whatever arguments you pass.
| - bestResult = lastResult | ||
| - logger.Debugf("best endpoint %q has duration %s", bestResult.Endpoint, bestResult.Duration) | ||
| - } | ||
| +// FastestHostPort dials the unique entries in the given hostPorts, in parallel, |
frobware
Oct 18, 2016
Contributor
s/FastestHostPort/ListeningEndpoints/ -- or something. Fastest doesn't convey that much? Fastest latency? Fastest for throughput?
| + | ||
| + for _, hostPort := range uniqueHPs { | ||
| + go func(hp HostPort) { | ||
| + conn, err := dialer.Dial("tcp", hp.NetAddr()) |
frobware
Oct 18, 2016
Contributor
It's not immediately clear why we can Dial without a timeout. Is it because of the select/timeout further down? And if some probes take longer than others do these active go routines close all their connections?
dimitern
Oct 18, 2016
Contributor
Because the dialer argument will be a net.Dialer, which already has a Timeout field you can set. This is explained in the doc comment. The final select is to ensure the global timeout argument is respected. Connections are always closed when established.
| + go func(hp HostPort) { | ||
| + conn, err := dialer.Dial("tcp", hp.NetAddr()) | ||
| + if err == nil { | ||
| + defer conn.Close() |
frobware
Oct 18, 2016
Contributor
Just call close. Why defer? This is an OS resource. Best we could do is discard it immediately if we can.
added some commits
Oct 18, 2016
dimitern
changed the title from
network: Added FastestHostPort and a few helpers
to
network: Added ReachableHostPort() and a few helpers
Oct 18, 2016
|
$$merge$$ |
|
Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju |
dimitern commentedOct 14, 2016
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.