diff --git a/.gitignore b/.gitignore index 286ca62..20afc69 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ # Test binary, build with `go test -c` *.test +# Mac stuff +.DS_Store + # Output of the go coverage tool, specifically when used with LiteIDE *.out diff --git a/docker-all.sh b/docker-all.sh index 882a0f4..37a26d5 100755 --- a/docker-all.sh +++ b/docker-all.sh @@ -6,5 +6,5 @@ ROOT=${PWD} echo "Build web" go run gen_web.go -sudo docker run --rm -v "$PWD":/usr/src/torr -v ~/go/pkg/mod:/go/pkg/mod -w /usr/src/torr golang:1.16.7-stretch ./build-all.sh +sudo docker run --rm -v "$PWD":/usr/src/torr -v ~/go/pkg/mod:/go/pkg/mod -w /usr/src/torr golang:1.17.0-stretch ./build-all.sh sudo chmod 0777 ./dist/* \ No newline at end of file diff --git a/server/go.sum b/server/go.sum index 72e1a3a..d0a23c1 100644 --- a/server/go.sum +++ b/server/go.sum @@ -749,6 +749,7 @@ golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e h1:VvfwVmMH40bpMeizC9/K7ipM5Qjucuu16RWfneFPyhQ= golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= diff --git a/server/torr/apihelper.go b/server/torr/apihelper.go index 1a46ef3..7752654 100644 --- a/server/torr/apihelper.go +++ b/server/torr/apihelper.go @@ -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) diff --git a/server/torr/storage/torrstor/cache.go b/server/torr/storage/torrstor/cache.go index 4620798..e887841 100644 --- a/server/torr/storage/torrstor/cache.go +++ b/server/torr/storage/torrstor/cache.go @@ -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() diff --git a/server/torr/storage/torrstor/diskpiece.go b/server/torr/storage/torrstor/diskpiece.go index f716fe7..b20de74 100644 --- a/server/torr/storage/torrstor/diskpiece.go +++ b/server/torr/storage/torrstor/diskpiece.go @@ -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) } diff --git a/server/torr/storage/torrstor/mempiece.go b/server/torr/storage/torrstor/mempiece.go index 6b69724..acb3074 100644 --- a/server/torr/storage/torrstor/mempiece.go +++ b/server/torr/storage/torrstor/mempiece.go @@ -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) } diff --git a/server/torr/storage/torrstor/piece.go b/server/torr/storage/torrstor/piece.go index dc8c643..f91aac4 100644 --- a/server/torr/storage/torrstor/piece.go +++ b/server/torr/storage/torrstor/piece.go @@ -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() + } } diff --git a/server/torr/storage/torrstor/reader.go b/server/torr/storage/torrstor/reader.go index ce191c1..b4188ba 100644 --- a/server/torr/storage/torrstor/reader.go +++ b/server/torr/storage/torrstor/reader.go @@ -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() } diff --git a/server/torr/torrent.go b/server/torr/torrent.go index b7167db..535eaa1 100644 --- a/server/torr/torrent.go +++ b/server/torr/torrent.go @@ -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(): diff --git a/server/version/version.go b/server/version/version.go index 6acb9b7..49c73f9 100644 --- a/server/version/version.go +++ b/server/version/version.go @@ -1,3 +1,3 @@ package version -const Version = "MatriX.103.NE" +const Version = "MatriX.104.NE"