Move requestStrategyDefaults into its own file

This commit is contained in:
Matt Joiner 2020-02-27 09:45:13 +11:00
parent bae791a5a2
commit 436da59801
2 changed files with 43 additions and 36 deletions

View File

@ -5,7 +5,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/anacrolix/missinggo/iter"
"github.com/anacrolix/missinggo/v2/bitmap" "github.com/anacrolix/missinggo/v2/bitmap"
"github.com/anacrolix/missinggo/v2/prioritybitmap" "github.com/anacrolix/missinggo/v2/prioritybitmap"
@ -38,15 +37,6 @@ type requestStrategyConnection interface {
chunksReceivedWhileExpecting() int64 chunksReceivedWhileExpecting() int64
} }
type requestStrategyDefaults struct{}
func (requestStrategyDefaults) hooks() requestStrategyHooks {
return requestStrategyHooks{
sentRequest: func(request) {},
deletedRequest: func(request) {},
}
}
type requestStrategy interface { type requestStrategy interface {
iterPendingPieces(requestStrategyConnection, func(pieceIndex) bool) bool iterPendingPieces(requestStrategyConnection, func(pieceIndex) bool) bool
iterUndirtiedChunks(requestStrategyPiece, func(chunkSpec) bool) bool iterUndirtiedChunks(requestStrategyPiece, func(chunkSpec) bool) bool
@ -145,10 +135,6 @@ func (rs requestStrategyDuplicateRequestTimeout) hooks() requestStrategyHooks {
} }
} }
func (requestStrategyDefaults) piecePriority(cn requestStrategyConnection, piece pieceIndex, tpp piecePriority, prio int) int {
return prio
}
func (rs requestStrategyDuplicateRequestTimeout) iterUndirtiedChunks(p requestStrategyPiece, f func(chunkSpec) bool) bool { func (rs requestStrategyDuplicateRequestTimeout) iterUndirtiedChunks(p requestStrategyPiece, f func(chunkSpec) bool) bool {
for i := pp.Integer(0); i < pp.Integer(p.numChunks()); i++ { for i := pp.Integer(0); i < pp.Integer(p.numChunks()); i++ {
if p.dirtyChunks().Get(bitmap.BitIndex(i)) { if p.dirtyChunks().Get(bitmap.BitIndex(i)) {
@ -165,18 +151,6 @@ func (rs requestStrategyDuplicateRequestTimeout) iterUndirtiedChunks(p requestSt
return true return true
} }
func (requestStrategyDefaults) iterUndirtiedChunks(p requestStrategyPiece, f func(chunkSpec) bool) bool {
chunkIndices := p.dirtyChunks().Copy()
chunkIndices.FlipRange(0, bitmap.BitIndex(p.numChunks()))
return iter.ForPerm(chunkIndices.Len(), func(i int) bool {
ci, err := chunkIndices.RB.Select(uint32(i))
if err != nil {
panic(err)
}
return f(p.chunkIndexRequest(pp.Integer(ci)).chunkSpec)
})
}
func (requestStrategyFuzzing) piecePriority(cn requestStrategyConnection, piece pieceIndex, tpp piecePriority, prio int) int { func (requestStrategyFuzzing) piecePriority(cn requestStrategyConnection, piece pieceIndex, tpp piecePriority, prio int) int {
switch tpp { switch tpp {
case PiecePriorityNormal: case PiecePriorityNormal:
@ -210,10 +184,6 @@ func (rs requestStrategyFastest) iterPendingPieces(cn requestStrategyConnection,
return defaultIterPendingPieces(rs, cn, cb) return defaultIterPendingPieces(rs, cn, cb)
} }
func (requestStrategyDefaults) shouldRequestWithoutBias(cn requestStrategyConnection) bool {
return false
}
func (rs requestStrategyDuplicateRequestTimeout) onSentRequest(r request) { func (rs requestStrategyDuplicateRequestTimeout) onSentRequest(r request) {
rs.lastRequested[r] = time.AfterFunc(rs.duplicateRequestTimeout, func() { rs.lastRequested[r] = time.AfterFunc(rs.duplicateRequestTimeout, func() {
rs.timeoutLocker.Lock() rs.timeoutLocker.Lock()
@ -244,12 +214,6 @@ func (rs requestStrategyDuplicateRequestTimeout) nominalMaxRequests(cn requestSt
), ),
)) ))
} }
func (requestStrategyDefaults) nominalMaxRequests(cn requestStrategyConnection) int {
return int(
max(64,
cn.stats().ChunksReadUseful.Int64()-(cn.stats().ChunksRead.Int64()-cn.stats().ChunksReadUseful.Int64())))
}
func (rs requestStrategyDuplicateRequestTimeout) wouldDuplicateRecent(r request) bool { func (rs requestStrategyDuplicateRequestTimeout) wouldDuplicateRecent(r request) bool {
// This piece has been requested on another connection, and the duplicate request timer is still // This piece has been requested on another connection, and the duplicate request timer is still
// running. // running.

View File

@ -0,0 +1,43 @@
package torrent
import (
"github.com/anacrolix/missinggo/iter"
"github.com/anacrolix/missinggo/v2/bitmap"
pp "github.com/anacrolix/torrent/peer_protocol"
)
// Provides default implementations for requestStrategy methods. Could be embedded, or delegated to.
type requestStrategyDefaults struct{}
func (requestStrategyDefaults) hooks() requestStrategyHooks {
return requestStrategyHooks{
sentRequest: func(request) {},
deletedRequest: func(request) {},
}
}
func (requestStrategyDefaults) iterUndirtiedChunks(p requestStrategyPiece, f func(chunkSpec) bool) bool {
chunkIndices := p.dirtyChunks().Copy()
chunkIndices.FlipRange(0, bitmap.BitIndex(p.numChunks()))
return iter.ForPerm(chunkIndices.Len(), func(i int) bool {
ci, err := chunkIndices.RB.Select(uint32(i))
if err != nil {
panic(err)
}
return f(p.chunkIndexRequest(pp.Integer(ci)).chunkSpec)
})
}
func (requestStrategyDefaults) nominalMaxRequests(cn requestStrategyConnection) int {
return int(
max(64,
cn.stats().ChunksReadUseful.Int64()-(cn.stats().ChunksRead.Int64()-cn.stats().ChunksReadUseful.Int64())))
}
func (requestStrategyDefaults) piecePriority(cn requestStrategyConnection, piece pieceIndex, tpp piecePriority, prio int) int {
return prio
}
func (requestStrategyDefaults) shouldRequestWithoutBias(cn requestStrategyConnection) bool {
return false
}