prevent sending of same block to a peer twice
Jeromy committed
Dec 7, 2014 at 07:54 UTC
39162f2cea5511fdc14461a386b925b2c26f33d5
4 files changed
+25
-3
exchange/bitswap/bitswap.go
+1
@@ -307,6 +307,7 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
307
308
blkmsg.AddBlock(block)
309
bs.send(ctx, p, blkmsg)
310
+ bs.strategy.BlockSentToPeer(block.Key(), p)
311
}
312
}
313
}
exchange/bitswap/strategy/interface.go
+2
@@ -32,6 +32,8 @@ type Strategy interface {
32
33
NumBytesReceivedFrom(peer.Peer) uint64
34
35
+ BlockSentToPeer(u.Key, peer.Peer)
36
+
37
// Values determining bitswap behavioural patterns
38
GetBatchSize() int
39
GetRebroadcastDelay() time.Duration
exchange/bitswap/strategy/ledger.go
+8
-3
@@ -13,9 +13,10 @@ type keySet map[u.Key]struct{}
13
14
func newLedger(p peer.Peer, strategy strategyFunc) *ledger {
15
return &ledger{
16
- wantList: keySet{},
17
- Strategy: strategy,
18
- Partner: p,
16
+ wantList: keySet{},
17
+ Strategy: strategy,
18
+ Partner: p,
19
+ sentToPeer: make(map[u.Key]struct{}),
20
}
21
}
22
@@ -40,6 +41,10 @@ type ledger struct {
41
// wantList is a (bounded, small) set of keys that Partner desires.
42
wantList keySet
43
44
+ // sentToPeer is a set of keys to ensure we dont send duplicate blocks
45
+ // to a given peer
46
+ sentToPeer map[u.Key]struct{}
47
+
48
Strategy strategyFunc
49
}
50
exchange/bitswap/strategy/strategy.go
+14
@@ -65,9 +65,23 @@ func (s *strategist) ShouldSendBlockToPeer(k u.Key, p peer.Peer) bool {
65
defer s.lock.RUnlock()
66
67
ledger := s.ledger(p)
68
+
69
+ // Dont resend blocks
70
+ if _, ok := ledger.sentToPeer[k]; ok {
71
+ return false
72
+ }
73
+
74
return ledger.ShouldSend()
75
}
76
77
+func (s *strategist) BlockSentToPeer(k u.Key, p peer.Peer) {
78
+ s.lock.Lock()
79
+ defer s.lock.Unlock()
80
+
81
+ ledger := s.ledger(p)
82
+ ledger.sentToPeer[k] = struct{}{}
83
+}
84
+
85
func (s *strategist) Seed(int64) {
86
s.lock.Lock()
87
defer s.lock.Unlock()