From 5ee3bddaadfdf823f330cba49feb896b7cfbe81d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 27 Jul 2017 18:43:54 -0700 Subject: [PATCH] notify: handle new connections asynchronously Trying to create a stream from with a connection notifier is *not* allowed (and will deadlock). Also, use the pubsub context when trying to open the stream. --- notify.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/notify.go b/notify.go index 5d94878..fdcaf39 100644 --- a/notify.go +++ b/notify.go @@ -1,8 +1,6 @@ package floodsub import ( - "context" - inet "github.com/libp2p/go-libp2p-net" ma "github.com/multiformats/go-multiaddr" ) @@ -18,17 +16,19 @@ func (p *PubSubNotif) ClosedStream(n inet.Network, s inet.Stream) { } func (p *PubSubNotif) Connected(n inet.Network, c inet.Conn) { - s, err := p.host.NewStream(context.Background(), c.RemotePeer(), ID) - if err != nil { - log.Warning("opening new stream to peer: ", err, c.LocalPeer(), c.RemotePeer()) - return - } + go func() { + s, err := p.host.NewStream(p.ctx, c.RemotePeer(), ID) + if err != nil { + log.Warning("opening new stream to peer: ", err, c.LocalPeer(), c.RemotePeer()) + return + } - select { - case p.newPeers <- s: - case <-p.ctx.Done(): - s.Close() - } + select { + case p.newPeers <- s: + case <-p.ctx.Done(): + s.Close() + } + }() } func (p *PubSubNotif) Disconnected(n inet.Network, c inet.Conn) {