2014-06-29 22:22:05 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-08-22 15:33:17 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-08-21 19:08:56 +08:00
|
|
|
|
|
|
|
pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
|
2014-06-29 22:22:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DownloadStrategy interface {
|
2014-08-25 20:14:10 +08:00
|
|
|
FillRequests(*torrent, *connection)
|
|
|
|
TorrentStarted(*torrent)
|
|
|
|
TorrentStopped(*torrent)
|
|
|
|
DeleteRequest(*torrent, request)
|
2014-07-24 11:42:31 +08:00
|
|
|
TorrentPrioritize(t *torrent, off, _len int64)
|
2014-08-25 20:14:10 +08:00
|
|
|
TorrentGotChunk(*torrent, request)
|
2014-07-24 11:42:31 +08:00
|
|
|
TorrentGotPiece(t *torrent, piece int)
|
2014-08-22 15:33:17 +08:00
|
|
|
WriteStatus(w io.Writer)
|
2014-08-24 01:08:11 +08:00
|
|
|
AssertNotRequested(*torrent, request)
|
2014-06-29 22:22:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultDownloadStrategy struct {
|
|
|
|
heat map[*torrent]map[request]int
|
|
|
|
}
|
|
|
|
|
2014-08-24 01:08:11 +08:00
|
|
|
func (me *DefaultDownloadStrategy) AssertNotRequested(t *torrent, r request) {
|
|
|
|
if me.heat[t][r] != 0 {
|
|
|
|
panic("outstanding requests break invariant")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 15:33:17 +08:00
|
|
|
func (me *DefaultDownloadStrategy) WriteStatus(w io.Writer) {}
|
|
|
|
|
2014-06-29 22:22:05 +08:00
|
|
|
func (s *DefaultDownloadStrategy) FillRequests(t *torrent, c *connection) {
|
2014-06-30 22:03:07 +08:00
|
|
|
if c.Interested {
|
|
|
|
if c.PeerChoked {
|
|
|
|
return
|
|
|
|
}
|
2014-07-16 15:11:45 +08:00
|
|
|
if len(c.Requests) >= (c.PeerMaxRequests+1)/2 {
|
2014-06-30 22:03:07 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-06-29 22:22:05 +08:00
|
|
|
th := s.heat[t]
|
|
|
|
addRequest := func(req request) (again bool) {
|
|
|
|
piece := t.Pieces[req.Index]
|
|
|
|
if piece.Hashing || piece.QueuedForHash {
|
|
|
|
// We can't be sure we want this.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if piece.Complete() {
|
|
|
|
// We already have this.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if c.RequestPending(req) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
again = c.Request(req)
|
|
|
|
if c.RequestPending(req) {
|
|
|
|
th[req]++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Then finish off incomplete pieces in order of bytes remaining.
|
2014-07-16 15:11:45 +08:00
|
|
|
for _, heatThreshold := range []int{1, 4, 15, 60} {
|
2014-08-25 03:31:34 +08:00
|
|
|
for e := t.IncompletePiecesByBytesLeft.Front(); e != nil; e = e.Next() {
|
2014-07-09 22:26:58 +08:00
|
|
|
pieceIndex := pp.Integer(e.Value.(int))
|
2014-07-16 15:11:45 +08:00
|
|
|
for _, chunkSpec := range t.Pieces[pieceIndex].shuffledPendingChunkSpecs() {
|
|
|
|
// for chunkSpec := range t.Pieces[pieceIndex].PendingChunkSpecs {
|
2014-06-29 22:22:05 +08:00
|
|
|
r := request{pieceIndex, chunkSpec}
|
2014-07-16 15:11:45 +08:00
|
|
|
if th[r] >= heatThreshold {
|
2014-06-29 22:22:05 +08:00
|
|
|
continue
|
|
|
|
}
|
2014-07-09 22:26:58 +08:00
|
|
|
if !addRequest(r) {
|
2014-06-29 22:22:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultDownloadStrategy) TorrentStarted(t *torrent) {
|
|
|
|
if s.heat[t] != nil {
|
|
|
|
panic("torrent already started")
|
|
|
|
}
|
|
|
|
if s.heat == nil {
|
|
|
|
s.heat = make(map[*torrent]map[request]int, 10)
|
|
|
|
}
|
|
|
|
s.heat[t] = make(map[request]int, t.ChunkCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultDownloadStrategy) TorrentStopped(t *torrent) {
|
|
|
|
if _, ok := s.heat[t]; !ok {
|
|
|
|
panic("torrent not yet started")
|
|
|
|
}
|
|
|
|
delete(s.heat, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DefaultDownloadStrategy) DeleteRequest(t *torrent, r request) {
|
|
|
|
m := s.heat[t]
|
|
|
|
if m[r] <= 0 {
|
|
|
|
panic("no pending requests")
|
|
|
|
}
|
|
|
|
m[r]--
|
|
|
|
}
|
|
|
|
|
2014-07-24 11:42:31 +08:00
|
|
|
func (me *DefaultDownloadStrategy) TorrentGotChunk(t *torrent, c request) {}
|
|
|
|
func (me *DefaultDownloadStrategy) TorrentGotPiece(t *torrent, piece int) {}
|
|
|
|
func (*DefaultDownloadStrategy) TorrentPrioritize(t *torrent, off, _len int64) {}
|
|
|
|
|
2014-08-24 01:08:11 +08:00
|
|
|
func NewResponsiveDownloadStrategy(readahead int64) *responsiveDownloadStrategy {
|
2014-07-24 11:42:31 +08:00
|
|
|
return &responsiveDownloadStrategy{
|
2014-08-24 01:08:11 +08:00
|
|
|
Readahead: readahead,
|
|
|
|
lastReadOffset: make(map[*torrent]int64),
|
|
|
|
priorities: make(map[*torrent]map[request]struct{}),
|
|
|
|
requestHeat: make(map[*torrent]map[request]int),
|
2014-07-24 11:42:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type responsiveDownloadStrategy struct {
|
2014-06-29 22:22:05 +08:00
|
|
|
// How many bytes to preemptively download starting at the beginning of
|
|
|
|
// the last piece read for a given torrent.
|
2014-08-24 01:08:11 +08:00
|
|
|
Readahead int64
|
|
|
|
lastReadOffset map[*torrent]int64
|
|
|
|
priorities map[*torrent]map[request]struct{}
|
|
|
|
requestHeat map[*torrent]map[request]int
|
2014-08-22 15:33:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *responsiveDownloadStrategy) WriteStatus(w io.Writer) {
|
|
|
|
fmt.Fprintf(w, "Priorities:\n")
|
|
|
|
for t, pp := range me.priorities {
|
|
|
|
fmt.Fprintf(w, "\t%s:", t.Name())
|
|
|
|
for r := range pp {
|
2014-08-25 20:14:10 +08:00
|
|
|
fmt.Fprintf(w, " %v", r)
|
2014-08-22 15:33:17 +08:00
|
|
|
}
|
|
|
|
fmt.Fprintln(w)
|
|
|
|
}
|
2014-06-29 22:22:05 +08:00
|
|
|
}
|
|
|
|
|
2014-07-24 11:42:31 +08:00
|
|
|
func (me *responsiveDownloadStrategy) TorrentStarted(t *torrent) {
|
2014-08-22 15:35:15 +08:00
|
|
|
me.priorities[t] = make(map[request]struct{})
|
2014-08-24 01:08:11 +08:00
|
|
|
me.requestHeat[t] = make(map[request]int)
|
2014-07-24 11:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *responsiveDownloadStrategy) TorrentStopped(t *torrent) {
|
2014-08-24 01:08:11 +08:00
|
|
|
delete(me.lastReadOffset, t)
|
2014-07-24 11:42:31 +08:00
|
|
|
delete(me.priorities, t)
|
|
|
|
}
|
2014-08-24 01:08:11 +08:00
|
|
|
func (me *responsiveDownloadStrategy) DeleteRequest(t *torrent, r request) {
|
|
|
|
rh := me.requestHeat[t]
|
|
|
|
if rh[r] <= 0 {
|
|
|
|
panic("request heat invariant broken")
|
|
|
|
}
|
|
|
|
rh[r]--
|
|
|
|
}
|
2014-06-29 22:22:05 +08:00
|
|
|
|
2014-07-24 11:42:31 +08:00
|
|
|
func (me *responsiveDownloadStrategy) FillRequests(t *torrent, c *connection) {
|
2014-08-25 03:31:34 +08:00
|
|
|
th := me.requestHeat[t]
|
2014-08-24 01:08:11 +08:00
|
|
|
requestWrapper := func(req request) bool {
|
|
|
|
if c.RequestPending(req) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
again := c.Request(req)
|
|
|
|
if c.RequestPending(req) {
|
2014-08-25 03:31:34 +08:00
|
|
|
th[req]++
|
2014-08-24 01:08:11 +08:00
|
|
|
}
|
|
|
|
return again
|
|
|
|
}
|
|
|
|
|
2014-08-22 15:35:15 +08:00
|
|
|
for req := range me.priorities[t] {
|
|
|
|
if _, ok := t.Pieces[req.Index].PendingChunkSpecs[req.chunkSpec]; !ok {
|
|
|
|
panic(req)
|
|
|
|
}
|
2014-08-24 01:08:11 +08:00
|
|
|
if !requestWrapper(req) {
|
2014-08-22 15:35:15 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-24 01:08:11 +08:00
|
|
|
if len(c.Requests) >= 16 {
|
2014-07-24 11:42:31 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-24 01:08:11 +08:00
|
|
|
requestWrapper = func() func(request) bool {
|
|
|
|
f := requestWrapper
|
|
|
|
return func(req request) bool {
|
|
|
|
if len(c.Requests) >= 32 {
|
2014-08-22 15:35:15 +08:00
|
|
|
return false
|
|
|
|
}
|
2014-08-24 01:08:11 +08:00
|
|
|
return f(req)
|
2014-06-29 22:22:05 +08:00
|
|
|
}
|
2014-08-24 01:08:11 +08:00
|
|
|
}()
|
2014-07-24 11:42:31 +08:00
|
|
|
|
2014-08-24 01:08:11 +08:00
|
|
|
if lastReadOffset, ok := me.lastReadOffset[t]; ok {
|
2014-08-25 03:31:34 +08:00
|
|
|
var nextAhead int64
|
|
|
|
for ahead := int64(0); ahead < me.Readahead; ahead = nextAhead {
|
|
|
|
off := lastReadOffset + ahead
|
2014-08-24 01:08:11 +08:00
|
|
|
req, ok := t.offsetRequest(off)
|
|
|
|
if !ok {
|
|
|
|
break
|
2014-06-29 22:22:05 +08:00
|
|
|
}
|
2014-08-25 03:31:34 +08:00
|
|
|
if !t.wantPiece(int(req.Index)) {
|
|
|
|
nextAhead = ahead + int64(t.PieceLength(req.Index))
|
2014-08-24 01:08:11 +08:00
|
|
|
continue
|
|
|
|
}
|
2014-08-25 03:31:34 +08:00
|
|
|
nextAhead = ahead + int64(req.Length)
|
2014-08-24 01:08:11 +08:00
|
|
|
if !t.wantChunk(req) {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-25 03:31:34 +08:00
|
|
|
if th[req] >= func() int {
|
|
|
|
// Determine allowed redundancy based on how far into the
|
|
|
|
// readahead zone we're looking.
|
|
|
|
if ahead >= (2*me.Readahead+2)/3 {
|
|
|
|
return 1
|
|
|
|
} else if ahead >= (me.Readahead+2)/3 {
|
|
|
|
return 2
|
|
|
|
} else {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
}() {
|
|
|
|
continue
|
|
|
|
}
|
2014-08-24 01:08:11 +08:00
|
|
|
if !requestWrapper(req) {
|
2014-06-29 22:22:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-25 03:31:34 +08:00
|
|
|
|
|
|
|
// t.assertIncompletePiecesByBytesLeftOrdering()
|
|
|
|
for e := t.IncompletePiecesByBytesLeft.Front(); e != nil; e = e.Next() {
|
|
|
|
p := e.Value.(int)
|
|
|
|
if !t.PiecePartiallyDownloaded(p) && int(t.PieceLength(pp.Integer(p))) == t.UsualPieceSize() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for chunkSpec := range t.Pieces[p].PendingChunkSpecs {
|
|
|
|
r := request{pp.Integer(p), chunkSpec}
|
|
|
|
if th[r] >= 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !requestWrapper(r) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-29 22:22:05 +08:00
|
|
|
}
|
2014-07-24 11:42:31 +08:00
|
|
|
|
|
|
|
func (me *responsiveDownloadStrategy) TorrentGotChunk(t *torrent, req request) {
|
2014-08-22 15:35:15 +08:00
|
|
|
delete(me.priorities[t], req)
|
2014-07-24 11:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *responsiveDownloadStrategy) TorrentGotPiece(t *torrent, piece int) {
|
2014-08-22 15:35:15 +08:00
|
|
|
for _, cs := range t.pieceChunks(piece) {
|
|
|
|
delete(me.priorities[t], request{pp.Integer(piece), cs})
|
2014-07-24 11:42:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *responsiveDownloadStrategy) TorrentPrioritize(t *torrent, off, _len int64) {
|
2014-08-24 01:08:11 +08:00
|
|
|
s.lastReadOffset[t] = off
|
2014-07-24 11:42:31 +08:00
|
|
|
for _len > 0 {
|
|
|
|
req, ok := t.offsetRequest(off)
|
|
|
|
if !ok {
|
|
|
|
panic("bad offset")
|
|
|
|
}
|
|
|
|
reqOff := t.requestOffset(req)
|
|
|
|
// Gain the alignment adjustment.
|
|
|
|
_len += off - reqOff
|
|
|
|
// Lose the length of this block.
|
|
|
|
_len -= int64(req.Length)
|
|
|
|
off = reqOff + int64(req.Length)
|
|
|
|
if t.wantChunk(req) {
|
2014-08-22 15:35:15 +08:00
|
|
|
s.priorities[t][req] = struct{}{}
|
2014-07-24 11:42:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-24 01:08:11 +08:00
|
|
|
|
|
|
|
func (s *responsiveDownloadStrategy) AssertNotRequested(t *torrent, r request) {
|
|
|
|
if s.requestHeat[t][r] != 0 {
|
|
|
|
panic("outstanding requests invariant broken")
|
|
|
|
}
|
|
|
|
}
|