Commit 29aa7547 authored by Jeromy's avatar Jeromy

same block cant be sent twice to a peer within a certain time period

parent 13ab516c
...@@ -16,7 +16,7 @@ func newLedger(p peer.Peer, strategy strategyFunc) *ledger { ...@@ -16,7 +16,7 @@ func newLedger(p peer.Peer, strategy strategyFunc) *ledger {
wantList: keySet{}, wantList: keySet{},
Strategy: strategy, Strategy: strategy,
Partner: p, Partner: p,
sentToPeer: make(map[u.Key]struct{}), sentToPeer: make(map[u.Key]time.Time),
} }
} }
...@@ -43,7 +43,7 @@ type ledger struct { ...@@ -43,7 +43,7 @@ type ledger struct {
// sentToPeer is a set of keys to ensure we dont send duplicate blocks // sentToPeer is a set of keys to ensure we dont send duplicate blocks
// to a given peer // to a given peer
sentToPeer map[u.Key]struct{} sentToPeer map[u.Key]time.Time
Strategy strategyFunc Strategy strategyFunc
} }
......
...@@ -10,6 +10,8 @@ import ( ...@@ -10,6 +10,8 @@ import (
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
) )
const resendTimeoutPeriod = time.Minute
var log = u.Logger("strategy") var log = u.Logger("strategy")
// TODO niceness should be on a per-peer basis. Use-case: Certain peers are // TODO niceness should be on a per-peer basis. Use-case: Certain peers are
...@@ -66,8 +68,9 @@ func (s *strategist) ShouldSendBlockToPeer(k u.Key, p peer.Peer) bool { ...@@ -66,8 +68,9 @@ func (s *strategist) ShouldSendBlockToPeer(k u.Key, p peer.Peer) bool {
ledger := s.ledger(p) ledger := s.ledger(p)
// Dont resend blocks // Dont resend blocks within a certain time period
if _, ok := ledger.sentToPeer[k]; ok { t, ok := ledger.sentToPeer[k]
if ok && t.Add(resendTimeoutPeriod).After(time.Now()) {
return false return false
} }
...@@ -79,7 +82,7 @@ func (s *strategist) BlockSentToPeer(k u.Key, p peer.Peer) { ...@@ -79,7 +82,7 @@ func (s *strategist) BlockSentToPeer(k u.Key, p peer.Peer) {
defer s.lock.Unlock() defer s.lock.Unlock()
ledger := s.ledger(p) ledger := s.ledger(p)
ledger.sentToPeer[k] = struct{}{} ledger.sentToPeer[k] = time.Now()
} }
func (s *strategist) Seed(int64) { func (s *strategist) Seed(int64) {
......
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