Reintroduce torrent-wide PEX throttling

https://github.com/anacrolix/torrent/discussions/836
This commit is contained in:
Matt Joiner 2023-05-24 12:11:10 +10:00
parent 98234f943f
commit 5380337f86
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
2 changed files with 15 additions and 2 deletions

9
pex.go
View File

@ -4,6 +4,7 @@ import (
"net" "net"
"net/netip" "net/netip"
"sync" "sync"
"time"
pp "github.com/anacrolix/torrent/peer_protocol" pp "github.com/anacrolix/torrent/peer_protocol"
) )
@ -144,8 +145,11 @@ func (me *pexMsgFactory) PexMsg() *pp.PexMsg {
// Per-torrent PEX state // Per-torrent PEX state
type pexState struct { type pexState struct {
sync.RWMutex sync.RWMutex
tail *pexEvent // event feed list tail *pexEvent // event feed list
hold []pexEvent // delayed drops hold []pexEvent // delayed drops
// Torrent-wide cooldown deadline on inbound. This exists to prevent PEX from drowning out other
// peer address sources, until that is fixed.
rest time.Time
nc int // net number of alive conns nc int // net number of alive conns
msg0 pexMsgFactory // initial message msg0 pexMsgFactory // initial message
} }
@ -157,6 +161,7 @@ func (s *pexState) Reset() {
s.tail = nil s.tail = nil
s.hold = nil s.hold = nil
s.nc = 0 s.nc = 0
s.rest = time.Time{}
s.msg0 = pexMsgFactory{} s.msg0 = pexMsgFactory{}
} }

View File

@ -143,9 +143,17 @@ func (s *pexConnState) Recv(payload []byte) error {
var peers peerInfos var peers peerInfos
peers.AppendFromPex(rx.Added6, rx.Added6Flags) peers.AppendFromPex(rx.Added6, rx.Added6Flags)
peers.AppendFromPex(rx.Added, rx.AddedFlags) peers.AppendFromPex(rx.Added, rx.AddedFlags)
if time.Now().Before(s.torrent.pex.rest) {
s.dbg.Printf("in cooldown period, incoming PEX discarded")
return nil
}
added := s.torrent.addPeers(peers) added := s.torrent.addPeers(peers)
s.dbg.Printf("got %v peers over pex, added %v", len(peers), added) s.dbg.Printf("got %v peers over pex, added %v", len(peers), added)
if len(peers) > 0 {
s.torrent.pex.rest = time.Now().Add(pexInterval)
}
// one day we may also want to: // one day we may also want to:
// - handle drops somehow // - handle drops somehow
// - detect malicious peers // - detect malicious peers