fix close not called
Vladimir Bauer
3 years ago
| 5 | 5 | ) |
| 6 | 6 | |
| 7 | 7 | type proxyReader struct { |
| 8 | io.Reader | |
| 8 | io.ReadCloser | |
| 9 | 9 | bar *Bar |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | func (x proxyReader) Read(p []byte) (int, error) { |
| 13 | n, err := x.Reader.Read(p) | |
| 13 | n, err := x.ReadCloser.Read(p) | |
| 14 | 14 | x.bar.IncrBy(n) |
| 15 | 15 | return n, err |
| 16 | 16 | } |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) { |
| 23 | n, err := x.Reader.(io.WriterTo).WriteTo(w) | |
| 23 | n, err := x.ReadCloser.(io.WriterTo).WriteTo(w) | |
| 24 | 24 | x.bar.IncrInt64(n) |
| 25 | 25 | return n, err |
| 26 | 26 | } |
| 44 | 44 | |
| 45 | 45 | func (x ewmaProxyWriterTo) WriteTo(w io.Writer) (int64, error) { |
| 46 | 46 | start := time.Now() |
| 47 | n, err := x.Reader.(io.WriterTo).WriteTo(w) | |
| 47 | n, err := x.ReadCloser.(io.WriterTo).WriteTo(w) | |
| 48 | 48 | if n > 0 { |
| 49 | 49 | x.bar.DecoratorEwmaUpdate(time.Since(start)) |
| 50 | 50 | } |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | func newProxyReader(r io.Reader, b *Bar, hasEwma bool) io.ReadCloser { |
| 55 | return toReadCloser(toReader(r, b, hasEwma)) | |
| 56 | } | |
| 57 | ||
| 58 | func toReader(r io.Reader, b *Bar, hasEwma bool) io.Reader { | |
| 59 | pr := proxyReader{r, b} | |
| 55 | pr := proxyReader{toReadCloser(r), b} | |
| 60 | 56 | if hasEwma { |
| 61 | 57 | epr := ewmaProxyReader{pr} |
| 62 | 58 | if _, ok := r.(io.WriterTo); ok { |