Skip to content

Commit

Permalink
don't use an extra goroutine for live hop cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed May 7, 2019
1 parent 1224606 commit 66dfe16
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions p2p/protocol/internal/circuitv1-deprecated/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,13 +369,18 @@ func (r *Relay) handleHopStream(s inet.Stream, msg *pb.CircuitRelay) {

r.addLiveHop(src.ID, dst.ID)

var wg sync.WaitGroup
wg.Add(2)
goroutines := new(int32)
*goroutines = 2
done := func() {
if atomic.AddInt32(goroutines, -1) == 0 {
r.rmLiveHop(src.ID, dst.ID)
}
}

// Don't reset streams after finishing or the other side will get an
// error, not an EOF.
go func() {
defer wg.Done()
defer done()

buf := pool.Get(HopStreamBufferSize)
defer pool.Put(buf)
Expand All @@ -394,7 +399,7 @@ func (r *Relay) handleHopStream(s inet.Stream, msg *pb.CircuitRelay) {
}()

go func() {
defer wg.Done()
defer done()

buf := pool.Get(HopStreamBufferSize)
defer pool.Put(buf)
Expand All @@ -411,11 +416,6 @@ func (r *Relay) handleHopStream(s inet.Stream, msg *pb.CircuitRelay) {
}
log.Debugf("relayed %d bytes from %s to %s", count, src.ID.Pretty(), dst.ID.Pretty())
}()

go func() {
wg.Wait()
r.rmLiveHop(src.ID, dst.ID)
}()
}

func (r *Relay) handleStopStream(s inet.Stream, msg *pb.CircuitRelay) {
Expand Down

0 comments on commit 66dfe16

Please sign in to comment.