When metainfo isn't complete, treat peers that don't support ut_metadata as useless

This commit is contained in:
Matt Joiner 2014-12-01 03:27:52 -06:00
parent 54ea32ddc4
commit e6ffa95fb4
1 changed files with 17 additions and 6 deletions

View File

@ -26,21 +26,32 @@ func (me *worstConns) Push(x interface{}) {
} }
type worstConnsSortKey struct { type worstConnsSortKey struct {
level int // Peer has something we want.
age time.Duration useless bool
// A fabricated duration since peer was last helpful.
age time.Duration
} }
func (me worstConnsSortKey) Less(other worstConnsSortKey) bool { func (me worstConnsSortKey) Less(other worstConnsSortKey) bool {
if me.level != other.level { if me.useless != other.useless {
return me.level > other.level return me.useless
} }
return me.age > other.age return me.age > other.age
} }
func (me worstConns) key(i int) (key worstConnsSortKey) { func (me worstConns) key(i int) (key worstConnsSortKey) {
c := me.c[i] c := me.c[i]
if time.Now().Sub(c.completedHandshake) >= 30*time.Second && !me.t.connHasWantedPieces(c) { // Peer has had time to declare what they have.
key.level = 1 if time.Now().Sub(c.completedHandshake) >= 30*time.Second {
if !me.t.haveInfo() {
if _, ok := c.PeerExtensionIDs["ut_metadata"]; !ok {
key.useless = true
}
} else {
if !me.t.connHasWantedPieces(c) {
key.useless = true
}
}
} }
key.age = time.Duration(1+3*c.UnwantedChunksReceived) * time.Now().Sub(func() time.Time { key.age = time.Duration(1+3*c.UnwantedChunksReceived) * time.Now().Sub(func() time.Time {
if !c.lastUsefulChunkReceived.IsZero() { if !c.lastUsefulChunkReceived.IsZero() {