From 4553e615168943a6cebe827544d4fecaac395632 Mon Sep 17 00:00:00 2001 From: YouROK <8yourok8@mail.ru> Date: Thu, 19 Aug 2021 08:59:21 +0300 Subject: [PATCH] change cache priority logic --- server/torr/storage/torrstor/cache.go | 32 ++++++++++++++------------ server/torr/storage/torrstor/reader.go | 7 ++++-- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/server/torr/storage/torrstor/cache.go b/server/torr/storage/torrstor/cache.go index 187822f..090c6a7 100644 --- a/server/torr/storage/torrstor/cache.go +++ b/server/torr/storage/torrstor/cache.go @@ -247,26 +247,28 @@ func (c *Cache) getRemPieces() []*Piece { if c.isIdInFileBE(ranges, r.getReaderPiece()) { continue } - pc := r.getReaderPiece() + readerPos := r.getReaderPiece() + readerRAHPos := r.getReaderRAHPiece() end := r.getPiecesRange().End - limit := 16777216 / c.pieceLength * 5 // 80 MB - if limit > 40 { - limit = 40 + count := int(16777216 / c.pieceLength * 5) // 80 MB + if count > 40 { + count = 40 } - count := limit + limit := 0 - for pc <= end && limit > 0 { - if !c.pieces[pc].Complete { - if c.torrent.PieceState(pc).Priority == torrent.PiecePriorityNone { - if limit < count/2 { - c.torrent.Piece(pc).SetPriority(torrent.PiecePriorityHigh) - } else { - c.torrent.Piece(pc).SetPriority(torrent.PiecePriorityNormal) - } + for i := readerPos; i < end && limit < count; i++ { + if !c.pieces[i].Complete { + if i == readerPos { + c.torrent.Piece(i).SetPriority(torrent.PiecePriorityNow) + } else if i > readerPos && i <= readerRAHPos { + c.torrent.Piece(i).SetPriority(torrent.PiecePriorityReadahead) + } else if i > readerRAHPos && i <= end/2 && c.torrent.PieceState(i).Priority != torrent.PiecePriorityHigh { + c.torrent.Piece(i).SetPriority(torrent.PiecePriorityHigh) + } else if i > end/2 && c.torrent.PieceState(i).Priority != torrent.PiecePriorityNormal { + c.torrent.Piece(i).SetPriority(torrent.PiecePriorityNormal) } - limit-- + limit++ } - pc++ } } c.muReaders.Unlock() diff --git a/server/torr/storage/torrstor/reader.go b/server/torr/storage/torrstor/reader.go index 76e6f2c..8bdd1db 100644 --- a/server/torr/storage/torrstor/reader.go +++ b/server/torr/storage/torrstor/reader.go @@ -125,8 +125,11 @@ func (r *Reader) getPiecesRange() Range { } func (r *Reader) getReaderPiece() int { - readerOff := r.offset - return r.getPieceNum(readerOff) + return r.getPieceNum(r.offset) +} + +func (r *Reader) getReaderRAHPiece() int { + return r.getPieceNum(r.offset + r.readahead) } func (r *Reader) getPieceNum(offset int64) int {