From f31593e883bc4573244f0d9ef9cfacb6208639ba Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 13 Dec 2018 23:56:04 +0200 Subject: [PATCH] prefill outoging channel with hello packet --- pubsub.go | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/pubsub.go b/pubsub.go index aeb076a..1f396e8 100644 --- a/pubsub.go +++ b/pubsub.go @@ -278,33 +278,20 @@ func (p *PubSub) processLoop(ctx context.Context) { } messages := make(chan *RPC, 32) + messages <- p.getHelloPacket() go p.handleNewPeer(ctx, pid, messages) p.peers[pid] = messages case s := <-p.newPeerStream: pid := s.Conn().RemotePeer() - ch, ok := p.peers[pid] + _, ok := p.peers[pid] if !ok { log.Warning("new stream for unknown peer: ", pid) s.Reset() continue } - select { - case ch <- p.getHelloPacket(): - default: - log.Warning("error sending hello packet; buffer full: ", pid) - go func() { - time.Sleep(time.Duration(1+rand.Intn(1000)) * time.Millisecond) - select { - case p.newPeerStream <- s: - case <-ctx.Done(): - } - }() - continue - } - p.rt.AddPeer(pid, s.Protocol()) case pid := <-p.newPeerError: @@ -323,6 +310,7 @@ func (p *PubSub) processLoop(ctx context.Context) { // we respawn the writer as we need to ensure there is a stream active log.Warning("peer declared dead but still connected; respawning writer: ", pid) messages := make(chan *RPC, 32) + messages <- p.getHelloPacket() go p.handleNewPeer(ctx, pid, messages) p.peers[pid] = messages continue