2020-01-10 04:09:21 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2020-01-24 06:55:20 +00:00
|
|
|
"math"
|
2020-01-20 00:51:24 +00:00
|
|
|
"sync"
|
2020-01-10 04:09:21 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/anacrolix/missinggo/v2/bitmap"
|
|
|
|
"github.com/anacrolix/missinggo/v2/prioritybitmap"
|
|
|
|
|
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
type requestStrategyPiece interface {
|
|
|
|
numChunks() pp.Integer
|
|
|
|
dirtyChunks() bitmap.Bitmap
|
2021-01-28 03:23:22 +00:00
|
|
|
chunkIndexRequest(i pp.Integer) Request
|
2020-01-10 04:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategyTorrent interface {
|
|
|
|
numConns() int
|
|
|
|
numReaders() int
|
|
|
|
numPieces() int
|
|
|
|
readerPiecePriorities() (now, readahead bitmap.Bitmap)
|
|
|
|
ignorePieces() bitmap.Bitmap
|
|
|
|
pendingPieces() *prioritybitmap.PriorityBitmap
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategyConnection interface {
|
|
|
|
torrent() requestStrategyTorrent
|
|
|
|
peerPieces() bitmap.Bitmap
|
|
|
|
pieceRequestOrder() *prioritybitmap.PriorityBitmap
|
|
|
|
fastest() bool
|
|
|
|
stats() *ConnStats
|
|
|
|
totalExpectingTime() time.Duration
|
|
|
|
peerMaxRequests() int
|
|
|
|
chunksReceivedWhileExpecting() int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategy interface {
|
|
|
|
iterPendingPieces(requestStrategyConnection, func(pieceIndex) bool) bool
|
2021-01-28 03:23:22 +00:00
|
|
|
iterUndirtiedChunks(requestStrategyPiece, func(ChunkSpec) bool) bool
|
2020-01-10 04:09:21 +00:00
|
|
|
nominalMaxRequests(requestStrategyConnection) int
|
|
|
|
shouldRequestWithoutBias(requestStrategyConnection) bool
|
|
|
|
piecePriority(requestStrategyConnection, pieceIndex, piecePriority, int) int
|
2020-01-10 05:18:55 +00:00
|
|
|
hooks() requestStrategyHooks
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategyHooks struct {
|
2021-01-28 03:23:22 +00:00
|
|
|
sentRequest func(Request)
|
|
|
|
deletedRequest func(Request)
|
2020-01-10 05:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategyCallbacks interface {
|
2021-01-28 03:23:22 +00:00
|
|
|
requestTimedOut(Request)
|
2020-01-10 04:09:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 06:30:57 +00:00
|
|
|
type requestStrategyFuzzing struct {
|
2020-01-24 04:09:39 +00:00
|
|
|
requestStrategyDefaults
|
|
|
|
}
|
2020-01-10 04:09:21 +00:00
|
|
|
|
2020-01-24 06:30:57 +00:00
|
|
|
type requestStrategyFastest struct {
|
2020-01-24 04:09:39 +00:00
|
|
|
requestStrategyDefaults
|
|
|
|
}
|
2020-01-10 04:09:21 +00:00
|
|
|
|
2020-11-08 23:56:27 +00:00
|
|
|
func newRequestStrategyMaker(rs requestStrategy) requestStrategyMaker {
|
2020-01-24 06:30:57 +00:00
|
|
|
return func(requestStrategyCallbacks, sync.Locker) requestStrategy {
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 03:43:21 +00:00
|
|
|
// The fastest connection downloads strictly in order of priority, while all others adhere to their
|
|
|
|
// piece inclinations.
|
2020-11-08 23:56:27 +00:00
|
|
|
func RequestStrategyFastest() requestStrategyMaker {
|
2020-01-24 06:30:57 +00:00
|
|
|
return newRequestStrategyMaker(requestStrategyFastest{})
|
|
|
|
}
|
|
|
|
|
2020-12-19 03:43:21 +00:00
|
|
|
// Favour higher priority pieces with some fuzzing to reduce overlaps and wastage across
|
|
|
|
// connections.
|
2020-11-08 23:56:27 +00:00
|
|
|
func RequestStrategyFuzzing() requestStrategyMaker {
|
2020-01-24 06:30:57 +00:00
|
|
|
return newRequestStrategyMaker(requestStrategyFuzzing{})
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:24:28 +00:00
|
|
|
func (requestStrategyFastest) shouldRequestWithoutBias(cn requestStrategyConnection) bool {
|
2020-01-10 04:09:21 +00:00
|
|
|
if cn.torrent().numReaders() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if cn.torrent().numConns() == 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if cn.fastest() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:30:57 +00:00
|
|
|
type requestStrategyDuplicateRequestTimeout struct {
|
2020-01-24 08:24:28 +00:00
|
|
|
requestStrategyDefaults
|
2020-01-10 05:18:55 +00:00
|
|
|
// How long to avoid duplicating a pending request.
|
|
|
|
duplicateRequestTimeout time.Duration
|
2020-01-20 00:51:24 +00:00
|
|
|
|
|
|
|
callbacks requestStrategyCallbacks
|
|
|
|
|
2020-01-10 05:18:55 +00:00
|
|
|
// The last time we requested a chunk. Deleting the request from any connection will clear this
|
|
|
|
// value.
|
2021-01-28 03:23:22 +00:00
|
|
|
lastRequested map[Request]*time.Timer
|
2020-01-20 00:51:24 +00:00
|
|
|
// The lock to take when running a request timeout handler.
|
|
|
|
timeoutLocker sync.Locker
|
2020-01-10 05:18:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 23:56:27 +00:00
|
|
|
// Generates a request strategy instance for a given torrent. callbacks are probably specific to the torrent.
|
|
|
|
type requestStrategyMaker func(callbacks requestStrategyCallbacks, clientLocker sync.Locker) requestStrategy
|
2020-01-10 05:18:55 +00:00
|
|
|
|
2020-12-19 03:43:21 +00:00
|
|
|
// Requests are strictly by piece priority, and not duplicated until duplicateRequestTimeout is
|
|
|
|
// reached.
|
2020-11-08 23:56:27 +00:00
|
|
|
func RequestStrategyDuplicateRequestTimeout(duplicateRequestTimeout time.Duration) requestStrategyMaker {
|
2020-01-20 00:51:24 +00:00
|
|
|
return func(callbacks requestStrategyCallbacks, clientLocker sync.Locker) requestStrategy {
|
2020-01-24 06:30:57 +00:00
|
|
|
return requestStrategyDuplicateRequestTimeout{
|
2020-01-10 05:18:55 +00:00
|
|
|
duplicateRequestTimeout: duplicateRequestTimeout,
|
|
|
|
callbacks: callbacks,
|
2021-01-28 03:23:22 +00:00
|
|
|
lastRequested: make(map[Request]*time.Timer),
|
2020-01-20 00:51:24 +00:00
|
|
|
timeoutLocker: clientLocker,
|
2020-01-10 05:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:30:57 +00:00
|
|
|
func (rs requestStrategyDuplicateRequestTimeout) hooks() requestStrategyHooks {
|
2020-01-10 05:18:55 +00:00
|
|
|
return requestStrategyHooks{
|
2021-01-28 03:23:22 +00:00
|
|
|
deletedRequest: func(r Request) {
|
2020-01-10 05:18:55 +00:00
|
|
|
if t, ok := rs.lastRequested[r]; ok {
|
|
|
|
t.Stop()
|
|
|
|
delete(rs.lastRequested, r)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sentRequest: rs.onSentRequest,
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 06:55:20 +00:00
|
|
|
|
2021-01-28 03:23:22 +00:00
|
|
|
func (rs requestStrategyDuplicateRequestTimeout) iterUndirtiedChunks(p requestStrategyPiece, f func(ChunkSpec) bool) bool {
|
2020-01-24 06:55:20 +00:00
|
|
|
for i := pp.Integer(0); i < pp.Integer(p.numChunks()); i++ {
|
|
|
|
if p.dirtyChunks().Get(bitmap.BitIndex(i)) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r := p.chunkIndexRequest(i)
|
|
|
|
if rs.wouldDuplicateRecent(r) {
|
|
|
|
continue
|
|
|
|
}
|
2021-01-28 03:23:22 +00:00
|
|
|
if !f(r.ChunkSpec) {
|
2020-01-24 06:55:20 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (requestStrategyFuzzing) piecePriority(cn requestStrategyConnection, piece pieceIndex, tpp piecePriority, prio int) int {
|
|
|
|
switch tpp {
|
|
|
|
case PiecePriorityNormal:
|
|
|
|
case PiecePriorityReadahead:
|
|
|
|
prio -= int(cn.torrent().numPieces())
|
|
|
|
case PiecePriorityNext, PiecePriorityNow:
|
|
|
|
prio -= 2 * int(cn.torrent().numPieces())
|
|
|
|
default:
|
|
|
|
panic(tpp)
|
|
|
|
}
|
|
|
|
prio += int(piece / 3)
|
|
|
|
return prio
|
|
|
|
}
|
|
|
|
|
|
|
|
func (requestStrategyDuplicateRequestTimeout) iterPendingPieces(cn requestStrategyConnection, f func(pieceIndex) bool) bool {
|
|
|
|
return iterUnbiasedPieceRequestOrder(cn, f)
|
|
|
|
}
|
|
|
|
func defaultIterPendingPieces(rs requestStrategy, cn requestStrategyConnection, f func(pieceIndex) bool) bool {
|
|
|
|
if rs.shouldRequestWithoutBias(cn) {
|
|
|
|
return iterUnbiasedPieceRequestOrder(cn, f)
|
|
|
|
} else {
|
|
|
|
return cn.pieceRequestOrder().IterTyped(func(i int) bool {
|
|
|
|
return f(pieceIndex(i))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (rs requestStrategyFuzzing) iterPendingPieces(cn requestStrategyConnection, cb func(pieceIndex) bool) bool {
|
|
|
|
return defaultIterPendingPieces(rs, cn, cb)
|
|
|
|
}
|
|
|
|
func (rs requestStrategyFastest) iterPendingPieces(cn requestStrategyConnection, cb func(pieceIndex) bool) bool {
|
|
|
|
return defaultIterPendingPieces(rs, cn, cb)
|
|
|
|
}
|
|
|
|
|
2021-01-28 03:23:22 +00:00
|
|
|
func (rs requestStrategyDuplicateRequestTimeout) onSentRequest(r Request) {
|
2020-01-24 06:55:20 +00:00
|
|
|
rs.lastRequested[r] = time.AfterFunc(rs.duplicateRequestTimeout, func() {
|
|
|
|
rs.timeoutLocker.Lock()
|
|
|
|
delete(rs.lastRequested, r)
|
|
|
|
rs.timeoutLocker.Unlock()
|
|
|
|
rs.callbacks.requestTimedOut(r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// The actual value to use as the maximum outbound requests.
|
|
|
|
func (rs requestStrategyDuplicateRequestTimeout) nominalMaxRequests(cn requestStrategyConnection) (ret int) {
|
|
|
|
expectingTime := int64(cn.totalExpectingTime())
|
|
|
|
if expectingTime == 0 {
|
|
|
|
expectingTime = math.MaxInt64
|
|
|
|
} else {
|
|
|
|
expectingTime *= 2
|
|
|
|
}
|
|
|
|
return int(clamp(
|
|
|
|
1,
|
|
|
|
int64(cn.peerMaxRequests()),
|
|
|
|
max(
|
|
|
|
// It makes sense to always pipeline at least one connection, since latency must be
|
|
|
|
// non-zero.
|
|
|
|
2,
|
|
|
|
// Request only as many as we expect to receive in the duplicateRequestTimeout
|
|
|
|
// window. We are trying to avoid having to duplicate requests.
|
|
|
|
cn.chunksReceivedWhileExpecting()*int64(rs.duplicateRequestTimeout)/expectingTime,
|
|
|
|
),
|
|
|
|
))
|
|
|
|
}
|
2021-01-28 03:23:22 +00:00
|
|
|
func (rs requestStrategyDuplicateRequestTimeout) wouldDuplicateRecent(r Request) bool {
|
2020-01-24 06:55:20 +00:00
|
|
|
// This piece has been requested on another connection, and the duplicate request timer is still
|
|
|
|
// running.
|
|
|
|
_, ok := rs.lastRequested[r]
|
|
|
|
return ok
|
|
|
|
}
|