clean up organization of receivemessage and fix race
Jeromy committed
May 26, 2015 at 11:14 UTC
ab161cf6b4f2cee1c3b63c943b640b120f9fca93
2 files changed
+21
-8
exchange/bitswap/bitswap.go
+19
-6
@@ -270,26 +270,40 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
270
// TODO: this is bad, and could be easily abused.
271
// Should only track *useful* messages in ledger
272
273
- if len(incoming.Blocks()) == 0 {
273
+ iblocks := incoming.Blocks()
274
+
275
+ if len(iblocks) == 0 {
276
return
277
}
278
279
// quickly send out cancels, reduces chances of duplicate block receives
280
var keys []u.Key
279
- for _, block := range incoming.Blocks() {
281
+ for _, block := range iblocks {
282
keys = append(keys, block.Key())
283
}
284
bs.wm.CancelWants(keys)
285
284
- for _, block := range incoming.Blocks() {
286
+ for _, block := range iblocks {
287
bs.counterLk.Lock()
288
bs.blocksRecvd++
287
- if has, err := bs.blockstore.Has(block.Key()); err == nil && has {
289
+ has, err := bs.blockstore.Has(block.Key())
290
+ if err == nil && has {
291
bs.dupBlocksRecvd++
292
}
293
brecvd := bs.blocksRecvd
294
bdup := bs.dupBlocksRecvd
295
bs.counterLk.Unlock()
296
+ if has {
297
+ continue
298
+ }
299
+
300
+ // put this after the duplicate check as a block not on our wantlist may
301
+ // have already been received.
302
+ if _, found := bs.wm.wl.Contains(block.Key()); !found {
303
+ log.Notice("received un-asked-for block: %s", block)
304
+ continue
305
+ }
306
+
307
log.Infof("got block %s from %s (%d,%d)", block, p, brecvd, bdup)
308
309
hasBlockCtx, cancel := context.WithTimeout(ctx, hasBlockTimeout)
@@ -302,7 +316,6 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
316
317
// Connected/Disconnected warns bitswap about peer connections
318
func (bs *Bitswap) PeerConnected(p peer.ID) {
305
- // TODO: add to clientWorker??
319
bs.wm.Connected(p)
320
}
321
@@ -313,7 +326,7 @@ func (bs *Bitswap) PeerDisconnected(p peer.ID) {
326
}
327
328
func (bs *Bitswap) ReceiveError(err error) {
316
- log.Debugf("Bitswap ReceiveError: %s", err)
329
+ log.Infof("Bitswap ReceiveError: %s", err)
330
// TODO log the network error
331
// TODO bubble the network error up to the parent context/error logger
332
}
exchange/bitswap/wantmanager.go
+2
-2
@@ -21,7 +21,7 @@ type WantManager struct {
21
22
// synchronized by Run loop, only touch inside there
23
peers map[peer.ID]*msgQueue
24
- wl *wantlist.Wantlist
24
+ wl *wantlist.ThreadSafe
25
26
network bsnet.BitSwapNetwork
27
ctx context.Context
@@ -33,7 +33,7 @@ func NewWantManager(ctx context.Context, network bsnet.BitSwapNetwork) *WantMana
33
connect: make(chan peer.ID, 10),
34
disconnect: make(chan peer.ID, 10),
35
peers: make(map[peer.ID]*msgQueue),
36
- wl: wantlist.New(),
36
+ wl: wantlist.NewThreadSafe(),
37
network: network,
38
ctx: ctx,
39
}