proxyReader: don't SetTotal inside Read
Vladimir Bauer
4 years ago
| 84 | 84 |
}
|
| 85 | 85 |
|
| 86 | 86 |
// ProxyReader wraps r with metrics required for progress tracking.
|
| 87 | |
// Panics if r is nil.
|
|
87 |
// If r is 'unknown total/size' reader it's mandatory to call
|
|
88 |
// (*Bar).SetTotal(-1, true) method after (Reader).Read returns io.EOF.
|
|
89 |
// Panics if r is nil. If bar is already completed or aborted, returns
|
|
90 |
// nil.
|
| 88 | 91 |
func (b *Bar) ProxyReader(r io.Reader) io.ReadCloser {
|
| 89 | 92 |
if r == nil {
|
| 90 | 93 |
panic("expected non nil io.Reader")
|
| 91 | 94 |
}
|
| 92 | |
return b.newProxyReader(r)
|
|
95 |
select {
|
|
96 |
case <-b.done:
|
|
97 |
return nil
|
|
98 |
default:
|
|
99 |
return b.newProxyReader(r)
|
|
100 |
}
|
| 93 | 101 |
}
|
| 94 | 102 |
|
| 95 | 103 |
// ID returs id of the bar.
|
| 13 | 13 |
func (x proxyReader) Read(p []byte) (int, error) {
|
| 14 | 14 |
n, err := x.ReadCloser.Read(p)
|
| 15 | 15 |
x.bar.IncrBy(n)
|
| 16 | |
if err == io.EOF {
|
| 17 | |
go x.bar.SetTotal(-1, true)
|
| 18 | |
}
|
| 19 | 16 |
return n, err
|
| 20 | 17 |
}
|
| 21 | 18 |
|
|
| 27 | 24 |
func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) {
|
| 28 | 25 |
n, err := x.wt.WriteTo(w)
|
| 29 | 26 |
x.bar.IncrInt64(n)
|
| 30 | |
if err == io.EOF {
|
| 31 | |
go x.bar.SetTotal(-1, true)
|
| 32 | |
}
|
| 33 | 27 |
return n, err
|
| 34 | 28 |
}
|
| 35 | 29 |
|