sync from master

This commit is contained in:
nikk gitanes
2021-08-22 22:48:24 +03:00
11 changed files with 61 additions and 21 deletions

View File

@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"sort"
"sync"
"time"
"github.com/anacrolix/torrent"
@@ -16,7 +17,8 @@ import (
)
var (
bts *BTServer
bts *BTServer
lockApi sync.Mutex
)
func InitApiHelper(bt *BTServer) {
@@ -24,6 +26,8 @@ func InitApiHelper(bt *BTServer) {
}
func LoadTorrent(tor *Torrent) *Torrent {
lockApi.Lock()
defer lockApi.Unlock()
if tor.TorrentSpec == nil {
return nil
}
@@ -41,6 +45,8 @@ func LoadTorrent(tor *Torrent) *Torrent {
}
func AddTorrent(spec *torrent.TorrentSpec, title, poster string, data string) (*Torrent, error) {
lockApi.Lock()
defer lockApi.Unlock()
torr, err := NewTorrent(spec, bts)
if err != nil {
log.TLogln("error add torrent:", err)
@@ -80,6 +86,8 @@ func SaveTorrentToDB(torr *Torrent) {
}
func GetTorrent(hashHex string) *Torrent {
lockApi.Lock()
defer lockApi.Unlock()
hash := metainfo.NewHashFromHex(hashHex)
tor := bts.GetTorrent(hash)
if tor != nil {
@@ -91,6 +99,9 @@ func GetTorrent(hashHex string) *Torrent {
if tr != nil {
tor = tr
go func() {
lockApi.Lock()
defer lockApi.Unlock()
log.TLogln("Add torrent")
tr, _ := NewTorrent(tor.TorrentSpec, bts)
if tr != nil {
tr.Title = tor.Title
@@ -106,6 +117,9 @@ func GetTorrent(hashHex string) *Torrent {
}
func SetTorrent(hashHex, title, poster, data string) *Torrent {
lockApi.Lock()
defer lockApi.Unlock()
hash := metainfo.NewHashFromHex(hashHex)
torr := bts.GetTorrent(hash)
torrDb := GetTorrentDB(hash)
@@ -141,6 +155,8 @@ func SetTorrent(hashHex, title, poster, data string) *Torrent {
}
func RemTorrent(hashHex string) {
lockApi.Lock()
defer lockApi.Unlock()
hash := metainfo.NewHashFromHex(hashHex)
if sets.BTsets.UseDisk && hashHex != "" && hashHex != "/" {
name := filepath.Join(sets.BTsets.TorrentsSavePath, hashHex)
@@ -158,6 +174,8 @@ func RemTorrent(hashHex string) {
}
func ListTorrent() []*Torrent {
lockApi.Lock()
defer lockApi.Unlock()
btlist := bts.ListTorrents()
dblist := ListTorrentsDB()
@@ -184,6 +202,8 @@ func ListTorrent() []*Torrent {
}
func DropTorrent(hashHex string) {
lockApi.Lock()
defer lockApi.Unlock()
hash := metainfo.NewHashFromHex(hashHex)
bts.RemoveTorrent(hash)
}
@@ -192,21 +212,42 @@ func SetSettings(set *sets.BTSets) {
if sets.ReadOnly {
return
}
lockApi.Lock()
defer lockApi.Unlock()
log.TLogln("drop all")
dropAllTorrent()
time.Sleep(time.Second * 2)
log.TLogln("disconect")
bts.Disconnect()
sets.SetBTSets(set)
log.TLogln("connect")
bts.Connect()
log.TLogln("end set settings")
}
func SetDefSettings() {
if sets.ReadOnly {
return
}
lockApi.Lock()
defer lockApi.Unlock()
dropAllTorrent()
bts.Disconnect()
sets.SetDefault()
bts.Connect()
time.Sleep(time.Second * 5)
}
func dropAllTorrent() {
for _, torr := range bts.torrents {
torr.drop()
<-torr.closed
}
}
func Shutdown() {
lockApi.Lock()
defer lockApi.Unlock()
bts.Disconnect()
sets.CloseDB()
log.TLogln("Received shutdown. Quit")
@@ -218,6 +259,8 @@ func WriteStatus(w io.Writer) {
}
func Preload(torr *Torrent, index int) {
lockApi.Lock()
lockApi.Unlock()
cache := float32(sets.BTsets.CacheSize)
preload := float32(sets.BTsets.PreloadCache)
size := int64((cache / 100.0) * preload)

View File

@@ -239,7 +239,7 @@ func (c *Cache) getRemPieces() []*Piece {
}
}
c.updatePriority()
c.clearPriority()
c.muReaders.Lock()
for r, _ := range c.readers {
@@ -328,10 +328,10 @@ func (c *Cache) CloseReader(r *Reader) {
r.Close()
delete(r.cache.readers, r)
r.cache.muReaders.Unlock()
go c.updatePriority()
go c.clearPriority()
}
func (c *Cache) updatePriority() {
func (c *Cache) clearPriority() {
time.Sleep(time.Second)
ranges := make([]Range, 0)
c.muReaders.Lock()

View File

@@ -8,8 +8,6 @@ import (
"sync"
"time"
"github.com/anacrolix/torrent"
"server/log"
"server/settings"
)
@@ -84,6 +82,4 @@ func (p *DiskPiece) Release() {
p.piece.Complete = false
os.Remove(p.name)
p.piece.cache.torrent.Piece(p.piece.Id).SetPriority(torrent.PiecePriorityNone)
}

View File

@@ -4,8 +4,6 @@ import (
"io"
"sync"
"time"
"github.com/anacrolix/torrent"
)
type MemPiece struct {
@@ -69,6 +67,4 @@ func (p *MemPiece) Release() {
}
p.piece.Size = 0
p.piece.Complete = false
p.piece.cache.torrent.Piece(p.piece.Id).SetPriority(torrent.PiecePriorityNone)
}

View File

@@ -1,6 +1,7 @@
package torrstor
import (
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/storage"
"server/settings"
)
@@ -73,9 +74,8 @@ func (p *Piece) Release() {
} else {
p.dPiece.Release()
}
// if !p.cache.isClosed {
// p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone)
// // fix remove pieces hash
// p.cache.torrent.Piece(p.Id).UpdateCompletion()
// }
if !p.cache.isClosed {
p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone)
p.cache.torrent.Piece(p.Id).UpdateCompletion()
}
}

View File

@@ -145,6 +145,7 @@ func (r *Reader) getPieceNum(offset int64) int {
func (r *Reader) getOffsetRange() (int64, int64) {
if time.Now().Unix() > r.lastAccess+60 && len(r.cache.readers) > 1 {
r.SetReadahead(0)
return r.file.Offset(), r.file.Offset()
}

View File

@@ -100,8 +100,8 @@ func (t *Torrent) WaitInfo() bool {
return false
}
// Close torrent if not info while 10 minutes
tm := time.NewTimer(time.Minute * 10)
// Close torrent if not info while 5 minutes
tm := time.NewTimer(time.Minute * 5)
select {
case <-t.Torrent.GotInfo():