forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworse-conns.go
118 lines (102 loc) · 2.72 KB
/
worse-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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package torrent
import (
"container/heap"
"fmt"
"time"
"unsafe"
"github.com/anacrolix/multiless"
"github.com/anacrolix/sync"
)
type worseConnInput struct {
BadDirection bool
Useful bool
LastHelpful time.Time
CompletedHandshake time.Time
GetPeerPriority func() (peerPriority, error)
getPeerPriorityOnce sync.Once
peerPriority peerPriority
peerPriorityErr error
Pointer uintptr
}
func (me *worseConnInput) doGetPeerPriority() {
me.peerPriority, me.peerPriorityErr = me.GetPeerPriority()
}
func (me *worseConnInput) doGetPeerPriorityOnce() {
me.getPeerPriorityOnce.Do(me.doGetPeerPriority)
}
type worseConnLensOpts struct {
incomingIsBad, outgoingIsBad bool
}
func worseConnInputFromPeer(p *PeerConn, opts worseConnLensOpts) *worseConnInput {
ret := &worseConnInput{
Useful: p.useful(),
LastHelpful: p.lastHelpful(),
CompletedHandshake: p.completedHandshake,
Pointer: uintptr(unsafe.Pointer(p)),
GetPeerPriority: p.peerPriority,
}
if opts.incomingIsBad && !p.outgoing {
ret.BadDirection = true
} else if opts.outgoingIsBad && p.outgoing {
ret.BadDirection = true
}
return ret
}
func (l *worseConnInput) Less(r *worseConnInput) bool {
less, ok := multiless.New().Bool(
r.BadDirection, l.BadDirection).Bool(
l.Useful, r.Useful).CmpInt64(
l.LastHelpful.Sub(r.LastHelpful).Nanoseconds()).CmpInt64(
l.CompletedHandshake.Sub(r.CompletedHandshake).Nanoseconds()).LazySameLess(
func() (same, less bool) {
l.doGetPeerPriorityOnce()
if l.peerPriorityErr != nil {
same = true
return
}
r.doGetPeerPriorityOnce()
if r.peerPriorityErr != nil {
same = true
return
}
same = l.peerPriority == r.peerPriority
less = l.peerPriority < r.peerPriority
return
}).Uintptr(
l.Pointer, r.Pointer,
).LessOk()
if !ok {
panic(fmt.Sprintf("cannot differentiate %#v and %#v", l, r))
}
return less
}
type worseConnSlice struct {
conns []*PeerConn
keys []*worseConnInput
}
func (me *worseConnSlice) initKeys(opts worseConnLensOpts) {
me.keys = make([]*worseConnInput, len(me.conns))
for i, c := range me.conns {
me.keys[i] = worseConnInputFromPeer(c, opts)
}
}
var _ heap.Interface = (*worseConnSlice)(nil)
func (me *worseConnSlice) Len() int {
return len(me.conns)
}
func (me *worseConnSlice) Less(i, j int) bool {
return me.keys[i].Less(me.keys[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{}) {
panic("not implemented")
}
func (me *worseConnSlice) Swap(i, j int) {
me.conns[i], me.conns[j] = me.conns[j], me.conns[i]
me.keys[i], me.keys[j] = me.keys[j], me.keys[i]
}