2020-01-10 12:09:21 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2020-01-20 08:51:24 +08:00
|
|
|
"sync"
|
2020-01-10 12:09:21 +08: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
|
2020-01-10 13:18:55 +08:00
|
|
|
chunkIndexRequest(i pp.Integer) request
|
2020-01-10 12:09:21 +08: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
|
|
|
|
iterUndirtiedChunks(requestStrategyPiece, func(chunkSpec) bool) bool
|
|
|
|
nominalMaxRequests(requestStrategyConnection) int
|
|
|
|
shouldRequestWithoutBias(requestStrategyConnection) bool
|
|
|
|
piecePriority(requestStrategyConnection, pieceIndex, piecePriority, int) int
|
2020-01-10 13:18:55 +08:00
|
|
|
hooks() requestStrategyHooks
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategyHooks struct {
|
|
|
|
sentRequest func(request)
|
|
|
|
deletedRequest func(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategyCallbacks interface {
|
|
|
|
requestTimedOut(request)
|
2020-01-10 12:09:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Favour higher priority pieces with some fuzzing to reduce overlaps and wastage across
|
|
|
|
// connections.
|
|
|
|
type requestStrategyOne struct{}
|
|
|
|
|
|
|
|
// The fastest connection downloads strictly in order of priority, while all others adhere to their
|
|
|
|
// piece inclinations.
|
|
|
|
type requestStrategyTwo struct{}
|
|
|
|
|
|
|
|
func (requestStrategyTwo) ShouldRequestWithoutBias(cn requestStrategyConnection) bool {
|
|
|
|
if cn.torrent().numReaders() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if cn.torrent().numConns() == 1 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if cn.fastest() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requests are strictly by piece priority, and not duplicated until duplicateRequestTimeout is
|
|
|
|
// reached.
|
|
|
|
type requestStrategyThree struct {
|
2020-01-10 13:18:55 +08:00
|
|
|
// How long to avoid duplicating a pending request.
|
|
|
|
duplicateRequestTimeout time.Duration
|
2020-01-20 08:51:24 +08:00
|
|
|
|
|
|
|
callbacks requestStrategyCallbacks
|
|
|
|
|
2020-01-10 13:18:55 +08:00
|
|
|
// The last time we requested a chunk. Deleting the request from any connection will clear this
|
|
|
|
// value.
|
|
|
|
lastRequested map[request]*time.Timer
|
2020-01-20 08:51:24 +08:00
|
|
|
// The lock to take when running a request timeout handler.
|
|
|
|
timeoutLocker sync.Locker
|
2020-01-10 13:18:55 +08:00
|
|
|
}
|
|
|
|
|
2020-01-20 08:51:24 +08:00
|
|
|
type requestStrategyMaker func(callbacks requestStrategyCallbacks, clientLocker sync.Locker) requestStrategy
|
2020-01-10 13:18:55 +08:00
|
|
|
|
|
|
|
func requestStrategyThreeMaker(duplicateRequestTimeout time.Duration) requestStrategyMaker {
|
2020-01-20 08:51:24 +08:00
|
|
|
return func(callbacks requestStrategyCallbacks, clientLocker sync.Locker) requestStrategy {
|
2020-01-10 13:18:55 +08:00
|
|
|
return requestStrategyThree{
|
|
|
|
duplicateRequestTimeout: duplicateRequestTimeout,
|
|
|
|
callbacks: callbacks,
|
|
|
|
lastRequested: make(map[request]*time.Timer),
|
2020-01-20 08:51:24 +08:00
|
|
|
timeoutLocker: clientLocker,
|
2020-01-10 13:18:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs requestStrategyThree) hooks() requestStrategyHooks {
|
|
|
|
return requestStrategyHooks{
|
|
|
|
deletedRequest: func(r request) {
|
|
|
|
if t, ok := rs.lastRequested[r]; ok {
|
|
|
|
t.Stop()
|
|
|
|
delete(rs.lastRequested, r)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sentRequest: rs.onSentRequest,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs requestStrategyOne) hooks() requestStrategyHooks {
|
|
|
|
return requestStrategyHooks{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs requestStrategyTwo) hooks() requestStrategyHooks {
|
|
|
|
return requestStrategyHooks{}
|
2020-01-10 12:09:21 +08:00
|
|
|
}
|