7 // Implements heap functions such that [0] is the worst connection.
8 type worstConns struct {
14 func (me *worstConns) Len() int { return len(me.c) }
15 func (me *worstConns) Swap(i, j int) { me.c[i], me.c[j] = me.c[j], me.c[i] }
17 func (me *worstConns) Pop() (ret interface{}) {
25 func (me *worstConns) Push(x interface{}) {
26 me.c = append(me.c, x.(*connection))
29 type worstConnsSortKey struct {
35 func (me worstConnsSortKey) Less(other worstConnsSortKey) bool {
36 if me.useful != other.useful {
39 if !me.lastHelpful.Equal(other.lastHelpful) {
40 return me.lastHelpful.Before(other.lastHelpful)
42 return me.connected.Before(other.connected)
45 func (me *worstConns) key(i int) (key worstConnsSortKey) {
47 key.useful = me.cl.usefulConn(me.t, c)
48 if me.cl.seeding(me.t) {
49 key.lastHelpful = c.lastChunkSent
51 // Intentionally consider the last time a chunk was received when seeding,
52 // because we might go from seeding back to leeching.
53 if c.lastUsefulChunkReceived.After(key.lastHelpful) {
54 key.lastHelpful = c.lastUsefulChunkReceived
56 key.connected = c.completedHandshake
60 func (me worstConns) Less(i, j int) bool {
61 return me.key(i).Less(me.key(j))