2018-02-05 16:05:59 -05:00
|
|
|
package progress // import "github.com/docker/docker/pkg/progress"
|
2015-11-13 19:59:01 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-09-14 18:53:24 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/time/rate"
|
2015-11-13 19:59:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Reader is a Reader with progress bar.
|
|
|
|
type Reader struct {
|
2016-09-14 18:53:24 -04:00
|
|
|
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
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProgressReader creates a new ProgressReader.
|
|
|
|
func NewProgressReader(in io.ReadCloser, out Output, size int64, id, action string) *Reader {
|
|
|
|
return &Reader{
|
2016-09-14 18:53:24 -04:00
|
|
|
in: in,
|
|
|
|
out: out,
|
|
|
|
size: size,
|
|
|
|
id: id,
|
|
|
|
action: action,
|
|
|
|
rateLimiter: rate.NewLimiter(rate.Every(100*time.Millisecond), 1),
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Reader) Read(buf []byte) (n int, err error) {
|
|
|
|
read, err := p.in.Read(buf)
|
|
|
|
p.current += int64(read)
|
|
|
|
updateEvery := int64(1024 * 512) //512kB
|
|
|
|
if p.size > 0 {
|
|
|
|
// Update progress for every 1% read if 1% < 512kB
|
|
|
|
if increment := int64(0.01 * float64(p.size)); increment < updateEvery {
|
|
|
|
updateEvery = increment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.current-p.lastUpdate > updateEvery || err != nil {
|
|
|
|
p.updateProgress(err != nil && read == 0)
|
|
|
|
p.lastUpdate = p.current
|
|
|
|
}
|
|
|
|
|
|
|
|
return read, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the progress reader and its underlying reader.
|
|
|
|
func (p *Reader) Close() error {
|
|
|
|
if p.current < p.size {
|
|
|
|
// print a full progress bar when closing prematurely
|
|
|
|
p.current = p.size
|
|
|
|
p.updateProgress(false)
|
|
|
|
}
|
|
|
|
return p.in.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Reader) updateProgress(last bool) {
|
2016-09-14 18:53:24 -04:00
|
|
|
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})
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|