proxyReader: manual trigger complete only if total is unknown
Vladimir Bauer
4 years ago
| 89 | 89 |
if r == nil {
|
| 90 | 90 |
panic("expected non nil io.Reader")
|
| 91 | 91 |
}
|
| 92 | |
return b.newProxyReader(r)
|
|
92 |
result := make(chan bool)
|
|
93 |
select {
|
|
94 |
case b.operateState <- func(s *bState) { result <- s.triggerComplete }:
|
|
95 |
triggerComplete := <-result
|
|
96 |
return b.newProxyReader(r, !triggerComplete)
|
|
97 |
case <-b.done:
|
|
98 |
return nil
|
|
99 |
}
|
| 93 | 100 |
}
|
| 94 | 101 |
|
| 95 | 102 |
// ID returs id of the bar.
|
| 7 | 7 |
|
| 8 | 8 |
type proxyReader struct {
|
| 9 | 9 |
io.ReadCloser
|
| 10 | |
bar *Bar
|
|
10 |
bar *Bar
|
|
11 |
totalUnknown bool
|
| 11 | 12 |
}
|
| 12 | 13 |
|
| 13 | 14 |
func (x proxyReader) Read(p []byte) (int, error) {
|
| 14 | 15 |
n, err := x.ReadCloser.Read(p)
|
| 15 | 16 |
x.bar.IncrBy(n)
|
| 16 | |
if err == io.EOF {
|
|
17 |
if x.totalUnknown && err == io.EOF {
|
| 17 | 18 |
go x.bar.SetTotal(-1, true)
|
| 18 | 19 |
}
|
| 19 | 20 |
return n, err
|
|
| 27 | 28 |
func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) {
|
| 28 | 29 |
n, err := x.wt.WriteTo(w)
|
| 29 | 30 |
x.bar.IncrInt64(n)
|
| 30 | |
if err == io.EOF {
|
|
31 |
if x.totalUnknown && err == io.EOF {
|
| 31 | 32 |
go x.bar.SetTotal(-1, true)
|
| 32 | 33 |
}
|
| 33 | 34 |
return n, err
|
|
| 60 | 61 |
return n, err
|
| 61 | 62 |
}
|
| 62 | 63 |
|
| 63 | |
func (b *Bar) newProxyReader(r io.Reader) (rc io.ReadCloser) {
|
| 64 | |
pr := proxyReader{toReadCloser(r), b}
|
|
64 |
func (b *Bar) newProxyReader(r io.Reader, totalUnknown bool) (rc io.ReadCloser) {
|
|
65 |
pr := proxyReader{toReadCloser(r), b, totalUnknown}
|
| 65 | 66 |
if wt, ok := r.(io.WriterTo); ok {
|
| 66 | 67 |
pw := proxyWriterTo{pr, wt}
|
| 67 | 68 |
if b.hasEwma {
|