1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #26584 from aaronlehmann/rate-limit-progress-bar

progress: Rate limit progress bar output
This commit is contained in:
Alexander Morozov 2016-09-14 22:15:42 -07:00 committed by GitHub
commit b248de7e33

View file

@ -2,27 +2,32 @@ package progress
import ( import (
"io" "io"
"time"
"golang.org/x/time/rate"
) )
// Reader is a Reader with progress bar. // Reader is a Reader with progress bar.
type Reader struct { type Reader struct {
in io.ReadCloser // Stream to read from in io.ReadCloser // Stream to read from
out Output // Where to send progress bar to out Output // Where to send progress bar to
size int64 size int64
current int64 current int64
lastUpdate int64 lastUpdate int64
id string id string
action string action string
rateLimiter *rate.Limiter
} }
// NewProgressReader creates a new ProgressReader. // NewProgressReader creates a new ProgressReader.
func NewProgressReader(in io.ReadCloser, out Output, size int64, id, action string) *Reader { func NewProgressReader(in io.ReadCloser, out Output, size int64, id, action string) *Reader {
return &Reader{ return &Reader{
in: in, in: in,
out: out, out: out,
size: size, size: size,
id: id, id: id,
action: action, 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) { 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})
}
} }