Merge piece and chunk iter inputs to nextRequestState
This will allow the iterator to filter chunks for request strategies.
This commit is contained in:
parent
3217c5012a
commit
1e9f342a15
|
@ -490,8 +490,7 @@ func nextRequestState(
|
||||||
networkingEnabled bool,
|
networkingEnabled bool,
|
||||||
currentRequests map[request]struct{},
|
currentRequests map[request]struct{},
|
||||||
peerChoking bool,
|
peerChoking bool,
|
||||||
requestPieces iter.Func,
|
iterPendingRequests func(f func(request) bool),
|
||||||
pendingChunks func(piece int, f func(chunkSpec) bool) bool,
|
|
||||||
requestsLowWater int,
|
requestsLowWater int,
|
||||||
requestsHighWater int,
|
requestsHighWater int,
|
||||||
) (
|
) (
|
||||||
|
@ -505,22 +504,18 @@ func nextRequestState(
|
||||||
if len(currentRequests) > requestsLowWater {
|
if len(currentRequests) > requestsLowWater {
|
||||||
return false, nil, true
|
return false, nil, true
|
||||||
}
|
}
|
||||||
requestPieces(func(_piece interface{}) bool {
|
iterPendingRequests(func(r request) bool {
|
||||||
interested = true
|
interested = true
|
||||||
if peerChoking {
|
if peerChoking {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
piece := _piece.(int)
|
if _, ok := currentRequests[r]; !ok {
|
||||||
return pendingChunks(piece, func(cs chunkSpec) bool {
|
if newRequests == nil {
|
||||||
r := request{pp.Integer(piece), cs}
|
newRequests = make([]request, 0, requestsHighWater-len(currentRequests))
|
||||||
if _, ok := currentRequests[r]; !ok {
|
|
||||||
if newRequests == nil {
|
|
||||||
newRequests = make([]request, 0, requestsHighWater-len(currentRequests))
|
|
||||||
}
|
|
||||||
newRequests = append(newRequests, r)
|
|
||||||
}
|
}
|
||||||
return len(currentRequests)+len(newRequests) < requestsHighWater
|
newRequests = append(newRequests, r)
|
||||||
})
|
}
|
||||||
|
return len(currentRequests)+len(newRequests) < requestsHighWater
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -600,21 +595,32 @@ func (cn *connection) pieceRequestOrderIter() iter.Func {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cn *connection) iterPendingRequests(f func(request) bool) {
|
||||||
|
cn.pieceRequestOrderIter()(func(_piece interface{}) bool {
|
||||||
|
piece := _piece.(int)
|
||||||
|
return iterUndirtiedChunks(piece, cn.t, func(cs chunkSpec) bool {
|
||||||
|
r := request{pp.Integer(piece), cs}
|
||||||
|
// log.Println(r, cn.t.pendingRequests[r], cn.requests)
|
||||||
|
// if _, ok := cn.requests[r]; !ok && cn.t.pendingRequests[r] != 0 {
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
return f(r)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (cn *connection) desiredRequestState() (bool, []request, bool) {
|
func (cn *connection) desiredRequestState() (bool, []request, bool) {
|
||||||
return nextRequestState(
|
return nextRequestState(
|
||||||
cn.t.networkingEnabled,
|
cn.t.networkingEnabled,
|
||||||
cn.requests,
|
cn.requests,
|
||||||
cn.PeerChoked,
|
cn.PeerChoked,
|
||||||
cn.pieceRequestOrderIter(),
|
cn.iterPendingRequests,
|
||||||
func(piece int, f func(chunkSpec) bool) bool {
|
|
||||||
return undirtiedChunks(piece, cn.t, f)
|
|
||||||
},
|
|
||||||
cn.requestsLowWater,
|
cn.requestsLowWater,
|
||||||
cn.nominalMaxRequests(),
|
cn.nominalMaxRequests(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func undirtiedChunks(piece int, t *Torrent, f func(chunkSpec) bool) bool {
|
func iterUndirtiedChunks(piece int, t *Torrent, f func(chunkSpec) bool) bool {
|
||||||
chunkIndices := t.pieces[piece].undirtiedChunkIndices().ToSortedSlice()
|
chunkIndices := t.pieces[piece].undirtiedChunkIndices().ToSortedSlice()
|
||||||
// TODO: Use "math/rand".Shuffle >= Go 1.10
|
// TODO: Use "math/rand".Shuffle >= Go 1.10
|
||||||
return iter.ForPerm(len(chunkIndices), func(i int) bool {
|
return iter.ForPerm(len(chunkIndices), func(i int) bool {
|
||||||
|
|
Loading…
Reference in New Issue