7 // Implements heap functions such that [0] is the worst connection.
8 type worstConns struct {
14 func (wc *worstConns) Len() int { return len(wc.c) }
15 func (wc *worstConns) Swap(i, j int) { wc.c[i], wc.c[j] = wc.c[j], wc.c[i] }
17 func (wc *worstConns) Pop() (ret interface{}) {
25 func (wc *worstConns) Push(x interface{}) {
26 wc.c = append(wc.c, x.(*connection))
29 type worstConnsSortKey struct {
35 func (wc worstConnsSortKey) Less(other worstConnsSortKey) bool {
36 if wc.useful != other.useful {
39 if !wc.lastHelpful.Equal(other.lastHelpful) {
40 return wc.lastHelpful.Before(other.lastHelpful)
42 return wc.connected.Before(other.connected)
45 func (wc *worstConns) key(i int) (key worstConnsSortKey) {
47 key.useful = wc.cl.usefulConn(wc.t, c)
48 if wc.cl.seeding(wc.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 (wc worstConns) Less(i, j int) bool {
61 return wc.key(i).Less(wc.key(j))