Skip to content

Commit

Permalink
proxyReader: don't SetTotal inside Read
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Feb 15, 2022
1 parent 3f5e7a5 commit 5e2ae42
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 10 additions & 2 deletions bar.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,20 @@ func newBar(container *Progress, bs *bState) *Bar {
}

// ProxyReader wraps r with metrics required for progress tracking.
// Panics if r is nil.
// If r is 'unknown total/size' reader it's mandatory to call
// (*Bar).SetTotal(-1, true) method after (Reader).Read returns io.EOF.
// Panics if r is nil. If bar is already completed or aborted, returns
// nil.
func (b *Bar) ProxyReader(r io.Reader) io.ReadCloser {
if r == nil {
panic("expected non nil io.Reader")
}
return b.newProxyReader(r)
select {
case <-b.done:
return nil
default:
return b.newProxyReader(r)
}
}

// ID returs id of the bar.
Expand Down
6 changes: 0 additions & 6 deletions proxyreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ type proxyReader struct {
func (x proxyReader) Read(p []byte) (int, error) {
n, err := x.ReadCloser.Read(p)
x.bar.IncrBy(n)
if err == io.EOF {
go x.bar.SetTotal(-1, true)
}
return n, err
}

Expand All @@ -28,9 +25,6 @@ type proxyWriterTo struct {
func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) {
n, err := x.wt.WriteTo(w)
x.bar.IncrInt64(n)
if err == io.EOF {
go x.bar.SetTotal(-1, true)
}
return n, err
}

Expand Down

0 comments on commit 5e2ae42

Please sign in to comment.