Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Commit

Permalink
Remove copy & clone not needed after removal of atomics (#1120)
Browse files Browse the repository at this point in the history
I added these methods to fix an erroneous copy of a struct with atomics,
now these have been remove these methods are not necessary.
  • Loading branch information
mh0lt committed Sep 10, 2023
1 parent 9b4a1c6 commit 18ee8c5
Showing 1 changed file with 4 additions and 44 deletions.
48 changes: 4 additions & 44 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,6 @@ type AggStats struct {
UploadRate, DownloadRate uint64
}

func (s *AggStats) clone() *AggStats {
sc := &AggStats{
MetadataReady: s.MetadataReady,
FilesTotal: s.FilesTotal,
PeersUnique: s.PeersUnique,
ConnectionsTotal: s.ConnectionsTotal,
Completed: s.Completed,
Progress: s.Progress,
BytesCompleted: s.BytesCompleted,
BytesTotal: s.BytesTotal,
BytesDownload: s.BytesDownload,
BytesUpload: s.BytesUpload,
UploadRate: s.UploadRate,
DownloadRate: s.DownloadRate,
}
atomic.StoreUint64(&sc.DroppedCompleted, atomic.LoadUint64(&s.DroppedCompleted))
atomic.StoreUint64(&sc.DroppedTotal, atomic.LoadUint64(&s.DroppedTotal))

return sc
}

func (s *AggStats) copy(o *AggStats) *AggStats {
s.MetadataReady = o.MetadataReady
s.FilesTotal = o.FilesTotal
s.PeersUnique = o.PeersUnique
s.ConnectionsTotal = o.ConnectionsTotal
s.Completed = o.Completed
s.Progress = o.Progress
s.BytesCompleted = o.BytesCompleted
s.BytesTotal = o.BytesTotal
s.BytesDownload = o.BytesDownload
s.BytesUpload = o.BytesUpload
s.UploadRate = o.UploadRate
s.DownloadRate = o.DownloadRate
atomic.StoreUint64(&s.DroppedCompleted, atomic.LoadUint64(&o.DroppedCompleted))
atomic.StoreUint64(&s.DroppedTotal, atomic.LoadUint64(&o.DroppedTotal))

return s
}

func New(ctx context.Context, cfg *downloadercfg.Cfg) (*Downloader, error) {
if err := portMustBeTCPAndUDPOpen(cfg.ListenPort); err != nil {
return nil, err
Expand Down Expand Up @@ -340,7 +300,7 @@ func (d *Downloader) ReCalcStats(interval time.Duration) {

d.statsLock.Lock()
defer d.statsLock.Unlock()
prevStats, stats := d.stats.clone(), d.stats.clone()
prevStats, stats := d.stats, d.stats

stats.Completed = true
stats.BytesDownload = uint64(connStats.BytesReadUsefulIntendedData.Int64())
Expand Down Expand Up @@ -382,7 +342,7 @@ func (d *Downloader) ReCalcStats(interval time.Duration) {
stats.PeersUnique = int32(len(peers))
stats.FilesTotal = int32(len(torrents))

d.stats.copy(stats)
d.stats = stats
}

func moveFromTmp(snapDir string) error {
Expand Down Expand Up @@ -580,10 +540,10 @@ func (d *Downloader) addSegments(ctx context.Context) error {
return nil
}

func (d *Downloader) Stats() *AggStats {
func (d *Downloader) Stats() AggStats {
d.statsLock.RLock()
defer d.statsLock.RUnlock()
return d.stats.clone()
return d.stats
}

func (d *Downloader) Close() {
Expand Down

0 comments on commit 18ee8c5

Please sign in to comment.