X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=requesting.go;h=8b9db971b537630e65dd41ca9618f8a708f6a698;hb=5d0de758a23911b24e16b6ba2ae1a9f9eeb585d0;hp=60e56df7d808564ce5e3e4b01b48eb98c5ee67b4;hpb=d3b3d2d9bf2ebbe5cb899cbc999d197b5fe502f0;p=btrtrc.git diff --git a/requesting.go b/requesting.go index 60e56df7..8b9db971 100644 --- a/requesting.go +++ b/requesting.go @@ -9,12 +9,12 @@ import ( "time" "unsafe" + "github.com/anacrolix/generics/heap" "github.com/anacrolix/log" "github.com/anacrolix/multiless" - "github.com/lispad/go-generics-tools/binheap" - "github.com/anacrolix/torrent/request-strategy" - "github.com/anacrolix/torrent/typed-roaring" + requestStrategy "github.com/anacrolix/torrent/request-strategy" + typedRoaring "github.com/anacrolix/torrent/typed-roaring" ) type ( @@ -23,8 +23,8 @@ type ( maxRequests = int ) -func (t *Torrent) requestStrategyPieceOrderState(i int) request_strategy.PieceRequestOrderState { - return request_strategy.PieceRequestOrderState{ +func (t *Torrent) requestStrategyPieceOrderState(i int) requestStrategy.PieceRequestOrderState { + return requestStrategy.PieceRequestOrderState{ Priority: t.piece(i).purePriority(), Partial: t.piecePartiallyDownloaded(i), Availability: t.piece(i).availability(), @@ -70,22 +70,14 @@ func (p *peerId) GobDecode(b []byte) error { } type ( - RequestIndex = request_strategy.RequestIndex - chunkIndexType = request_strategy.ChunkIndex + RequestIndex = requestStrategy.RequestIndex + chunkIndexType = requestStrategy.ChunkIndex ) type desiredPeerRequests struct { requestIndexes []RequestIndex peer *Peer - pieceStates []request_strategy.PieceRequestOrderState -} - -func (p *desiredPeerRequests) Len() int { - return len(p.requestIndexes) -} - -func (p *desiredPeerRequests) Less(i, j int) bool { - return p.lessByValue(p.requestIndexes[i], p.requestIndexes[j]) + pieceStates []requestStrategy.PieceRequestOrderState } func (p *desiredPeerRequests) lessByValue(leftRequest, rightRequest RequestIndex) bool { @@ -168,21 +160,6 @@ func (p *desiredPeerRequests) lessByValue(leftRequest, rightRequest RequestIndex return ml.Less() } -func (p *desiredPeerRequests) Swap(i, j int) { - p.requestIndexes[i], p.requestIndexes[j] = p.requestIndexes[j], p.requestIndexes[i] -} - -func (p *desiredPeerRequests) Push(x interface{}) { - p.requestIndexes = append(p.requestIndexes, x.(RequestIndex)) -} - -func (p *desiredPeerRequests) Pop() interface{} { - last := len(p.requestIndexes) - 1 - x := p.requestIndexes[last] - p.requestIndexes = p.requestIndexes[:last] - return x -} - type desiredRequestState struct { Requests desiredPeerRequests Interested bool @@ -204,10 +181,10 @@ func (p *Peer) getDesiredRequestState() (desired desiredRequestState) { } // Caller-provided allocation for roaring bitmap iteration. var it typedRoaring.Iterator[RequestIndex] - request_strategy.GetRequestablePieces( + requestStrategy.GetRequestablePieces( input, t.getPieceRequestOrder(), - func(ih InfoHash, pieceIndex int, pieceExtra request_strategy.PieceRequestOrderState) { + func(ih InfoHash, pieceIndex int, pieceExtra requestStrategy.PieceRequestOrderState) { if ih != t.infoHash { return } @@ -216,7 +193,7 @@ func (p *Peer) getDesiredRequestState() (desired desiredRequestState) { } requestHeap.pieceStates[pieceIndex] = pieceExtra allowedFast := p.peerAllowedFast.Contains(pieceIndex) - t.iterUndirtiedRequestIndexesInPiece(&it, pieceIndex, func(r request_strategy.RequestIndex) { + t.iterUndirtiedRequestIndexesInPiece(&it, pieceIndex, func(r requestStrategy.RequestIndex) { if !allowedFast { // We must signal interest to request this. TODO: We could set interested if the // peers pieces (minus the allowed fast set) overlap with our missing pieces if @@ -272,10 +249,12 @@ func (p *Peer) maybeUpdateActualRequestState() { func (p *Peer) applyRequestState(next desiredRequestState) { current := &p.requestState if !p.setInterested(next.Interested) { - panic("insufficient write buffer") + return } more := true - requestHeap := binheap.FromSlice(next.Requests.requestIndexes, next.Requests.lessByValue) + requestHeap := heap.InterfaceForSlice(&next.Requests.requestIndexes, next.Requests.lessByValue) + heap.Init(requestHeap) + t := p.t originalRequestCount := current.Requests.GetCardinality() // We're either here on a timer, or because we ran out of requests. Both are valid reasons to @@ -286,7 +265,7 @@ func (p *Peer) applyRequestState(next desiredRequestState) { originalRequestCount, p.needRequestUpdate)) } for requestHeap.Len() != 0 && maxRequests(current.Requests.GetCardinality()+current.Cancelled.GetCardinality()) < p.nominalMaxRequests() { - req := requestHeap.Pop() + req := heap.Pop(requestHeap) existing := t.requestingPeer(req) if existing != nil && existing != p { // Don't steal from the poor.