Commit 382daf3d authored by Juan Batiz-Benet's avatar Juan Batiz-Benet
Browse files

Merge pull request #640 from jbenet/races

more races 2015-01-24
parents 992c520a ef4fb9dd
...@@ -89,10 +89,17 @@ func (d *Dialer) rawConnDial(ctx context.Context, raddr ma.Multiaddr, remote pee ...@@ -89,10 +89,17 @@ func (d *Dialer) rawConnDial(ctx context.Context, raddr ma.Multiaddr, remote pee
laddr := pickLocalAddr(d.LocalAddrs, raddr) laddr := pickLocalAddr(d.LocalAddrs, raddr)
log.Debugf("%s dialing %s -- %s --> %s", d.LocalPeer, remote, laddr, raddr) log.Debugf("%s dialing %s -- %s --> %s", d.LocalPeer, remote, laddr, raddr)
// make a copy of the manet.Dialer, we may need to change its timeout.
madialer := d.Dialer
if laddr != nil && reuseport.Available() { if laddr != nil && reuseport.Available() {
// we're perhaps going to dial twice. half the timeout, so we can afford to.
// otherwise our context would expire right after the first dial.
madialer.Dialer.Timeout = (madialer.Dialer.Timeout / 2)
// dial using reuseport.Dialer, because we're probably reusing addrs. // dial using reuseport.Dialer, because we're probably reusing addrs.
// this is optimistic, as the reuseDial may fail to bind the port. // this is optimistic, as the reuseDial may fail to bind the port.
if nconn, retry, reuseErr := d.reuseDial(laddr, raddr); reuseErr == nil { if nconn, retry, reuseErr := reuseDial(madialer.Dialer, laddr, raddr); reuseErr == nil {
// if it worked, wrap the raw net.Conn with our manet.Conn // if it worked, wrap the raw net.Conn with our manet.Conn
log.Debugf("%s reuse worked! %s %s %s", d.LocalPeer, laddr, nconn.RemoteAddr(), nconn) log.Debugf("%s reuse worked! %s %s %s", d.LocalPeer, laddr, nconn.RemoteAddr(), nconn)
return manet.WrapNetConn(nconn) return manet.WrapNetConn(nconn)
...@@ -105,22 +112,18 @@ func (d *Dialer) rawConnDial(ctx context.Context, raddr ma.Multiaddr, remote pee ...@@ -105,22 +112,18 @@ func (d *Dialer) rawConnDial(ctx context.Context, raddr ma.Multiaddr, remote pee
} }
} }
// no local addr, or reuseport failed. just dial straight with a new port. return madialer.Dial(raddr)
return d.Dialer.Dial(raddr)
} }
func (d *Dialer) reuseDial(laddr, raddr ma.Multiaddr) (conn net.Conn, retry bool, err error) { func reuseDial(dialer net.Dialer, laddr, raddr ma.Multiaddr) (conn net.Conn, retry bool, err error) {
if laddr == nil { if laddr == nil {
// if we're given no local address no sense in using reuseport to dial, dial out as usual. // if we're given no local address no sense in using reuseport to dial, dial out as usual.
return nil, true, reuseport.ErrReuseFailed return nil, true, reuseport.ErrReuseFailed
} }
// half the timeout so we can retry regularly if this fails.
d.Dialer.Dialer.Timeout = (d.Dialer.Dialer.Timeout / 2)
// give reuse.Dialer the manet.Dialer's Dialer. // give reuse.Dialer the manet.Dialer's Dialer.
// (wow, Dialer should've so been an interface...) // (wow, Dialer should've so been an interface...)
rd := reuseport.Dialer{d.Dialer.Dialer} rd := reuseport.Dialer{dialer}
// get the local net.Addr manually // get the local net.Addr manually
rd.D.LocalAddr, err = manet.ToNetAddr(laddr) rd.D.LocalAddr, err = manet.ToNetAddr(laddr)
......
...@@ -10,6 +10,7 @@ import ( ...@@ -10,6 +10,7 @@ import (
peer "github.com/jbenet/go-ipfs/p2p/peer" peer "github.com/jbenet/go-ipfs/p2p/peer"
testutil "github.com/jbenet/go-ipfs/util/testutil" testutil "github.com/jbenet/go-ipfs/util/testutil"
ci "github.com/jbenet/go-ipfs/util/testutil/ci"
jenkins "github.com/jbenet/go-ipfs/util/testutil/ci/jenkins" jenkins "github.com/jbenet/go-ipfs/util/testutil/ci/jenkins"
travis "github.com/jbenet/go-ipfs/util/testutil/ci/travis" travis "github.com/jbenet/go-ipfs/util/testutil/ci/travis"
...@@ -377,9 +378,9 @@ func TestDialBackoffClears(t *testing.T) { ...@@ -377,9 +378,9 @@ func TestDialBackoffClears(t *testing.T) {
defer s2.Close() defer s2.Close()
s1.dialT = time.Millisecond * 300 // lower timeout for tests. s1.dialT = time.Millisecond * 300 // lower timeout for tests.
s2.dialT = time.Millisecond * 300 // lower timeout for tests. s2.dialT = time.Millisecond * 300 // lower timeout for tests.
if travis.IsRunning() { if ci.IsRunning() {
s1.dialT = time.Second s1.dialT = 2 * time.Second
s2.dialT = time.Second s2.dialT = 2 * time.Second
} }
// use another address first, that accept and hang on conns // use another address first, that accept and hang on conns
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment