mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-13 00:26:37 +08:00
refactor: split modules and harden scraping workflows
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package service
|
||||
|
||||
import "time"
|
||||
|
||||
func (d *DownloadService) currentTime() time.Time {
|
||||
if d != nil && d.now != nil {
|
||||
return d.now()
|
||||
}
|
||||
return time.Now()
|
||||
}
|
||||
|
||||
func (d *DownloadService) recordLiveTorrentSnapshot(live []QBitTorrent) {
|
||||
if d == nil {
|
||||
return
|
||||
}
|
||||
snapshot := cloneQBitTorrentSlice(live)
|
||||
d.mu.Lock()
|
||||
d.liveTorrents = snapshot
|
||||
d.liveTorrentsAt = d.currentTime()
|
||||
d.mu.Unlock()
|
||||
}
|
||||
|
||||
func (d *DownloadService) LiveTorrentSnapshot(maxAge time.Duration) []QBitTorrent {
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
now := d.currentTime()
|
||||
d.mu.Lock()
|
||||
defer d.mu.Unlock()
|
||||
if d.liveTorrentsAt.IsZero() {
|
||||
return nil
|
||||
}
|
||||
if maxAge > 0 && now.Sub(d.liveTorrentsAt) > maxAge {
|
||||
return nil
|
||||
}
|
||||
return cloneQBitTorrentSlice(d.liveTorrents)
|
||||
}
|
||||
|
||||
func cloneQBitTorrentSlice(in []QBitTorrent) []QBitTorrent {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
return append([]QBitTorrent(nil), in...)
|
||||
}
|
||||
Reference in New Issue
Block a user