diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index d30fe665cc6..498fd9112b1 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -200,7 +200,7 @@ }, { "ImportPath": "github.com/jbenet/go-peerstream", - "Rev": "f90119e97e8be7b2bdd5e598067b0dc44df63381" + "Rev": "f3ab20739a88aa79306dc039c1b5a39e7afa45d6" }, { "ImportPath": "github.com/jbenet/go-random", diff --git a/Godeps/_workspace/src/github.com/jbenet/go-peerstream/conn.go b/Godeps/_workspace/src/github.com/jbenet/go-peerstream/conn.go index 2e066c2f40e..c4d9d3725e2 100644 --- a/Godeps/_workspace/src/github.com/jbenet/go-peerstream/conn.go +++ b/Godeps/_workspace/src/github.com/jbenet/go-peerstream/conn.go @@ -47,6 +47,9 @@ type Conn struct { closed bool closeLock sync.Mutex + + closing bool + closingLock sync.Mutex } func newConn(nconn net.Conn, tconn smux.Conn, s *Swarm) *Conn { @@ -115,14 +118,31 @@ func (c *Conn) Streams() []*Stream { return streams } +// GoClose spawns off a goroutine to close the connection iff the connection is +// not already being closed and returns immediately +func (c *Conn) GoClose() { + c.closingLock.Lock() + defer c.closingLock.Unlock() + if c.closing { + return + } + c.closing = true + + go c.Close() +} + // Close closes this connection func (c *Conn) Close() error { c.closeLock.Lock() defer c.closeLock.Unlock() - - if c.closed { + if c.closed == true { return nil } + + c.closingLock.Lock() + c.closing = true + c.closingLock.Unlock() + c.closed = true // close streams diff --git a/Godeps/_workspace/src/github.com/jbenet/go-peerstream/muxtest/muxt.go b/Godeps/_workspace/src/github.com/jbenet/go-peerstream/muxtest/muxt.go index 4bfef14e672..dd3342a0d2a 100644 --- a/Godeps/_workspace/src/github.com/jbenet/go-peerstream/muxtest/muxt.go +++ b/Godeps/_workspace/src/github.com/jbenet/go-peerstream/muxtest/muxt.go @@ -14,6 +14,7 @@ import ( "testing" ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream" + smux "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-stream-muxer" ) diff --git a/Godeps/_workspace/src/github.com/jbenet/go-peerstream/swarm.go b/Godeps/_workspace/src/github.com/jbenet/go-peerstream/swarm.go index 9497bd93340..c152471827d 100644 --- a/Godeps/_workspace/src/github.com/jbenet/go-peerstream/swarm.go +++ b/Godeps/_workspace/src/github.com/jbenet/go-peerstream/swarm.go @@ -184,7 +184,7 @@ func (s *Swarm) Conns() []*Conn { open := make([]*Conn, 0, len(conns)) for _, c := range conns { if c.smuxConn.IsClosed() { - c.Close() + c.GoClose() } else { open = append(open, c) }