From cfb23e271f1f509cc39e59484c9bfe3e0075990d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sat, 18 Sep 2021 18:57:50 +1000 Subject: [PATCH] Export request_strategy.GetRequestablePieces --- request-strategy/order.go | 23 +++++++++++++---------- request-strategy/torrent.go | 3 ++- requesting.go | 10 +++++++--- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/request-strategy/order.go b/request-strategy/order.go index 8b4b6799..0a32ae9a 100644 --- a/request-strategy/order.go +++ b/request-strategy/order.go @@ -89,13 +89,13 @@ type filterPiece struct { *Piece } -func getRequestablePieces(input Input) (ret []requestablePiece) { +// Calls f with requestable pieces in order. +func GetRequestablePieces(input Input, f func(t *Torrent, p *Piece, pieceIndex int)) { maxPieces := 0 for i := range input.Torrents { maxPieces += len(input.Torrents[i].Pieces) } pieces := make([]filterPiece, 0, maxPieces) - ret = make([]requestablePiece, 0, maxPieces) // Storage capacity left for this run, keyed by the storage capacity pointer on the storage // TorrentImpl. A nil value means no capacity limit. storageLeft := make(map[storage.TorrentCapacity]*int64) @@ -147,13 +147,7 @@ func getRequestablePieces(input Input) (ret []requestablePiece) { } piece.t.unverifiedBytes += piece.Length allTorrentsUnverifiedBytes += piece.Length - ret = append(ret, requestablePiece{ - index: piece.index, - t: piece.t.Torrent, - NumPendingChunks: piece.NumPendingChunks, - IterPendingChunks: piece.iterPendingChunksWrapper, - alwaysReallocate: piece.Priority >= types.PiecePriorityNext, - }) + f(piece.t.Torrent, piece.Piece, piece.index) } return } @@ -165,7 +159,16 @@ type Input struct { // TODO: We could do metainfo requests here. func Run(input Input) map[PeerId]PeerNextRequestState { - requestPieces := getRequestablePieces(input) + var requestPieces []requestablePiece + GetRequestablePieces(input, func(t *Torrent, piece *Piece, pieceIndex int) { + requestPieces = append(requestPieces, requestablePiece{ + index: pieceIndex, + t: t, + NumPendingChunks: piece.NumPendingChunks, + IterPendingChunks: piece.iterPendingChunksWrapper, + alwaysReallocate: piece.Priority >= types.PiecePriorityNext, + }) + }) torrents := input.Torrents allPeers := make(map[uintptr][]*requestsPeer, len(torrents)) for _, t := range torrents { diff --git a/request-strategy/torrent.go b/request-strategy/torrent.go index c7ed3c60..a31ec772 100644 --- a/request-strategy/torrent.go +++ b/request-strategy/torrent.go @@ -7,7 +7,8 @@ import ( type Torrent struct { Pieces []Piece Capacity storage.TorrentCapacity - Peers []Peer // not closed. + // Unclosed Peers. Not necessary for getting requestable piece ordering. + Peers []Peer // Some value that's unique and stable between runs. Could even use the infohash? StableId uintptr diff --git a/requesting.go b/requesting.go index ea1fb8e8..7e42baba 100644 --- a/requesting.go +++ b/requesting.go @@ -39,7 +39,7 @@ func (cl *Client) tickleRequester() { cl.updateRequests.Broadcast() } -func (cl *Client) doRequests() { +func (cl *Client) getRequestStrategyInput() request_strategy.Input { ts := make([]request_strategy.Torrent, 0, len(cl.torrents)) for _, t := range cl.torrents { rst := request_strategy.Torrent{ @@ -90,10 +90,14 @@ func (cl *Client) doRequests() { }) ts = append(ts, rst) } - nextPeerStates := request_strategy.Run(request_strategy.Input{ + return request_strategy.Input{ Torrents: ts, MaxUnverifiedBytes: cl.config.MaxUnverifiedBytes, - }) + } +} + +func (cl *Client) doRequests() { + nextPeerStates := request_strategy.Run(cl.getRequestStrategyInput()) for p, state := range nextPeerStates { setPeerNextRequestState(p, state) } -- 2.48.1