2014-08-28 06:04:41 +08:00
|
|
|
package torrent
|
|
|
|
|
2018-07-10 07:50:39 +08:00
|
|
|
import (
|
|
|
|
"container/heap"
|
|
|
|
"fmt"
|
2021-11-28 14:06:10 +08:00
|
|
|
"time"
|
2018-07-10 07:50:39 +08:00
|
|
|
"unsafe"
|
2019-12-23 11:04:07 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/multiless"
|
2021-11-29 07:07:48 +08:00
|
|
|
"github.com/anacrolix/sync"
|
2018-07-10 07:50:39 +08:00
|
|
|
)
|
2016-10-31 13:24:48 +08:00
|
|
|
|
2021-11-28 14:06:10 +08:00
|
|
|
type worseConnInput struct {
|
2021-11-29 07:07:48 +08:00
|
|
|
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)
|
2021-11-28 14:06:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func worseConnInputFromPeer(p *Peer) worseConnInput {
|
|
|
|
ret := worseConnInput{
|
|
|
|
Useful: p.useful(),
|
|
|
|
LastHelpful: p.lastHelpful(),
|
|
|
|
CompletedHandshake: p.completedHandshake,
|
|
|
|
Pointer: uintptr(unsafe.Pointer(p)),
|
2021-11-29 07:07:48 +08:00
|
|
|
GetPeerPriority: p.peerPriority,
|
2021-11-28 14:06:10 +08:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func worseConn(_l, _r *Peer) bool {
|
2021-11-29 07:07:48 +08:00
|
|
|
// TODO: Use generics for ptr to
|
|
|
|
l := worseConnInputFromPeer(_l)
|
|
|
|
r := worseConnInputFromPeer(_r)
|
|
|
|
return l.Less(&r)
|
2021-11-28 14:06:10 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:07:48 +08:00
|
|
|
func (l *worseConnInput) Less(r *worseConnInput) bool {
|
2019-12-23 11:04:07 +08:00
|
|
|
less, ok := multiless.New().Bool(
|
2021-11-28 14:06:10 +08:00
|
|
|
l.Useful, r.Useful).CmpInt64(
|
|
|
|
l.LastHelpful.Sub(r.LastHelpful).Nanoseconds()).CmpInt64(
|
|
|
|
l.CompletedHandshake.Sub(r.CompletedHandshake).Nanoseconds()).LazySameLess(
|
2020-04-13 12:08:32 +08:00
|
|
|
func() (same, less bool) {
|
2021-11-29 07:07:48 +08:00
|
|
|
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
|
2021-11-28 14:06:10 +08:00
|
|
|
return
|
2020-04-13 12:08:32 +08:00
|
|
|
}).Uintptr(
|
2021-11-28 14:06:10 +08:00
|
|
|
l.Pointer, r.Pointer,
|
2020-04-13 12:08:32 +08:00
|
|
|
).LessOk()
|
2018-07-10 07:50:39 +08:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("cannot differentiate %#v and %#v", l, r))
|
|
|
|
}
|
|
|
|
return less
|
2014-09-11 18:31:31 +08:00
|
|
|
}
|
2016-10-31 13:24:48 +08:00
|
|
|
|
|
|
|
type worseConnSlice struct {
|
2020-02-21 08:07:50 +08:00
|
|
|
conns []*PeerConn
|
2021-11-29 07:07:48 +08:00
|
|
|
keys []worseConnInput
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *worseConnSlice) initKeys() {
|
|
|
|
me.keys = make([]worseConnInput, len(me.conns))
|
|
|
|
for i, c := range me.conns {
|
|
|
|
me.keys[i] = worseConnInputFromPeer(&c.Peer)
|
|
|
|
}
|
2016-10-31 13:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ heap.Interface = &worseConnSlice{}
|
|
|
|
|
|
|
|
func (me worseConnSlice) Len() int {
|
|
|
|
return len(me.conns)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me worseConnSlice) Less(i, j int) bool {
|
2021-11-29 07:07:48 +08:00
|
|
|
return me.keys[i].Less(&me.keys[j])
|
2016-10-31 13:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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{}) {
|
2021-11-29 07:07:48 +08:00
|
|
|
panic("not implemented")
|
2016-10-31 13:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me worseConnSlice) Swap(i, j int) {
|
|
|
|
me.conns[i], me.conns[j] = me.conns[j], me.conns[i]
|
2021-11-29 07:07:48 +08:00
|
|
|
me.keys[i], me.keys[j] = me.keys[j], me.keys[i]
|
2016-10-31 13:24:48 +08:00
|
|
|
}
|