From 01ce8e7d7106481b9edc521ab4c9206a1e6d41f2 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 28 Sep 2016 17:08:13 -0700 Subject: [PATCH] only pass keys down newBlocks chan in bitswap License: MIT Signed-off-by: Jeromy --- exchange/bitswap/bitswap.go | 11 ++++++++--- exchange/bitswap/workers.go | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index 1f99fa4cd68..580d498451e 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -90,7 +90,7 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork, network: network, findKeys: make(chan *blockRequest, sizeBatchRequestChan), process: px, - newBlocks: make(chan blocks.Block, HasBlockBufferSize), + newBlocks: make(chan key.Key, HasBlockBufferSize), provideKeys: make(chan key.Key, provideKeysBufferSize), wm: NewWantManager(ctx, network), } @@ -137,7 +137,7 @@ type Bitswap struct { process process.Process - newBlocks chan blocks.Block + newBlocks chan key.Key provideKeys chan key.Key @@ -308,12 +308,17 @@ func (bs *Bitswap) HasBlock(blk blocks.Block) error { return err } + // NOTE: There exists the possiblity for a race condition here. If a user + // creates a node, then adds it to the dagservice while another goroutine + // is waiting on a GetBlock for that object, they will receive a reference + // to the same node. We should address this soon, but i'm not going to do + // it now as it requires more thought and isnt causing immediate problems. bs.notifications.Publish(blk) bs.engine.AddBlock(blk) select { - case bs.newBlocks <- blk: + case bs.newBlocks <- blk.Key(): // send block off to be reprovided case <-bs.process.Closing(): return bs.process.Close() diff --git a/exchange/bitswap/workers.go b/exchange/bitswap/workers.go index 9f5c6c5ead7..51fc1fde89a 100644 --- a/exchange/bitswap/workers.go +++ b/exchange/bitswap/workers.go @@ -127,17 +127,17 @@ func (bs *Bitswap) provideCollector(ctx context.Context) { for { select { - case blk, ok := <-bs.newBlocks: + case blkey, ok := <-bs.newBlocks: if !ok { log.Debug("newBlocks channel closed") return } if keysOut == nil { - nextKey = blk.Key() + nextKey = blkey keysOut = bs.provideKeys } else { - toProvide = append(toProvide, blk.Key()) + toProvide = append(toProvide, blkey) } case keysOut <- nextKey: if len(toProvide) > 0 {