forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worst_conns.go
42 lines (33 loc) · 870 Bytes
/
worst_conns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package torrent
import "container/heap"
func worseConn(l, r *connection) bool {
if l.useful() != r.useful() {
return r.useful()
}
if !l.lastHelpful().Equal(r.lastHelpful()) {
return l.lastHelpful().Before(r.lastHelpful())
}
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]
}