1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:23:18 +08:00

make sure not to orphan any extra goroutines

This commit is contained in:
Jeromy
2015-02-26 16:43:18 -08:00
parent 9c66f65410
commit 8256b18058
2 changed files with 14 additions and 1 deletions

View File

@ -368,14 +368,19 @@ func (bs *bitswap) wantNewBlocks(ctx context.Context, bkeys []u.Key) {
for i, k := range bkeys {
message.AddEntry(k, kMaxPriority-i)
}
wg := sync.WaitGroup{}
for _, p := range bs.engine.Peers() {
wg.Add(1)
go func(p peer.ID) {
defer wg.Done()
err := bs.send(ctx, p, message)
if err != nil {
log.Debugf("Error sending message: %s", err)
}
}(p)
}
wg.Wait()
}
func (bs *bitswap) ReceiveError(err error) {

View File

@ -90,7 +90,11 @@ func (bs *bitswap) clientWorker(parent context.Context) {
bs.wantlist.Add(k, kMaxPriority-i)
}
bs.wantNewBlocks(req.ctx, keys)
done := make(chan struct{})
go func() {
bs.wantNewBlocks(req.ctx, keys)
close(done)
}()
// NB: Optimization. Assumes that providers of key[0] are likely to
// be able to provide for all keys. This currently holds true in most
@ -101,6 +105,10 @@ func (bs *bitswap) clientWorker(parent context.Context) {
if err != nil {
log.Debugf("error sending wantlist: %s", err)
}
// Wait for wantNewBlocks to finish
<-done
case <-parent.Done():
return
}