2020-01-10 04:09:21 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2021-10-07 06:31:10 +00:00
|
|
|
"container/heap"
|
2021-10-08 02:53:36 +00:00
|
|
|
"context"
|
2021-10-05 09:06:23 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"reflect"
|
2021-10-08 02:53:36 +00:00
|
|
|
"runtime/pprof"
|
2020-01-10 04:09:21 +00:00
|
|
|
"time"
|
2021-05-09 14:53:32 +00:00
|
|
|
"unsafe"
|
2020-01-10 04:09:21 +00:00
|
|
|
|
2021-09-20 05:24:24 +00:00
|
|
|
"github.com/RoaringBitmap/roaring"
|
2021-10-05 09:06:23 +00:00
|
|
|
"github.com/anacrolix/log"
|
2021-05-20 01:16:54 +00:00
|
|
|
"github.com/anacrolix/missinggo/v2/bitmap"
|
2021-10-07 06:31:10 +00:00
|
|
|
"github.com/anacrolix/multiless"
|
2021-05-20 10:23:45 +00:00
|
|
|
|
2021-05-12 23:56:58 +00:00
|
|
|
request_strategy "github.com/anacrolix/torrent/request-strategy"
|
2020-01-10 04:09:21 +00:00
|
|
|
)
|
|
|
|
|
2021-05-20 10:23:45 +00:00
|
|
|
func (cl *Client) tickleRequester() {
|
|
|
|
cl.updateRequests.Broadcast()
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:57:50 +00:00
|
|
|
func (cl *Client) getRequestStrategyInput() request_strategy.Input {
|
2021-05-14 03:06:12 +00:00
|
|
|
ts := make([]request_strategy.Torrent, 0, len(cl.torrents))
|
2021-05-09 04:14:11 +00:00
|
|
|
for _, t := range cl.torrents {
|
2021-09-19 05:16:37 +00:00
|
|
|
if !t.haveInfo() {
|
2021-09-20 05:09:28 +00:00
|
|
|
// This would be removed if metadata is handled here. We have to guard against not
|
|
|
|
// knowing the piece size. If we have no info, we have no pieces too, so the end result
|
|
|
|
// is the same.
|
2021-09-19 05:16:37 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-05-14 03:06:12 +00:00
|
|
|
rst := request_strategy.Torrent{
|
2021-09-19 05:16:37 +00:00
|
|
|
InfoHash: t.infoHash,
|
|
|
|
ChunksPerPiece: (t.usualPieceSize() + int(t.chunkSize) - 1) / int(t.chunkSize),
|
2021-05-14 01:50:41 +00:00
|
|
|
}
|
2021-05-12 23:56:58 +00:00
|
|
|
if t.storage != nil {
|
|
|
|
rst.Capacity = t.storage.Capacity
|
2021-05-12 07:45:36 +00:00
|
|
|
}
|
2021-09-11 11:17:31 +00:00
|
|
|
rst.Pieces = make([]request_strategy.Piece, 0, len(t.pieces))
|
2021-05-12 23:56:58 +00:00
|
|
|
for i := range t.pieces {
|
|
|
|
p := &t.pieces[i]
|
|
|
|
rst.Pieces = append(rst.Pieces, request_strategy.Piece{
|
2021-09-11 11:17:47 +00:00
|
|
|
Request: !t.ignorePieceForRequests(i),
|
|
|
|
Priority: p.purePriority(),
|
|
|
|
Partial: t.piecePartiallyDownloaded(i),
|
|
|
|
Availability: p.availability,
|
|
|
|
Length: int64(p.length()),
|
|
|
|
NumPendingChunks: int(t.pieceNumPendingChunks(i)),
|
2021-10-05 09:06:23 +00:00
|
|
|
IterPendingChunks: p.undirtiedChunksIter(),
|
2021-05-12 07:45:36 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-12 23:56:58 +00:00
|
|
|
t.iterPeers(func(p *Peer) {
|
|
|
|
if p.closed.IsSet() {
|
|
|
|
return
|
2021-05-09 04:14:11 +00:00
|
|
|
}
|
2021-05-19 22:56:53 +00:00
|
|
|
if p.piecesReceivedSinceLastRequestUpdate > p.maxPiecesReceivedBetweenRequestUpdates {
|
|
|
|
p.maxPiecesReceivedBetweenRequestUpdates = p.piecesReceivedSinceLastRequestUpdate
|
|
|
|
}
|
|
|
|
p.piecesReceivedSinceLastRequestUpdate = 0
|
2021-05-13 01:26:22 +00:00
|
|
|
rst.Peers = append(rst.Peers, request_strategy.Peer{
|
2021-10-05 09:06:23 +00:00
|
|
|
Pieces: *p.newPeerPieces(),
|
|
|
|
MaxRequests: p.nominalMaxRequests(),
|
|
|
|
ExistingRequests: p.actualRequestState.Requests,
|
|
|
|
Choking: p.peerChoking,
|
|
|
|
PieceAllowedFast: p.peerAllowedFast,
|
|
|
|
DownloadRate: p.downloadRate(),
|
|
|
|
Age: time.Since(p.completedHandshake),
|
2021-09-10 13:02:20 +00:00
|
|
|
Id: peerId{
|
|
|
|
Peer: p,
|
|
|
|
ptr: uintptr(unsafe.Pointer(p)),
|
|
|
|
},
|
2021-05-12 07:45:36 +00:00
|
|
|
})
|
2021-05-09 04:14:11 +00:00
|
|
|
})
|
2021-05-12 23:56:58 +00:00
|
|
|
ts = append(ts, rst)
|
2020-01-24 06:55:20 +00:00
|
|
|
}
|
2021-09-18 08:57:50 +00:00
|
|
|
return request_strategy.Input{
|
2021-05-14 03:40:09 +00:00
|
|
|
Torrents: ts,
|
|
|
|
MaxUnverifiedBytes: cl.config.MaxUnverifiedBytes,
|
2021-09-18 08:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 09:06:23 +00:00
|
|
|
func init() {
|
|
|
|
gob.Register(peerId{})
|
|
|
|
}
|
|
|
|
|
2021-09-10 13:02:20 +00:00
|
|
|
type peerId struct {
|
|
|
|
*Peer
|
|
|
|
ptr uintptr
|
|
|
|
}
|
2021-05-13 01:26:22 +00:00
|
|
|
|
2021-09-10 13:02:20 +00:00
|
|
|
func (p peerId) Uintptr() uintptr {
|
|
|
|
return p.ptr
|
2021-05-13 01:26:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 09:06:23 +00:00
|
|
|
func (p peerId) GobEncode() (b []byte, _ error) {
|
|
|
|
*(*reflect.SliceHeader)(unsafe.Pointer(&b)) = reflect.SliceHeader{
|
|
|
|
Data: uintptr(unsafe.Pointer(&p.ptr)),
|
|
|
|
Len: int(unsafe.Sizeof(p.ptr)),
|
|
|
|
Cap: int(unsafe.Sizeof(p.ptr)),
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *peerId) GobDecode(b []byte) error {
|
|
|
|
if uintptr(len(b)) != unsafe.Sizeof(p.ptr) {
|
|
|
|
panic(len(b))
|
|
|
|
}
|
|
|
|
ptr := unsafe.Pointer(&b[0])
|
|
|
|
p.ptr = *(*uintptr)(ptr)
|
|
|
|
log.Printf("%p", ptr)
|
|
|
|
dst := reflect.SliceHeader{
|
|
|
|
Data: uintptr(unsafe.Pointer(&p.Peer)),
|
|
|
|
Len: int(unsafe.Sizeof(p.Peer)),
|
|
|
|
Cap: int(unsafe.Sizeof(p.Peer)),
|
|
|
|
}
|
|
|
|
copy(*(*[]byte)(unsafe.Pointer(&dst)), b)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-19 05:16:37 +00:00
|
|
|
type RequestIndex = request_strategy.RequestIndex
|
|
|
|
type chunkIndexType = request_strategy.ChunkIndex
|
|
|
|
|
2021-10-07 06:31:10 +00:00
|
|
|
type peerRequests struct {
|
|
|
|
requestIndexes []RequestIndex
|
|
|
|
peer *Peer
|
|
|
|
torrentStrategyInput request_strategy.Torrent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p peerRequests) Len() int {
|
|
|
|
return len(p.requestIndexes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p peerRequests) Less(i, j int) bool {
|
|
|
|
leftRequest := p.requestIndexes[i]
|
|
|
|
rightRequest := p.requestIndexes[j]
|
|
|
|
t := p.peer.t
|
|
|
|
leftPieceIndex := leftRequest / t.chunksPerRegularPiece()
|
|
|
|
rightPieceIndex := rightRequest / t.chunksPerRegularPiece()
|
|
|
|
leftCurrent := p.peer.actualRequestState.Requests.Contains(leftRequest)
|
|
|
|
rightCurrent := p.peer.actualRequestState.Requests.Contains(rightRequest)
|
|
|
|
pending := func(index RequestIndex, current bool) int {
|
|
|
|
ret := t.pendingRequests[index]
|
|
|
|
if current {
|
|
|
|
ret--
|
2021-09-20 05:09:28 +00:00
|
|
|
}
|
2021-10-07 06:31:10 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
ml := multiless.New()
|
|
|
|
ml = ml.Int(
|
|
|
|
pending(leftRequest, leftCurrent),
|
|
|
|
pending(rightRequest, rightCurrent))
|
|
|
|
ml = ml.Bool(rightCurrent, leftCurrent)
|
|
|
|
ml = ml.Int(
|
|
|
|
int(p.torrentStrategyInput.Pieces[leftPieceIndex].Priority),
|
|
|
|
int(p.torrentStrategyInput.Pieces[rightPieceIndex].Priority))
|
|
|
|
ml = ml.Int(
|
|
|
|
int(p.torrentStrategyInput.Pieces[leftPieceIndex].Availability),
|
|
|
|
int(p.torrentStrategyInput.Pieces[rightPieceIndex].Availability))
|
|
|
|
ml = ml.Uint32(leftPieceIndex, rightPieceIndex)
|
|
|
|
ml = ml.Uint32(leftRequest, rightRequest)
|
|
|
|
return ml.MustLess()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p peerRequests) Swap(i, j int) {
|
|
|
|
p.requestIndexes[i], p.requestIndexes[j] = p.requestIndexes[j], p.requestIndexes[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *peerRequests) Push(x interface{}) {
|
|
|
|
p.requestIndexes = append(p.requestIndexes, x.(RequestIndex))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *peerRequests) Pop() interface{} {
|
|
|
|
last := len(p.requestIndexes) - 1
|
|
|
|
x := p.requestIndexes[last]
|
|
|
|
p.requestIndexes = p.requestIndexes[:last]
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) getDesiredRequestState() (desired requestState) {
|
|
|
|
input := p.t.cl.getRequestStrategyInput()
|
|
|
|
requestHeap := peerRequests{
|
|
|
|
requestIndexes: nil,
|
|
|
|
peer: p,
|
|
|
|
}
|
|
|
|
for _, t := range input.Torrents {
|
|
|
|
if t.InfoHash == p.t.infoHash {
|
|
|
|
requestHeap.torrentStrategyInput = t
|
|
|
|
break
|
2021-09-20 05:09:28 +00:00
|
|
|
}
|
2021-10-07 06:31:10 +00:00
|
|
|
}
|
|
|
|
request_strategy.GetRequestablePieces(
|
|
|
|
input,
|
|
|
|
func(t *request_strategy.Torrent, rsp *request_strategy.Piece, pieceIndex int) {
|
|
|
|
if t.InfoHash != p.t.infoHash {
|
|
|
|
return
|
2021-09-18 10:34:14 +00:00
|
|
|
}
|
2021-10-07 06:31:10 +00:00
|
|
|
if !p.peerHasPiece(pieceIndex) {
|
|
|
|
return
|
2021-05-20 10:23:45 +00:00
|
|
|
}
|
2021-10-07 06:31:10 +00:00
|
|
|
rsp.IterPendingChunks.Iter(func(ci request_strategy.ChunkIndex) {
|
|
|
|
requestHeap.requestIndexes = append(
|
|
|
|
requestHeap.requestIndexes,
|
|
|
|
p.t.pieceRequestIndexOffset(pieceIndex)+ci)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
heap.Init(&requestHeap)
|
|
|
|
for requestHeap.Len() != 0 && desired.Requests.GetCardinality() < uint64(p.nominalMaxRequests()) {
|
|
|
|
requestIndex := heap.Pop(&requestHeap).(RequestIndex)
|
|
|
|
pieceIndex := requestIndex / p.t.chunksPerRegularPiece()
|
|
|
|
allowedFast := p.peerAllowedFast.Contains(pieceIndex)
|
|
|
|
if !allowedFast {
|
|
|
|
desired.Interested = true
|
2021-05-12 07:45:36 +00:00
|
|
|
}
|
2021-10-07 06:31:10 +00:00
|
|
|
if allowedFast || !p.peerChoking {
|
|
|
|
desired.Requests.Add(requestIndex)
|
2021-05-20 10:23:45 +00:00
|
|
|
}
|
2020-01-24 06:55:20 +00:00
|
|
|
}
|
2021-10-07 06:31:10 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-20 05:09:28 +00:00
|
|
|
|
2021-10-07 06:31:10 +00:00
|
|
|
func (p *Peer) applyNextRequestState() bool {
|
2021-10-08 02:53:36 +00:00
|
|
|
if p.needRequestUpdate == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var more bool
|
|
|
|
pprof.Do(
|
|
|
|
context.Background(),
|
|
|
|
pprof.Labels("update request", p.needRequestUpdate),
|
|
|
|
func(_ context.Context) {
|
|
|
|
next := p.getDesiredRequestState()
|
|
|
|
more = p.applyRequestState(next)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return more
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Peer) applyRequestState(next requestState) bool {
|
2021-09-20 05:09:28 +00:00
|
|
|
current := p.actualRequestState
|
|
|
|
if !p.setInterested(next.Interested) {
|
2021-09-18 10:34:14 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-09-20 05:09:28 +00:00
|
|
|
more := true
|
2021-09-20 05:24:24 +00:00
|
|
|
cancel := roaring.AndNot(¤t.Requests, &next.Requests)
|
|
|
|
cancel.Iterate(func(req uint32) bool {
|
|
|
|
more = p.cancel(req)
|
|
|
|
return more
|
2021-09-20 05:09:28 +00:00
|
|
|
})
|
|
|
|
if !more {
|
|
|
|
return false
|
2021-09-18 10:34:14 +00:00
|
|
|
}
|
2021-09-20 05:09:28 +00:00
|
|
|
next.Requests.Iterate(func(req uint32) bool {
|
|
|
|
// This could happen if the peer chokes us between the next state being generated, and us
|
|
|
|
// trying to transmit the state.
|
|
|
|
if p.peerChoking && !p.peerAllowedFast.Contains(bitmap.BitIndex(req/p.t.chunksPerRegularPiece())) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
more, err = p.request(req)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} /* else {
|
|
|
|
log.Print(req)
|
|
|
|
} */
|
|
|
|
return more
|
|
|
|
})
|
2021-10-08 02:53:36 +00:00
|
|
|
if more {
|
|
|
|
p.needRequestUpdate = ""
|
|
|
|
}
|
2021-09-18 10:34:14 +00:00
|
|
|
return more
|
2020-01-24 06:55:20 +00:00
|
|
|
}
|