Skip to content

Commit

Permalink
Merge branch 'master' into 1941
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Nov 4, 2014
2 parents b3eafc1 + db52be0 commit b1d92af
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions tlsdialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type ConnWithTimings struct {
// HandshakeTime: the amount of time that it took to complete the TLS
// handshake
HandshakeTime time.Duration
// ResolvedAddr: the address to which our dns lookup resolved
ResolvedAddr *net.TCPAddr
// VerifiedChains: like tls.ConnectionState.VerifiedChains
VerifiedChains [][]*x509.Certificate
}
Expand Down Expand Up @@ -89,11 +91,10 @@ func DialForTimings(dialer *net.Dialer, network, addr string, sendServerName boo

log.Tracef("Resolving addr: %s", addr)
start := time.Now()
var resolved *net.TCPAddr
var err error
if timeout == 0 {
log.Tracef("Resolving immediately")
resolved, err = net.ResolveTCPAddr("tcp", addr)
result.ResolvedAddr, err = net.ResolveTCPAddr("tcp", addr)
} else {
log.Tracef("Resolving on goroutine")
resolvedCh := make(chan *net.TCPAddr, 10)
Expand All @@ -106,19 +107,19 @@ func DialForTimings(dialer *net.Dialer, network, addr string, sendServerName boo
err = <-errCh
if err == nil {
log.Tracef("No error, looking for resolved")
resolved = <-resolvedCh
result.ResolvedAddr = <-resolvedCh
}
}

if err != nil {
return result, err
}
result.ResolutionTime = time.Now().Sub(start)
log.Tracef("Resolved addr %s to %s in %s", addr, resolved, result.ResolutionTime)
log.Tracef("Resolved addr %s to %s in %s", addr, result.ResolvedAddr, result.ResolutionTime)

log.Tracef("Dialing %s %s (%s)", network, addr, resolved)
log.Tracef("Dialing %s %s (%s)", network, addr, result.ResolvedAddr)
start = time.Now()
rawConn, err := dialer.Dial(network, resolved.String())
rawConn, err := dialer.Dial(network, result.ResolvedAddr.String())
if err != nil {
return result, err
}
Expand Down
8 changes: 6 additions & 2 deletions tlsdialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ func init() {

func TestOKWithServerName(t *testing.T) {
fdStart := countTCPFiles()
conn, err := Dial("tcp", ADDR, true, &tls.Config{
cwt, err := DialForTimings(new(net.Dialer), "tcp", ADDR, true, &tls.Config{
RootCAs: cert.PoolContainingCert(),
})
conn := cwt.Conn
assert.NoError(t, err, "Unable to dial")
serverName := <-receivedServerNames
assert.Equal(t, "localhost", serverName, "Unexpected ServerName on server")
assert.NotNil(t, cwt.ResolvedAddr, "Should have resolved addr")
closeAndCountFDs(t, conn, err, fdStart)
}

Expand Down Expand Up @@ -148,6 +150,8 @@ func TestVariableTimeouts(t *testing.T) {
for i := 0; i < 500; i++ {
doTestTimeout(t, time.Duration(rand.Intn(5000)+1)*time.Microsecond)
}
// Wait to give the sockets time to close
time.Sleep(1 * time.Second)
fdEnd := countTCPFiles()
assert.Equal(t, fdStart, fdEnd, "Number of open files should be the same after test as before")
}
Expand Down Expand Up @@ -180,7 +184,7 @@ func closeAndCountFDs(t *testing.T, conn *tls.Conn, err error, fdStart int) {
conn.Close()
}
fdEnd := countTCPFiles()
assert.Equal(t, fdStart, fdEnd, "Number of open files should be the same after test as before")
assert.Equal(t, fdStart, fdEnd, "Number of open TCP files should be the same after test as before")
}

// see https://groups.google.com/forum/#!topic/golang-nuts/c0AnWXjzNIA
Expand Down

0 comments on commit b1d92af

Please sign in to comment.