It looks like Torrent.worstBadConn was returning an arbitrary bad connection, it wasn't the worst

heap.Init got lost somewhere along the way? Need a unit test for this.
This commit is contained in:
Matt Joiner 2017-09-21 19:32:03 +10:00
parent 326f60b319
commit 1f6a7eb810
1 changed files with 3 additions and 2 deletions

View File

@ -204,7 +204,7 @@ func (t *Torrent) addrActive(addr string) bool {
return false return false
} }
func (t *Torrent) worstUnclosedConns() (ret []*connection) { func (t *Torrent) unclosedConnsAsSlice() (ret []*connection) {
ret = make([]*connection, 0, len(t.conns)) ret = make([]*connection, 0, len(t.conns))
for c := range t.conns { for c := range t.conns {
if !c.closed.IsSet() { if !c.closed.IsSet() {
@ -758,7 +758,8 @@ func (t *Torrent) wantPieceIndex(index int) bool {
// pieces, or has been in worser half of the established connections for more // pieces, or has been in worser half of the established connections for more
// than a minute. // than a minute.
func (t *Torrent) worstBadConn() *connection { func (t *Torrent) worstBadConn() *connection {
wcs := worseConnSlice{t.worstUnclosedConns()} wcs := worseConnSlice{t.unclosedConnsAsSlice()}
heap.Init(&wcs)
for wcs.Len() != 0 { for wcs.Len() != 0 {
c := heap.Pop(&wcs).(*connection) c := heap.Pop(&wcs).(*connection)
if c.UnwantedChunksReceived >= 6 && c.UnwantedChunksReceived > c.UsefulChunksReceived { if c.UnwantedChunksReceived >= 6 && c.UnwantedChunksReceived > c.UsefulChunksReceived {