From 2be5a95b067d9a805ef578d3646cf84254af6ab1 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 9 Oct 2021 09:14:57 +1100 Subject: [PATCH] Add reasons for updateRequests to be triggered --- client_test.go | 2 +- file.go | 2 +- piece.go | 2 +- t.go | 10 +++++----- torrent.go | 36 ++++++++++++++++++------------------ torrent_test.go | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/client_test.go b/client_test.go index 414aac06..7a04c591 100644 --- a/client_test.go +++ b/client_test.go @@ -483,7 +483,7 @@ func testDownloadCancel(t *testing.T, ps testDownloadCancelParams) { leecherGreeting.cl.lock() leecherGreeting.downloadPiecesLocked(0, leecherGreeting.numPieces()) if ps.Cancel { - leecherGreeting.cancelPiecesLocked(0, leecherGreeting.NumPieces()) + leecherGreeting.cancelPiecesLocked(0, leecherGreeting.NumPieces(), "") } leecherGreeting.cl.unlock() done := make(chan struct{}) diff --git a/file.go b/file.go index 35fe9239..01515187 100644 --- a/file.go +++ b/file.go @@ -149,7 +149,7 @@ func (f *File) SetPriority(prio piecePriority) { f.t.cl.lock() if prio != f.prio { f.prio = prio - f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex()) + f.t.updatePiecePriorities(f.firstPieceIndex(), f.endPieceIndex(), "File.SetPriority") } f.t.cl.unlock() } diff --git a/piece.go b/piece.go index 20c5c19a..411b187c 100644 --- a/piece.go +++ b/piece.go @@ -191,7 +191,7 @@ func (p *Piece) SetPriority(prio piecePriority) { p.t.cl.lock() defer p.t.cl.unlock() p.priority = prio - p.t.updatePiecePriority(p.index) + p.t.updatePiecePriority(p.index, "Piece.SetPriority") } func (p *Piece) purePriority() (ret piecePriority) { diff --git a/t.go b/t.go index 042a5262..78c500dc 100644 --- a/t.go +++ b/t.go @@ -185,25 +185,25 @@ func (t *Torrent) DownloadPieces(begin, end pieceIndex) { func (t *Torrent) downloadPiecesLocked(begin, end pieceIndex) { for i := begin; i < end; i++ { if t.pieces[i].priority.Raise(PiecePriorityNormal) { - t.updatePiecePriority(i) + t.updatePiecePriority(i, "Torrent.DownloadPieces") } } } -func (t *Torrent) CancelPieces(begin, end pieceIndex) { +func (t *Torrent) CancelPieces(begin, end pieceIndex, reason string) { t.cl.lock() - t.cancelPiecesLocked(begin, end) + t.cancelPiecesLocked(begin, end, "Torrent.CancelPieces") t.cl.unlock() } -func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex) { +func (t *Torrent) cancelPiecesLocked(begin, end pieceIndex, reason string) { for i := begin; i < end; i++ { p := &t.pieces[i] if p.priority == PiecePriorityNone { continue } p.priority = PiecePriorityNone - t.updatePiecePriority(i) + t.updatePiecePriority(i, reason) } } diff --git a/torrent.go b/torrent.go index a89a114e..b2596be3 100644 --- a/torrent.go +++ b/torrent.go @@ -1060,7 +1060,7 @@ func (t *Torrent) pieceAllDirty(piece pieceIndex) bool { func (t *Torrent) readersChanged() { t.updateReaderPieces() - t.updateAllPiecePriorities() + t.updateAllPiecePriorities("Torrent.readersChanged") } func (t *Torrent) updateReaderPieces() { @@ -1079,15 +1079,15 @@ func (t *Torrent) readerPosChanged(from, to pieceRange) { } if l.end < h.begin { // Two distinct ranges. - t.updatePiecePriorities(l.begin, l.end) - t.updatePiecePriorities(h.begin, h.end) + t.updatePiecePriorities(l.begin, l.end, "Torrent.readerPosChanged") + t.updatePiecePriorities(h.begin, h.end, "Torrent.readerPosChanged") } else { // Ranges overlap. end := l.end if h.end > end { end = h.end } - t.updatePiecePriorities(l.begin, end) + t.updatePiecePriorities(l.begin, end, "Torrent.readerPosChanged") } } @@ -1097,17 +1097,17 @@ func (t *Torrent) maybeNewConns() { t.openNewConns() } -func (t *Torrent) piecePriorityChanged(piece pieceIndex) { +func (t *Torrent) piecePriorityChanged(piece pieceIndex, reason string) { if t._pendingPieces.Contains(piece) { t.iterPeers(func(c *Peer) { - c.updateRequests("piece priority changed") + c.updateRequests(reason) }) } t.maybeNewConns() t.publishPieceChange(piece) } -func (t *Torrent) updatePiecePriority(piece pieceIndex) { +func (t *Torrent) updatePiecePriority(piece pieceIndex, reason string) { p := &t.pieces[piece] newPrio := p.uncachedPriority() // t.logger.Printf("torrent %p: piece %d: uncached priority: %v", t, piece, newPrio) @@ -1120,18 +1120,18 @@ func (t *Torrent) updatePiecePriority(piece pieceIndex) { return } } - t.piecePriorityChanged(piece) + t.piecePriorityChanged(piece, reason) } -func (t *Torrent) updateAllPiecePriorities() { - t.updatePiecePriorities(0, t.numPieces()) +func (t *Torrent) updateAllPiecePriorities(reason string) { + t.updatePiecePriorities(0, t.numPieces(), reason) } // Update all piece priorities in one hit. This function should have the same // output as updatePiecePriority, but across all pieces. -func (t *Torrent) updatePiecePriorities(begin, end pieceIndex) { +func (t *Torrent) updatePiecePriorities(begin, end pieceIndex, reason string) { for i := begin; i < end; i++ { - t.updatePiecePriority(i) + t.updatePiecePriority(i, reason) } } @@ -1190,14 +1190,14 @@ func (t *Torrent) pendRequest(req RequestIndex) { t.piece(int(req / t.chunksPerRegularPiece())).pendChunkIndex(req % t.chunksPerRegularPiece()) } -func (t *Torrent) pieceCompletionChanged(piece pieceIndex) { +func (t *Torrent) pieceCompletionChanged(piece pieceIndex, reason string) { t.cl.event.Broadcast() if t.pieceComplete(piece) { t.onPieceCompleted(piece) } else { t.onIncompletePiece(piece) } - t.updatePiecePriority(piece) + t.updatePiecePriority(piece, reason) } func (t *Torrent) numReceivedConns() (ret int) { @@ -1275,7 +1275,7 @@ func (t *Torrent) updatePieceCompletion(piece pieceIndex) bool { } if changed { log.Fstr("piece %d completion changed: %+v -> %+v", piece, cached, uncached).SetLevel(log.Debug).Log(t.logger) - t.pieceCompletionChanged(piece) + t.pieceCompletionChanged(piece, "Torrent.updatePieceCompletion") } return changed } @@ -2030,7 +2030,7 @@ func (t *Torrent) tryCreatePieceHasher() bool { t.piecesQueuedForHash.Remove(bitmap.BitIndex(pi)) p.hashing = true t.publishPieceChange(pi) - t.updatePiecePriority(pi) + t.updatePiecePriority(pi, "Torrent.tryCreatePieceHasher") t.storageLock.RLock() t.activePieceHashes++ go t.pieceHasher(pi) @@ -2063,7 +2063,7 @@ func (t *Torrent) pieceHasher(index pieceIndex) { defer t.cl.unlock() p.hashing = false t.pieceHashed(index, correct, copyErr) - t.updatePiecePriority(index) + t.updatePiecePriority(index, "Torrent.pieceHasher") t.activePieceHashes-- t.tryCreateMorePieceHashers() } @@ -2091,7 +2091,7 @@ func (t *Torrent) queuePieceCheck(pieceIndex pieceIndex) { } t.piecesQueuedForHash.Add(bitmap.BitIndex(pieceIndex)) t.publishPieceChange(pieceIndex) - t.updatePiecePriority(pieceIndex) + t.updatePiecePriority(pieceIndex, "Torrent.queuePieceCheck") t.tryCreateMorePieceHashers() } diff --git a/torrent_test.go b/torrent_test.go index 64d90b69..02e56e2d 100644 --- a/torrent_test.go +++ b/torrent_test.go @@ -101,7 +101,7 @@ func BenchmarkUpdatePiecePriorities(b *testing.B) { } t.DownloadPieces(0, t.numPieces()) for i := 0; i < b.N; i += 1 { - t.updateAllPiecePriorities() + t.updateAllPiecePriorities("") } }