Browse code

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 <aaron.lehmann@docker.com>

Aaron Lehmann authored on 2016/09/15 07:53:24
Showing 1 changed files
... ...
@@ -2,27 +2,32 @@ package progress
2 2
 
3 3
 import (
4 4
 	"io"
5
+	"time"
6
+
7
+	"golang.org/x/time/rate"
5 8
 )
6 9
 
7 10
 // Reader is a Reader with progress bar.
8 11
 type Reader struct {
9
-	in         io.ReadCloser // Stream to read from
10
-	out        Output        // Where to send progress bar to
11
-	size       int64
12
-	current    int64
13
-	lastUpdate int64
14
-	id         string
15
-	action     string
12
+	in          io.ReadCloser // Stream to read from
13
+	out         Output        // Where to send progress bar to
14
+	size        int64
15
+	current     int64
16
+	lastUpdate  int64
17
+	id          string
18
+	action      string
19
+	rateLimiter *rate.Limiter
16 20
 }
17 21
 
18 22
 // NewProgressReader creates a new ProgressReader.
19 23
 func NewProgressReader(in io.ReadCloser, out Output, size int64, id, action string) *Reader {
20 24
 	return &Reader{
21
-		in:     in,
22
-		out:    out,
23
-		size:   size,
24
-		id:     id,
25
-		action: action,
25
+		in:          in,
26
+		out:         out,
27
+		size:        size,
28
+		id:          id,
29
+		action:      action,
30
+		rateLimiter: rate.NewLimiter(rate.Every(100*time.Millisecond), 1),
26 31
 	}
27 32
 }
28 33
 
... ...
@@ -55,5 +60,7 @@ func (p *Reader) Close() error {
55 55
 }
56 56
 
57 57
 func (p *Reader) updateProgress(last bool) {
58
-	p.out.WriteProgress(Progress{ID: p.id, Action: p.action, Current: p.current, Total: p.size, LastUpdate: last})
58
+	if last || p.current == p.size || p.rateLimiter.Allow() {
59
+		p.out.WriteProgress(Progress{ID: p.id, Action: p.action, Current: p.current, Total: p.size, LastUpdate: last})
60
+	}
59 61
 }