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

3
.gitignore vendored
View File

@@ -11,6 +11,9 @@
# Test binary, build with `go test -c` # Test binary, build with `go test -c`
*.test *.test
# Mac stuff
.DS_Store
# Output of the go coverage tool, specifically when used with LiteIDE # Output of the go coverage tool, specifically when used with LiteIDE
*.out *.out

View File

@@ -6,5 +6,5 @@ ROOT=${PWD}
echo "Build web" echo "Build web"
go run gen_web.go 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/* sudo chmod 0777 ./dist/*

View File

@@ -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-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-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-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-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 h1:VvfwVmMH40bpMeizC9/K7ipM5Qjucuu16RWfneFPyhQ=
golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20210813211128-0a44fdfbc16e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,7 @@
package torrstor package torrstor
import ( import (
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/storage" "github.com/anacrolix/torrent/storage"
"server/settings" "server/settings"
) )
@@ -73,9 +74,8 @@ func (p *Piece) Release() {
} else { } else {
p.dPiece.Release() p.dPiece.Release()
} }
// if !p.cache.isClosed { if !p.cache.isClosed {
// p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone) p.cache.torrent.Piece(p.Id).SetPriority(torrent.PiecePriorityNone)
// // fix remove pieces hash p.cache.torrent.Piece(p.Id).UpdateCompletion()
// 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) { func (r *Reader) getOffsetRange() (int64, int64) {
if time.Now().Unix() > r.lastAccess+60 && len(r.cache.readers) > 1 { if time.Now().Unix() > r.lastAccess+60 && len(r.cache.readers) > 1 {
r.SetReadahead(0)
return r.file.Offset(), r.file.Offset() return r.file.Offset(), r.file.Offset()
} }

View File

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

View File

@@ -1,3 +1,3 @@
package version package version
const Version = "MatriX.103.NE" const Version = "MatriX.104.NE"