Use new anacrolix/multiless, and incorporate Peer.Trusted into peer prioritization

This commit is contained in:
Matt Joiner 2019-12-23 14:04:07 +11:00
parent cf85d70a5c
commit d4e90519e6
7 changed files with 29 additions and 28 deletions

View File

@ -32,7 +32,7 @@ type ClientConfig struct {
ListenHost func(network string) string
ListenPort int
NoDefaultPortForwarding bool
UpnpID string
UpnpID string
// Don't announce to trackers. This only leaves DHT to discover peers.
DisableTrackers bool `long:"disable-trackers"`
DisablePEX bool `long:"disable-pex"`
@ -152,7 +152,7 @@ func NewDefaultClientConfig() *ClientConfig {
HTTPUserAgent: DefaultHTTPUserAgent,
ExtendedHandshakeClientVersion: "go.torrent dev 20181121",
Bep20: "-GT0002-",
UpnpID: "anacrolix/torrent",
UpnpID: "anacrolix/torrent",
NominalDialTimeout: 20 * time.Second,
MinDialTimeout: 3 * time.Second,
EstablishedConnsPerTorrent: 50,

View File

@ -19,6 +19,7 @@ import (
"github.com/anacrolix/missinggo/bitmap"
"github.com/anacrolix/missinggo/iter"
"github.com/anacrolix/missinggo/prioritybitmap"
"github.com/anacrolix/multiless"
"github.com/pkg/errors"
"github.com/anacrolix/torrent/bencode"
@ -1580,8 +1581,5 @@ type connectionTrust struct {
}
func (l connectionTrust) Less(r connectionTrust) bool {
var ml missinggo.MultiLess
ml.NextBool(!l.Implicit, !r.Implicit)
ml.StrictNext(l.NetGoodPiecesDirted == r.NetGoodPiecesDirted, l.NetGoodPiecesDirted < r.NetGoodPiecesDirted)
return ml.Less()
return multiless.New().Bool(l.Implicit, r.Implicit).Int64(l.NetGoodPiecesDirted, r.NetGoodPiecesDirted).Less()
}

1
go.mod
View File

@ -11,6 +11,7 @@ require (
github.com/anacrolix/missinggo v1.2.1
github.com/anacrolix/missinggo/perf v1.0.0
github.com/anacrolix/missinggo/v2 v2.3.1
github.com/anacrolix/multiless v0.0.0-20191223025854-070b7994e841
github.com/anacrolix/sync v0.2.0
github.com/anacrolix/tagflag v1.0.1
github.com/anacrolix/upnp v0.1.1

2
go.sum
View File

@ -69,6 +69,8 @@ github.com/anacrolix/mmsg v0.0.0-20180515031531-a4a3ba1fc8bb h1:2Or5ccMoY4Kfao+W
github.com/anacrolix/mmsg v0.0.0-20180515031531-a4a3ba1fc8bb/go.mod h1:x2/ErsYUmT77kezS63+wzZp8E3byYB0gzirM/WMBLfw=
github.com/anacrolix/mmsg v1.0.0 h1:btC7YLjOn29aTUAExJiVUhQOuf/8rhm+/nWCMAnL3Hg=
github.com/anacrolix/mmsg v1.0.0/go.mod h1:x8kRaJY/dCrY9Al0PEcj1mb/uFHwP6GCJ9fLl4thEPc=
github.com/anacrolix/multiless v0.0.0-20191223025854-070b7994e841 h1:AIQdjlpS6GLF/OrfLjWXTEneg7ZnXnlblcbAP83PJTk=
github.com/anacrolix/multiless v0.0.0-20191223025854-070b7994e841/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4=
github.com/anacrolix/stm v0.1.0 h1:B/Kt3e4+0uqJoLcNZFW69cCBASok6WxX9CEhz9PqIPM=
github.com/anacrolix/stm v0.1.0/go.mod h1:ZKz7e7ERWvP0KgL7WXfRjBXHNRhlVRlbBQecqFtPq+A=
github.com/anacrolix/stm v0.1.1-0.20191106051447-e749ba3531cf/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg=

View File

@ -1,6 +1,9 @@
package torrent
import "github.com/google/btree"
import (
"github.com/anacrolix/multiless"
"github.com/google/btree"
)
// Peers are stored with their priority at insertion. Their priority may
// change if our apparent IP changes, we don't currently handle that.
@ -10,7 +13,11 @@ type prioritizedPeersItem struct {
}
func (me prioritizedPeersItem) Less(than btree.Item) bool {
return me.prio < than.(prioritizedPeersItem).prio
other := than.(prioritizedPeersItem)
return multiless.New().Bool(
me.p.Trusted, other.p.Trusted).Uint32(
me.prio, other.prio,
).Less()
}
type prioritizedPeers struct {

View File

@ -2,7 +2,6 @@ package torrent
import (
"net"
"sort"
"testing"
"github.com/google/btree"
@ -23,16 +22,14 @@ func TestPrioritizedPeers(t *testing.T) {
{IP: net.ParseIP("1.2.3.4")},
{IP: net.ParseIP("1::2")},
{IP: net.ParseIP("")},
{IP: net.ParseIP(""), Trusted: true},
}
for i, p := range ps {
t.Logf("peer %d priority: %08x\n", i, pp.getPrio(p))
t.Logf("peer %d priority: %08x trusted: %t\n", i, pp.getPrio(p), p.Trusted)
assert.False(t, pp.Add(p))
assert.True(t, pp.Add(p))
assert.Equal(t, i+1, pp.Len())
}
sort.Slice(ps, func(i, j int) bool {
return pp.getPrio(ps[i]) < pp.getPrio(ps[j])
})
pop := func(expected *Peer) {
if expected == nil {
assert.Panics(t, func() { pp.PopMax() })
@ -49,9 +46,10 @@ func TestPrioritizedPeers(t *testing.T) {
assert.Equal(t, *expected, i.p)
}
}
pop(&ps[2])
min(&ps[0])
pop(&ps[3])
pop(&ps[1])
min(&ps[2])
pop(&ps[0])
min(nil)
pop(nil)
}

View File

@ -4,22 +4,17 @@ import (
"container/heap"
"fmt"
"unsafe"
"github.com/anacrolix/multiless"
)
func worseConn(l, r *connection) bool {
var ml multiLess
ml.NextBool(!l.useful(), !r.useful())
ml.StrictNext(
l.lastHelpful().Equal(r.lastHelpful()),
l.lastHelpful().Before(r.lastHelpful()))
ml.StrictNext(
l.completedHandshake.Equal(r.completedHandshake),
l.completedHandshake.Before(r.completedHandshake))
ml.Next(func() (bool, bool) {
return l.peerPriority() == r.peerPriority(), l.peerPriority() < r.peerPriority()
})
ml.StrictNext(l == r, uintptr(unsafe.Pointer(l)) < uintptr(unsafe.Pointer(r)))
less, ok := ml.FinalOk()
less, ok := multiless.New().Bool(
l.useful(), r.useful()).CmpInt64(
l.lastHelpful().Sub(r.lastHelpful()).Nanoseconds()).CmpInt64(
l.completedHandshake.Sub(r.completedHandshake).Nanoseconds()).Uint32(
l.peerPriority(), r.peerPriority()).Uintptr(
uintptr(unsafe.Pointer(l)), uintptr(unsafe.Pointer(r))).LessOk()
if !ok {
panic(fmt.Sprintf("cannot differentiate %#v and %#v", l, r))
}