PEX: impede full-meching in tracker-less swarms by adding a cooldown minute

This commit is contained in:
Yaroslav Kolomiiets 2020-12-14 16:04:45 +00:00 committed by Matt Joiner
parent f00f513706
commit d8d71addfc
2 changed files with 16 additions and 2 deletions

3
pex.go
View File

@ -3,6 +3,7 @@ package torrent
import ( import (
"net" "net"
"sync" "sync"
"time"
"github.com/anacrolix/dht/v2/krpc" "github.com/anacrolix/dht/v2/krpc"
pp "github.com/anacrolix/torrent/peer_protocol" pp "github.com/anacrolix/torrent/peer_protocol"
@ -179,6 +180,7 @@ func shortestIP(ip net.IP) net.IP {
type pexState struct { type pexState struct {
ev []pexEvent // event feed, append-only ev []pexEvent // event feed, append-only
hold []pexEvent // delayed drops hold []pexEvent // delayed drops
rest time.Time // cooldown deadline on inbound
nc int // net number of alive conns nc int // net number of alive conns
initCache pexMsgFactory // last generated initial message initCache pexMsgFactory // last generated initial message
initSeq int // number of events which went into initCache initSeq int // number of events which went into initCache
@ -190,6 +192,7 @@ func (s *pexState) Reset() {
s.ev = nil s.ev = nil
s.hold = nil s.hold = nil
s.nc = 0 s.nc = 0
s.rest = time.Time{}
s.initLock.Lock() s.initLock.Lock()
s.initCache = pexMsgFactory{} s.initCache = pexMsgFactory{}
s.initSeq = 0 s.initSeq = 0

View File

@ -88,6 +88,15 @@ func (s *pexConnState) Share(postfn messageWriter) bool {
// Recv is called from the reader goroutine // Recv is called from the reader goroutine
func (s *pexConnState) Recv(payload []byte) error { func (s *pexConnState) Recv(payload []byte) error {
if !s.torrent.wantPeers() {
s.dbg.Printf("peer reserve ok, incoming PEX discarded")
return nil
}
if time.Now().Before(s.torrent.pex.rest) {
s.dbg.Printf("in cooldown period, incoming PEX discarded")
return nil
}
rx, err := pp.LoadPexMsg(payload) rx, err := pp.LoadPexMsg(payload)
if err != nil { if err != nil {
return fmt.Errorf("error unmarshalling PEX message: %s", err) return fmt.Errorf("error unmarshalling PEX message: %s", err)
@ -100,8 +109,10 @@ func (s *pexConnState) Recv(payload []byte) error {
peers.AppendFromPex(rx.Added6, rx.Added6Flags) peers.AppendFromPex(rx.Added6, rx.Added6Flags)
peers.AppendFromPex(rx.Added, rx.AddedFlags) peers.AppendFromPex(rx.Added, rx.AddedFlags)
s.dbg.Printf("adding %d peers from PEX", len(peers)) s.dbg.Printf("adding %d peers from PEX", len(peers))
if len(peers) > 0 {
s.torrent.pex.rest = time.Now().Add(pexInterval)
s.torrent.addPeers(peers) s.torrent.addPeers(peers)
// s.dbg.Print("known swarm now:", s.torrent.KnownSwarm()) }
// one day we may also want to: // one day we may also want to:
// - check if the peer is not flooding us with PEX updates // - check if the peer is not flooding us with PEX updates