From e6ffa95fb425cc53a8347271e7ef72f0edeef057 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 1 Dec 2014 03:27:52 -0600 Subject: [PATCH] When metainfo isn't complete, treat peers that don't support ut_metadata as useless --- worst_conns.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/worst_conns.go b/worst_conns.go index 3b26c18f..3176ba2a 100644 --- a/worst_conns.go +++ b/worst_conns.go @@ -26,21 +26,32 @@ func (me *worstConns) Push(x interface{}) { } type worstConnsSortKey struct { - level int - age time.Duration + // Peer has something we want. + useless bool + // A fabricated duration since peer was last helpful. + age time.Duration } func (me worstConnsSortKey) Less(other worstConnsSortKey) bool { - if me.level != other.level { - return me.level > other.level + if me.useless != other.useless { + return me.useless } return me.age > other.age } func (me worstConns) key(i int) (key worstConnsSortKey) { c := me.c[i] - if time.Now().Sub(c.completedHandshake) >= 30*time.Second && !me.t.connHasWantedPieces(c) { - key.level = 1 + // Peer has had time to declare what they have. + 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 { if !c.lastUsefulChunkReceived.IsZero() {