refactor expired time

This commit is contained in:
YouROK
2020-12-30 10:19:41 +03:00
parent 34f81ed64f
commit 237d9e9fde
2 changed files with 9 additions and 5 deletions

View File

@@ -54,7 +54,7 @@ func GetTorrent(hashHex string) *Torrent {
hash := metainfo.NewHashFromHex(hashHex) hash := metainfo.NewHashFromHex(hashHex)
tor := bts.GetTorrent(hash) tor := bts.GetTorrent(hash)
if tor != nil { if tor != nil {
tor.expiredTime = time.Now().Add(time.Minute) tor.AddExpiredTime(time.Minute)
return tor return tor
} }

View File

@@ -7,7 +7,7 @@ import (
"sort" "sort"
"time" "time"
sync "github.com/sasha-s/go-deadlock" "sync"
"server/log" "server/log"
"server/settings" "server/settings"
@@ -84,7 +84,7 @@ func NewTorrent(spec *torrent.TorrentSpec, bt *BTServer) (*Torrent, error) {
torr.bt = bt torr.bt = bt
torr.closed = goTorrent.Closed() torr.closed = goTorrent.Closed()
torr.TorrentSpec = spec torr.TorrentSpec = spec
torr.expiredTime = time.Now().Add(time.Minute) torr.AddExpiredTime(time.Minute)
torr.Timestamp = time.Now().Unix() torr.Timestamp = time.Now().Unix()
go torr.watch() go torr.watch()
@@ -120,7 +120,7 @@ func (t *Torrent) GotInfo() bool {
t.Stat = state.TorrentGettingInfo t.Stat = state.TorrentGettingInfo
if t.WaitInfo() { if t.WaitInfo() {
t.Stat = state.TorrentWorking t.Stat = state.TorrentWorking
t.expiredTime = time.Now().Add(time.Minute * 5) t.AddExpiredTime(time.Minute * 5)
return true return true
} else { } else {
t.Close() t.Close()
@@ -128,6 +128,10 @@ func (t *Torrent) GotInfo() bool {
} }
} }
func (t *Torrent) AddExpiredTime(duration time.Duration) {
t.expiredTime = time.Now().Add(duration)
}
func (t *Torrent) watch() { func (t *Torrent) watch() {
t.progressTicker = time.NewTicker(time.Second) t.progressTicker = time.NewTicker(time.Second)
defer t.progressTicker.Stop() defer t.progressTicker.Stop()
@@ -228,7 +232,7 @@ func (t *Torrent) NewReader(file *torrent.File) *torrstor.Reader {
func (t *Torrent) CloseReader(reader *torrstor.Reader) { func (t *Torrent) CloseReader(reader *torrstor.Reader) {
t.cache.CloseReader(reader) t.cache.CloseReader(reader)
t.expiredTime = time.Now().Add(time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout)) t.AddExpiredTime(time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout))
} }
func (t *Torrent) GetCache() *torrstor.Cache { func (t *Torrent) GetCache() *torrstor.Cache {