Skip to content

Commit

Permalink
do not wait hash check before starting next piece download
Browse files Browse the repository at this point in the history
  • Loading branch information
cenkalti committed Feb 19, 2019
1 parent 80267ba commit e69eb3b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
3 changes: 3 additions & 0 deletions torrent/messagehandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (t *torrent) handlePieceMessage(pm peer.PieceMessage) {
t.closePieceDownloader(pd)
pe.StopSnubTimer()

// Request next piece while writing the completed piece, being optimistic about hash check.
t.startPieceDownloaderFor(pe)

if piece.Writing {
panic("piece is already writing")
}
Expand Down
24 changes: 6 additions & 18 deletions torrent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,21 @@ func (t *torrent) run() {
break
}

// Copy requested peers
requestedPeers := t.piecePicker.RequestedPeers(pw.Piece.Index)
peers := make([]*peer.Peer, len(requestedPeers))
for i := range requestedPeers {
peers[i] = requestedPeers[i]
pw.Piece.Done = true
if t.bitfield.Test(pw.Piece.Index) {
panic("already have the piece")
}
t.bitfield.Set(pw.Piece.Index)

if t.piecePicker != nil {
for _, pe := range peers {
for _, pe := range t.piecePicker.RequestedPeers(pw.Piece.Index) {
pd2 := t.pieceDownloaders[pe]
t.closePieceDownloader(pd2)
pd2.CancelPending()
t.startPieceDownloaderFor(pe)
}
}

pw.Piece.Done = true
if t.bitfield.Test(pw.Piece.Index) {
panic("already have the piece")
}
t.bitfield.Set(pw.Piece.Index)

// Tell everyone that we have this piece
for pe := range t.peers {
t.updateInterestedState(pe)
Expand All @@ -163,12 +157,6 @@ func (t *torrent) run() {
pe.SendMessage(msg)
}

// Start download on current peer first, then canceled peers
t.startPieceDownloaderFor(pw.Peer)
for _, p := range peers {
t.startPieceDownloaderFor(p)
}

completed := t.checkCompletion()
if t.resume != nil {
if completed {
Expand Down

0 comments on commit e69eb3b

Please sign in to comment.