2014-08-28 06:04:41 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-09-11 18:31:31 +08:00
|
|
|
// Implements heap functions such that [0] is the worst connection.
|
|
|
|
type worstConns []*connection
|
2014-08-28 06:04:41 +08:00
|
|
|
|
2014-09-11 18:31:31 +08:00
|
|
|
func (me worstConns) Len() int { return len(me) }
|
|
|
|
func (me worstConns) Swap(i, j int) { me[i], me[j] = me[j], me[i] }
|
2014-08-28 06:04:41 +08:00
|
|
|
|
2014-09-11 18:31:31 +08:00
|
|
|
func (me *worstConns) Pop() (ret interface{}) {
|
2014-08-28 06:04:41 +08:00
|
|
|
old := *me
|
|
|
|
n := len(old)
|
|
|
|
ret = old[n-1]
|
|
|
|
*me = old[:n-1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-11 18:31:31 +08:00
|
|
|
func (me *worstConns) Push(x interface{}) {
|
2014-08-28 06:04:41 +08:00
|
|
|
*me = append(*me, x.(*connection))
|
|
|
|
}
|
2014-09-11 18:31:31 +08:00
|
|
|
|
|
|
|
func (me worstConns) key(i int) (ret time.Duration) {
|
|
|
|
c := me[i]
|
|
|
|
return time.Duration(1+c.UnwantedChunksReceived) * time.Now().Sub(func() time.Time {
|
|
|
|
if !c.lastUsefulChunkReceived.IsZero() {
|
|
|
|
return c.lastUsefulChunkReceived
|
|
|
|
}
|
|
|
|
return c.completedHandshake.Add(-time.Minute)
|
|
|
|
}()) / time.Duration(1+c.UsefulChunksReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me worstConns) Less(i, j int) bool {
|
|
|
|
return me.key(i) > me.key(j)
|
|
|
|
}
|