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

This commit is contained in:
Jeromy 2014-12-07 20:54:31 +00:00
parent 39162f2cea
commit e4a8a2ed65
2 changed files with 8 additions and 5 deletions

View File

@ -16,7 +16,7 @@ func newLedger(p peer.Peer, strategy strategyFunc) *ledger {
wantList: keySet{},
Strategy: strategy,
Partner: p,
sentToPeer: make(map[u.Key]struct{}),
sentToPeer: make(map[u.Key]time.Time),
}
}
@ -43,7 +43,7 @@ type ledger struct {
// sentToPeer is a set of keys to ensure we dont send duplicate blocks
// to a given peer
sentToPeer map[u.Key]struct{}
sentToPeer map[u.Key]time.Time
Strategy strategyFunc
}

View File

@ -10,6 +10,8 @@ import (
u "github.com/jbenet/go-ipfs/util"
)
const resendTimeoutPeriod = time.Minute
var log = u.Logger("strategy")
// 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 {
ledger := s.ledger(p)
// Dont resend blocks
if _, ok := ledger.sentToPeer[k]; ok {
// Dont resend blocks within a certain time period
t, ok := ledger.sentToPeer[k]
if ok && t.Add(resendTimeoutPeriod).After(time.Now()) {
return false
}
@ -79,7 +82,7 @@ func (s *strategist) BlockSentToPeer(k u.Key, p peer.Peer) {
defer s.lock.Unlock()
ledger := s.ledger(p)
ledger.sentToPeer[k] = struct{}{}
ledger.sentToPeer[k] = time.Now()
}
func (s *strategist) Seed(int64) {