func (t *Torrent) SetMaxEstablishedConns(max int) (oldMax int) { t.cl.mu.Lock() defer t.cl.mu.Unlock() oldMax = t.maxEstablishedConns t.maxEstablishedConns = max wcs := slices.HeapInterface(append([]*connection(nil), t.conns...), worseConn) for len(t.conns) > t.maxEstablishedConns && wcs.Len() > 0 { t.dropConnection(wcs.Pop().(*connection)) } t.openNewConns() return oldMax }
// The worst connection is one that hasn't been sent, or sent anything useful // for the longest. A bad connection is one that usually sends us unwanted // pieces, or has been in worser half of the established connections for more // than a minute. func (t *Torrent) worstBadConn() *connection { wcs := slices.HeapInterface(t.worstUnclosedConns(), worseConn) for wcs.Len() != 0 { c := heap.Pop(wcs).(*connection) if c.UnwantedChunksReceived >= 6 && c.UnwantedChunksReceived > c.UsefulChunksReceived { return c } if wcs.Len() >= (t.maxEstablishedConns+1)/2 { // Give connections 1 minute to prove themselves. if time.Since(c.completedHandshake) > time.Minute { return c } } } return nil }