FedP2P/worst_conns.go

43 lines
870 B
Go
Raw Normal View History

package torrent
import "container/heap"
2016-07-05 22:42:16 +08:00
func worseConn(l, r *connection) bool {
if l.useful() != r.useful() {
return r.useful()
}
2016-07-05 22:42:16 +08:00
if !l.lastHelpful().Equal(r.lastHelpful()) {
return l.lastHelpful().Before(r.lastHelpful())
}
2016-07-05 22:42:16 +08:00
return l.completedHandshake.Before(r.completedHandshake)
}
type worseConnSlice struct {
conns []*connection
}
var _ heap.Interface = &worseConnSlice{}
func (me worseConnSlice) Len() int {
return len(me.conns)
}
func (me worseConnSlice) Less(i, j int) bool {
return worseConn(me.conns[i], me.conns[j])
}
func (me *worseConnSlice) Pop() interface{} {
i := len(me.conns) - 1
ret := me.conns[i]
me.conns = me.conns[:i]
return ret
}
func (me *worseConnSlice) Push(x interface{}) {
me.conns = append(me.conns, x.(*connection))
}
func (me worseConnSlice) Swap(i, j int) {
me.conns[i], me.conns[j] = me.conns[j], me.conns[i]
}