From 9d1c03f56ec068ced427089f7a6824110b4b3b68 Mon Sep 17 00:00:00 2001 From: Chapuis Bertil Date: Tue, 18 Aug 2015 09:26:10 +0200 Subject: [PATCH] Update progress status when necessary --- progress.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/progress.go b/progress.go index 9d61320f9..7215a3206 100644 --- a/progress.go +++ b/progress.go @@ -6,19 +6,22 @@ import ( "time" ) +const minTickerTime = time.Second / 60 + type Progress struct { OnStart func() OnUpdate ProgressFunc OnDone ProgressFunc fnM sync.Mutex - cur Stat - curM sync.Mutex - start time.Time - c *time.Ticker - cancel chan struct{} - o sync.Once - d time.Duration + cur Stat + curM sync.Mutex + start time.Time + c *time.Ticker + cancel chan struct{} + o sync.Once + d time.Duration + lastUpdate time.Time running bool } @@ -92,9 +95,17 @@ func (p *Progress) Report(s Stat) { p.curM.Lock() p.cur.Add(s) cur := p.cur + needUpdate := false + if time.Since(p.lastUpdate) > minTickerTime { + p.lastUpdate = time.Now() + needUpdate = true + } p.curM.Unlock() - p.updateProgress(cur, false) + if needUpdate { + p.updateProgress(cur, false) + } + } func (p *Progress) updateProgress(cur Stat, ticker bool) {