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
# Mac stuff
.DS_Store
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

View File

@@ -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/*

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-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=

View File

@@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"sort"
"sync"
"time"
"github.com/anacrolix/torrent"
@@ -17,6 +18,7 @@ import (
var (
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():

View File

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