peerconn.go | 65 +++++------------------------------------------------ requesting.go | 2 +- torrent.go | 13 ++++++------- diff --git a/peerconn.go b/peerconn.go index dc172e6d336841faa38448716402d2be471aa07b..c1c02a8289237347ff58e53ea1044d90c0302799 100644 --- a/peerconn.go +++ b/peerconn.go @@ -17,7 +17,6 @@ "github.com/RoaringBitmap/roaring" "github.com/anacrolix/log" "github.com/anacrolix/missinggo/iter" "github.com/anacrolix/missinggo/v2/bitmap" - "github.com/anacrolix/missinggo/v2/prioritybitmap" "github.com/anacrolix/multiless" "github.com/anacrolix/chansync" @@ -126,9 +125,6 @@ PeerMaxRequests maxRequests // Maximum pending requests the peer allows. PeerExtensionIDs map[pp.ExtensionName]pp.ExtensionNumber PeerClientName string - - pieceInclination []int - _pieceRequestOrder prioritybitmap.PriorityBitmap logger log.Logger } @@ -404,8 +400,6 @@ func (p *Peer) close() { if !p.closed.Set() { return } - p.discardPieceInclination() - p._pieceRequestOrder.Clear() p.peerImpl.onClose() if p.t != nil { p.t.decPeerPieceAvailability(p) @@ -662,7 +656,6 @@ } func (cn *PeerConn) updateRequests() { if peerRequesting { - cn.nextRequestState = cn.getDesiredRequestState() cn.tickleWriter() return } @@ -692,54 +685,8 @@ } } } -// check callers updaterequests -func (cn *Peer) stopRequestingPiece(piece pieceIndex) bool { - return cn._pieceRequestOrder.Remove(piece) -} - -// This is distinct from Torrent piece priority, which is the user's -// preference. Connection piece priority is specific to a connection and is -// used to pseudorandomly avoid connections always requesting the same pieces -// and thus wasting effort. -func (cn *Peer) updatePiecePriority(piece pieceIndex) bool { - tpp := cn.t.piecePriority(piece) - if !cn.peerHasPiece(piece) { - tpp = PiecePriorityNone - } - if tpp == PiecePriorityNone { - return cn.stopRequestingPiece(piece) - } - prio := cn.getPieceInclination()[piece] - return cn._pieceRequestOrder.Set(piece, prio) -} - -func (cn *Peer) getPieceInclination() []int { - if cn.pieceInclination == nil { - cn.pieceInclination = cn.t.getConnPieceInclination() - } - return cn.pieceInclination -} - -func (cn *Peer) discardPieceInclination() { - if cn.pieceInclination == nil { - return - } - cn.t.putPieceInclination(cn.pieceInclination) - cn.pieceInclination = nil -} - func (cn *Peer) peerPiecesChanged() { - if cn.t.haveInfo() { - prioritiesChanged := false - for i := pieceIndex(0); i < cn.t.numPieces(); i++ { - if cn.updatePiecePriority(i) { - prioritiesChanged = true - } - } - if prioritiesChanged { - cn.updateRequests() - } - } + cn.updateRequests() cn.t.maybeDropMutuallyCompletePeer(cn) } @@ -761,10 +708,7 @@ if !cn.peerHasPiece(piece) { cn.t.incPieceAvailability(piece) } cn._peerPieces.Add(uint32(piece)) - cn.t.maybeDropMutuallyCompletePeer(&cn.Peer) - if cn.updatePiecePriority(piece) { - cn.updateRequests() - } + cn.peerPiecesChanged() return nil } @@ -1470,7 +1414,10 @@ return cn._stats.PiecesDirtiedGood.Int64() - cn._stats.PiecesDirtiedBad.Int64() } func (c *Peer) peerHasWantedPieces() bool { - return !c._pieceRequestOrder.IsEmpty() + // TODO: Can this be done just with AndCardinality? + missingPeerHas := c.newPeerPieces() + missingPeerHas.AndNot(&c.t._completedPieces) + return !missingPeerHas.IsEmpty() } func (c *Peer) deleteRequest(r RequestIndex) bool { diff --git a/requesting.go b/requesting.go index 4513ea3dfbbc904d6917aca3dd957bd26f3d6b19..c6dd6415de1a91b235e34faf1c20cfaa72333ef7 100644 --- a/requesting.go +++ b/requesting.go @@ -262,7 +262,7 @@ return } func (p *Peer) applyNextRequestState() bool { - next := p.nextRequestState + next := p.getDesiredRequestState() current := p.actualRequestState if !p.setInterested(next.Interested) { return false diff --git a/torrent.go b/torrent.go index 32b1d720d3a1ab266619c1d5bb83ac7894f83911..b99e4d18afe080522ee98bd7629cf755a9239934 100644 --- a/torrent.go +++ b/torrent.go @@ -923,7 +923,8 @@ return t.haveInfo() && t.pieceComplete(index) } func (t *Torrent) maybeDropMutuallyCompletePeer( - // I'm not sure about taking peer here, not all peer implementations actually drop. Maybe that's okay? + // I'm not sure about taking peer here, not all peer implementations actually drop. Maybe that's + // okay? p *Peer, ) { if !t.cl.config.DropMutuallyCompletePeers { @@ -1096,13 +1097,11 @@ t.openNewConns() } func (t *Torrent) piecePriorityChanged(piece pieceIndex) { - // t.logger.Printf("piece %d priority changed", piece) - t.iterPeers(func(c *Peer) { - if c.updatePiecePriority(piece) { - // log.Print("conn piece priority changed") + if true || t._pendingPieces.Contains(piece) { + t.iterPeers(func(c *Peer) { c.updateRequests() - } - }) + }) + } t.maybeNewConns() t.publishPieceChange(piece) }