peer.go | 30 ++---------------------------- peerconn.go | 30 ++++++++++++++++++++++++++++-- torrent.go | 4 ++-- worse-conns.go | 11 ++--------- diff --git a/peer.go b/peer.go index d59c5c4c336ac594b871a48dafd489f9e114a382..151c4b143b7dcf854284adaf1ecba094bde6f158 100644 --- a/peer.go +++ b/peer.go @@ -6,7 +6,6 @@ "fmt" "io" "net" "strings" - "sync/atomic" "time" "github.com/RoaringBitmap/roaring" @@ -94,9 +93,7 @@ // Pieces we've accepted chunks for from the peer. peerTouchedPieces map[pieceIndex]struct{} peerAllowedFast typedRoaring.Bitmap[pieceIndex] - PeerMaxRequests maxRequests // Maximum pending requests the peer allows. - PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber - PeerClientName atomic.Value + PeerMaxRequests maxRequests // Maximum pending requests the peer allows. logger log.Logger } @@ -188,7 +185,7 @@ func (cn *Peer) locker() *lockWithDeferreds { return cn.t.cl.locker() } -func (cn *Peer) supportsExtension(ext pp.ExtensionName) bool { +func (cn *PeerConn) supportsExtension(ext pp.ExtensionName) bool { _, ok := cn.PeerExtensionIDs[ext] return ok } @@ -513,10 +510,6 @@ } } } -func (cn *Peer) peerPiecesChanged() { - cn.t.maybeDropMutuallyCompletePeer(cn) -} - // After handshake, we know what Torrent and Client stats to include for a // connection. func (cn *Peer) postHandshakeStats(f func(*ConnStats)) { @@ -537,25 +530,6 @@ } func (cn *Peer) readBytes(n int64) { cn.allStats(add(n, func(cs *ConnStats) *Count { return &cs.BytesRead })) -} - -// Returns whether the connection could be useful to us. We're seeding and -// they want data, we don't have metainfo and they can provide it, etc. -func (c *Peer) useful() bool { - t := c.t - if c.closed.IsSet() { - return false - } - if !t.haveInfo() { - return c.supportsExtension("ut_metadata") - } - if t.seeding() && c.peerInterested { - return true - } - if c.peerHasWantedPieces() { - return true - } - return false } func (c *Peer) lastHelpful() (ret time.Time) { diff --git a/peerconn.go b/peerconn.go index 68da20f1b1db29e54085fb31d7a7b97ab3d12134..ee7aa609d005bf8dbc17077919557b10f30269a8 100644 --- a/peerconn.go +++ b/peerconn.go @@ -12,6 +12,7 @@ "net" "net/netip" "strconv" "strings" + "sync/atomic" "time" "github.com/RoaringBitmap/roaring" @@ -54,8 +55,10 @@ r io.Reader messageWriter peerConnMsgWriter - uploadTimer *time.Timer - pex pexConnState + PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber + PeerClientName atomic.Value + uploadTimer *time.Timer + pex pexConnState // The pieces the peer has claimed to have. _peerPieces roaring.Bitmap @@ -1106,3 +1109,26 @@ func (pc *PeerConn) bitExtensionEnabled(bit pp.ExtensionBit) bool { return pc.t.cl.config.Extensions.GetBit(bit) && pc.PeerExtensionBytes.GetBit(bit) } + +func (cn *PeerConn) peerPiecesChanged() { + cn.t.maybeDropMutuallyCompletePeer(cn) +} + +// Returns whether the connection could be useful to us. We're seeding and +// they want data, we don't have metainfo and they can provide it, etc. +func (c *PeerConn) useful() bool { + t := c.t + if c.closed.IsSet() { + return false + } + if !t.haveInfo() { + return c.supportsExtension("ut_metadata") + } + if t.seeding() && c.peerInterested { + return true + } + if c.peerHasWantedPieces() { + return true + } + return false +} diff --git a/torrent.go b/torrent.go index 315f707236495a7a0834c8a520e643dd68e9846a..82dfc0a54acf9ccea2d952faf0409ef7037f6798 100644 --- a/torrent.go +++ b/torrent.go @@ -1060,7 +1060,7 @@ func (t *Torrent) maybeDropMutuallyCompletePeer( // I'm not sure about taking peer here, not all peer implementations actually drop. Maybe that's // okay? - p *Peer, + p *PeerConn, ) { if !t.cl.config.DropMutuallyCompletePeers { return @@ -2220,7 +2220,7 @@ t.cancelRequestsForPiece(piece) t.piece(piece).readerCond.Broadcast() for conn := range t.conns { conn.have(piece) - t.maybeDropMutuallyCompletePeer(&conn.Peer) + t.maybeDropMutuallyCompletePeer(conn) } } diff --git a/worse-conns.go b/worse-conns.go index 62895e69151cf3bbfffcd79f6616eee6a53df776..ef33b97902c4cd82f07b3a76f71f1f631a2ce821 100644 --- a/worse-conns.go +++ b/worse-conns.go @@ -34,7 +34,7 @@ type worseConnLensOpts struct { incomingIsBad, outgoingIsBad bool } -func worseConnInputFromPeer(p *Peer, opts worseConnLensOpts) worseConnInput { +func worseConnInputFromPeer(p *PeerConn, opts worseConnLensOpts) worseConnInput { ret := worseConnInput{ Useful: p.useful(), LastHelpful: p.lastHelpful(), @@ -48,13 +48,6 @@ } else if opts.outgoingIsBad && p.outgoing { ret.BadDirection = true } return ret -} - -func worseConn(_l, _r *Peer) bool { - // TODO: Use generics for ptr to - l := worseConnInputFromPeer(_l, worseConnLensOpts{}) - r := worseConnInputFromPeer(_r, worseConnLensOpts{}) - return l.Less(&r) } func (l *worseConnInput) Less(r *worseConnInput) bool { @@ -94,7 +87,7 @@ func (me *worseConnSlice) initKeys(opts worseConnLensOpts) { me.keys = make([]worseConnInput, len(me.conns)) for i, c := range me.conns { - me.keys[i] = worseConnInputFromPeer(&c.Peer, opts) + me.keys[i] = worseConnInputFromPeer(c, opts) } }