Skip to content

Instantly share code, notes, and snippets.

@anacrolix
Created June 25, 2018 13:59
Show Gist options
  • Save anacrolix/f04e62f4e14761ebb0194a75985b8c17 to your computer and use it in GitHub Desktop.
Save anacrolix/f04e62f4e14761ebb0194a75985b8c17 to your computer and use it in GitHub Desktop.
func (cn *connection) iterUnbiasedPieceRequestOrder(f func(piece int) bool) bool {
now, readahead := cn.t.readerPiecePriorities()
var skip bitmap.Bitmap
if !cn.peerSentHaveAll {
// Pieces to skip include pieces the peer doesn't have.
skip = bitmap.Flip(cn.peerPieces, 0, cn.t.numPieces())
}
// And pieces that we already have.
skip.Union(cn.t.completedPieces)
skip.Union(cn.t.piecesQueuedForHash)
// Return an iterator over the different priority classes, minus the skip
// pieces.
return iter.All(
func(_piece interface{}) bool {
i := _piece.(pieceIndex)
if cn.t.hashingPiece(i) {
return true
}
return f(i)
},
iterBitmapsDistinct(&skip, now, readahead),
func(cb iter.Callback) {
cn.t.pendingPieces.IterTyped(func(piece int) bool {
if skip.Contains(piece) {
return true
}
more := cb(piece)
skip.Add(piece)
return more
})
},
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment