Skip to content

Commit

Permalink
defer stream removal instead of doing it inline.
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jan 12, 2021
1 parent b080f59 commit 9e129db
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions comm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ func (p *PubSub) handleNewStream(s network.Stream) {
p.inboundStreams[peer] = s
p.inboundStreamsMx.Unlock()

defer func() {
p.inboundStreamsMx.Lock()
if p.inboundStreams[peer] == s {
delete(p.inboundStreams, peer)
}
p.inboundStreamsMx.Unlock()
}()

r := protoio.NewDelimitedReader(s, p.maxMessageSize)
for {
rpc := new(RPC)
Expand All @@ -66,12 +74,6 @@ func (p *PubSub) handleNewStream(s network.Stream) {
s.Close()
}

p.inboundStreamsMx.Lock()
if p.inboundStreams[peer] == s {
delete(p.inboundStreams, peer)
}
p.inboundStreamsMx.Unlock()

return
}

Expand Down

0 comments on commit 9e129db

Please sign in to comment.