Commit 89c950aa authored by Jeromy's avatar Jeromy

clean up organization of receivemessage and fix race

parent 5056a837
...@@ -270,26 +270,40 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg ...@@ -270,26 +270,40 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
// TODO: this is bad, and could be easily abused. // TODO: this is bad, and could be easily abused.
// Should only track *useful* messages in ledger // Should only track *useful* messages in ledger
if len(incoming.Blocks()) == 0 { iblocks := incoming.Blocks()
if len(iblocks) == 0 {
return return
} }
// quickly send out cancels, reduces chances of duplicate block receives // quickly send out cancels, reduces chances of duplicate block receives
var keys []u.Key var keys []u.Key
for _, block := range incoming.Blocks() { for _, block := range iblocks {
keys = append(keys, block.Key()) keys = append(keys, block.Key())
} }
bs.wm.CancelWants(keys) bs.wm.CancelWants(keys)
for _, block := range incoming.Blocks() { for _, block := range iblocks {
bs.counterLk.Lock() bs.counterLk.Lock()
bs.blocksRecvd++ bs.blocksRecvd++
if has, err := bs.blockstore.Has(block.Key()); err == nil && has { has, err := bs.blockstore.Has(block.Key())
if err == nil && has {
bs.dupBlocksRecvd++ bs.dupBlocksRecvd++
} }
brecvd := bs.blocksRecvd brecvd := bs.blocksRecvd
bdup := bs.dupBlocksRecvd bdup := bs.dupBlocksRecvd
bs.counterLk.Unlock() bs.counterLk.Unlock()
if has {
continue
}
// put this after the duplicate check as a block not on our wantlist may
// have already been received.
if _, found := bs.wm.wl.Contains(block.Key()); !found {
log.Notice("received un-asked-for block: %s", block)
continue
}
log.Infof("got block %s from %s (%d,%d)", block, p, brecvd, bdup) log.Infof("got block %s from %s (%d,%d)", block, p, brecvd, bdup)
hasBlockCtx, cancel := context.WithTimeout(ctx, hasBlockTimeout) hasBlockCtx, cancel := context.WithTimeout(ctx, hasBlockTimeout)
...@@ -302,7 +316,6 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg ...@@ -302,7 +316,6 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
// Connected/Disconnected warns bitswap about peer connections // Connected/Disconnected warns bitswap about peer connections
func (bs *Bitswap) PeerConnected(p peer.ID) { func (bs *Bitswap) PeerConnected(p peer.ID) {
// TODO: add to clientWorker??
bs.wm.Connected(p) bs.wm.Connected(p)
} }
...@@ -313,7 +326,7 @@ func (bs *Bitswap) PeerDisconnected(p peer.ID) { ...@@ -313,7 +326,7 @@ func (bs *Bitswap) PeerDisconnected(p peer.ID) {
} }
func (bs *Bitswap) ReceiveError(err error) { func (bs *Bitswap) ReceiveError(err error) {
log.Debugf("Bitswap ReceiveError: %s", err) log.Infof("Bitswap ReceiveError: %s", err)
// TODO log the network error // TODO log the network error
// TODO bubble the network error up to the parent context/error logger // TODO bubble the network error up to the parent context/error logger
} }
......
...@@ -21,7 +21,7 @@ type WantManager struct { ...@@ -21,7 +21,7 @@ type WantManager struct {
// synchronized by Run loop, only touch inside there // synchronized by Run loop, only touch inside there
peers map[peer.ID]*msgQueue peers map[peer.ID]*msgQueue
wl *wantlist.Wantlist wl *wantlist.ThreadSafe
network bsnet.BitSwapNetwork network bsnet.BitSwapNetwork
ctx context.Context ctx context.Context
...@@ -33,7 +33,7 @@ func NewWantManager(ctx context.Context, network bsnet.BitSwapNetwork) *WantMana ...@@ -33,7 +33,7 @@ func NewWantManager(ctx context.Context, network bsnet.BitSwapNetwork) *WantMana
connect: make(chan peer.ID, 10), connect: make(chan peer.ID, 10),
disconnect: make(chan peer.ID, 10), disconnect: make(chan peer.ID, 10),
peers: make(map[peer.ID]*msgQueue), peers: make(map[peer.ID]*msgQueue),
wl: wantlist.New(), wl: wantlist.NewThreadSafe(),
network: network, network: network,
ctx: ctx, ctx: ctx,
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment