2020-01-10 04:09:21 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
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
|
2020-01-10 05:18:55 +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
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:09:39 +00:00
|
|
|
type requestStrategyDefaults struct{}
|
|
|
|
|
|
|
|
func (requestStrategyDefaults) hooks() requestStrategyHooks {
|
|
|
|
return requestStrategyHooks{
|
|
|
|
sentRequest: func(request) {},
|
|
|
|
deletedRequest: func(request) {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 04:09:21 +00:00
|
|
|
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 05:18:55 +00:00
|
|
|
hooks() requestStrategyHooks
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategyHooks struct {
|
|
|
|
sentRequest func(request)
|
|
|
|
deletedRequest func(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestStrategyCallbacks interface {
|
|
|
|
requestTimedOut(request)
|
2020-01-10 04:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Favour higher priority pieces with some fuzzing to reduce overlaps and wastage across
|
|
|
|
// connections.
|
2020-01-24 04:09:39 +00:00
|
|
|
type requestStrategyOne struct {
|
|
|
|
requestStrategyDefaults
|
|
|
|
}
|
2020-01-10 04:09:21 +00:00
|
|
|
|
|
|
|
// The fastest connection downloads strictly in order of priority, while all others adhere to their
|
|
|
|
// piece inclinations.
|
2020-01-24 04:09:39 +00:00
|
|
|
type requestStrategyTwo struct {
|
|
|
|
requestStrategyDefaults
|
|
|
|
}
|
2020-01-10 04:09:21 +00:00
|
|
|
|
|
|
|
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 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.
|
|
|
|
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-01-20 00:51:24 +00:00
|
|
|
type requestStrategyMaker func(callbacks requestStrategyCallbacks, clientLocker sync.Locker) requestStrategy
|
2020-01-10 05:18:55 +00:00
|
|
|
|
|
|
|
func requestStrategyThreeMaker(duplicateRequestTimeout time.Duration) requestStrategyMaker {
|
2020-01-20 00:51:24 +00:00
|
|
|
return func(callbacks requestStrategyCallbacks, clientLocker sync.Locker) requestStrategy {
|
2020-01-10 05:18:55 +00:00
|
|
|
return requestStrategyThree{
|
|
|
|
duplicateRequestTimeout: duplicateRequestTimeout,
|
|
|
|
callbacks: callbacks,
|
|
|
|
lastRequested: make(map[request]*time.Timer),
|
2020-01-20 00:51:24 +00:00
|
|
|
timeoutLocker: clientLocker,
|
2020-01-10 05:18:55 +00: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,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|