From f8eab6fea85f92aaa3c23bd0c87451a0811b5e3a Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Wed, 14 Sep 2016 15:53:24 -0700 Subject: [PATCH] progress: Rate limit progress bar output ProgressReader outputs progress information every 500 KB. This could be excessive if something is being transfered at a fast rate. Rate-limit progress output to 10 per second. Signed-off-by: Aaron Lehmann --- pkg/progress/progressreader.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/pkg/progress/progressreader.go b/pkg/progress/progressreader.go index c39e2b69fb..6b3927eecf 100644 --- a/pkg/progress/progressreader.go +++ b/pkg/progress/progressreader.go @@ -2,27 +2,32 @@ package progress import ( "io" + "time" + + "golang.org/x/time/rate" ) // Reader is a Reader with progress bar. type Reader struct { - in io.ReadCloser // Stream to read from - out Output // Where to send progress bar to - size int64 - current int64 - lastUpdate int64 - id string - action string + in io.ReadCloser // Stream to read from + out Output // Where to send progress bar to + size int64 + current int64 + lastUpdate int64 + id string + action string + rateLimiter *rate.Limiter } // NewProgressReader creates a new ProgressReader. func NewProgressReader(in io.ReadCloser, out Output, size int64, id, action string) *Reader { return &Reader{ - in: in, - out: out, - size: size, - id: id, - action: action, + in: in, + out: out, + size: size, + id: id, + action: action, + rateLimiter: rate.NewLimiter(rate.Every(100*time.Millisecond), 1), } } @@ -55,5 +60,7 @@ func (p *Reader) Close() error { } func (p *Reader) updateProgress(last bool) { - p.out.WriteProgress(Progress{ID: p.id, Action: p.action, Current: p.current, Total: p.size, LastUpdate: last}) + if last || p.current == p.size || p.rateLimiter.Allow() { + p.out.WriteProgress(Progress{ID: p.id, Action: p.action, Current: p.current, Total: p.size, LastUpdate: last}) + } }