From 67fa4aa7a3ed211f9b09a2b2901d5c3fac77ae2b Mon Sep 17 00:00:00 2001 From: Anis Eleuch Date: Mon, 2 Oct 2023 07:03:54 -0700 Subject: [PATCH] Fix mirror stats with --quiet flag (#4703) --- cmd/accounting-reader.go | 16 ++++++++++++---- cmd/progress-bar.go | 8 +++----- cmd/status.go | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cmd/accounting-reader.go b/cmd/accounting-reader.go index d1c9984d..d0c90e53 100644 --- a/cmd/accounting-reader.go +++ b/cmd/accounting-reader.go @@ -32,7 +32,7 @@ import ( type accounter struct { current int64 - Total int64 + total int64 startTime time.Time startValue int64 refreshRate time.Duration @@ -44,7 +44,7 @@ type accounter struct { // Instantiate a new accounter. func newAccounter(total int64) *accounter { acct := &accounter{ - Total: total, + total: total, startTime: time.Now(), startValue: 0, refreshRate: time.Millisecond * 200, @@ -112,7 +112,7 @@ func (a *accounter) Stat() accountStat { var acntStat accountStat a.finishOnce.Do(func() { close(a.isFinished) - acntStat.Total = a.Total + acntStat.Total = a.total acntStat.Transferred = atomic.LoadInt64(&a.current) acntStat.Speed = a.write(atomic.LoadInt64(&a.current)) }) @@ -139,7 +139,8 @@ func (a *accounter) Get() int64 { return atomic.LoadInt64(&a.current) } -func (a *accounter) SetTotal(int64) { +func (a *accounter) SetTotal(n int64) { + atomic.StoreInt64(&a.total, n) } // Add add to current value atomically. @@ -149,6 +150,13 @@ func (a *accounter) Add(n int64) int64 { // Read implements Reader which internally updates current value. func (a *accounter) Read(p []byte) (n int, err error) { + defer func() { + // Upload retry can read one object twice; Avoid read to be greater than Total + if n, t := a.Get(), atomic.LoadInt64(&a.total); t > 0 && n > t { + a.Set(t) + } + }() + n = len(p) a.Add(int64(n)) return diff --git a/cmd/progress-bar.go b/cmd/progress-bar.go index 7554006a..da502dc1 100644 --- a/cmd/progress-bar.go +++ b/cmd/progress-bar.go @@ -105,11 +105,9 @@ func (p *progressBar) Set64(length int64) *progressBar { func (p *progressBar) Read(buf []byte) (n int, err error) { defer func() { - // After updating the internal progress bar, make sure that its - // current progress doesn't exceed the specified total progress - currentProgress := p.ProgressBar.Get() - if currentProgress > p.ProgressBar.Total { - p.ProgressBar.Set64(p.ProgressBar.Total) + // Upload retry can read one object twice; Avoid read to be greater than Total + if n, t := p.ProgressBar.Get(), p.ProgressBar.Total; t > 0 && n > t { + p.ProgressBar.Set64(t) } }() diff --git a/cmd/status.go b/cmd/status.go index a8de58b8..88645d57 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -87,7 +87,7 @@ func (qs *QuietStatus) AddCounts(v int64) { // SetTotal sets the total of the progressbar, ignored for quietstatus func (qs *QuietStatus) SetTotal(v int64) Status { - qs.accounter.Set(v) + qs.accounter.SetTotal(v) return qs }