fixup! refactor newProxyReader
Vladimir Bauer
4 years ago
| 10 | 10 | bar *Bar |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | func (x *proxyReader) Read(p []byte) (int, error) { | |
| 13 | func (x proxyReader) Read(p []byte) (int, error) { | |
| 14 | 14 | n, err := x.ReadCloser.Read(p) |
| 15 | 15 | x.bar.IncrBy(n) |
| 16 | 16 | if err == io.EOF { |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | type proxyWriterTo struct { |
| 23 | io.ReadCloser // *proxyReader | |
| 24 | wt io.WriterTo | |
| 25 | bar *Bar | |
| 23 | proxyReader | |
| 24 | wt io.WriterTo | |
| 26 | 25 | } |
| 27 | 26 | |
| 28 | func (x *proxyWriterTo) WriteTo(w io.Writer) (int64, error) { | |
| 27 | func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) { | |
| 29 | 28 | n, err := x.wt.WriteTo(w) |
| 30 | 29 | x.bar.IncrInt64(n) |
| 31 | 30 | if err == io.EOF { |
| 35 | 34 | } |
| 36 | 35 | |
| 37 | 36 | type ewmaProxyReader struct { |
| 38 | io.ReadCloser // *proxyReader | |
| 39 | bar *Bar | |
| 37 | proxyReader | |
| 40 | 38 | } |
| 41 | 39 | |
| 42 | func (x *ewmaProxyReader) Read(p []byte) (int, error) { | |
| 40 | func (x ewmaProxyReader) Read(p []byte) (int, error) { | |
| 43 | 41 | start := time.Now() |
| 44 | n, err := x.ReadCloser.Read(p) | |
| 42 | n, err := x.proxyReader.Read(p) | |
| 45 | 43 | if n > 0 { |
| 46 | 44 | x.bar.DecoratorEwmaUpdate(time.Since(start)) |
| 47 | 45 | } |
| 49 | 47 | } |
| 50 | 48 | |
| 51 | 49 | type ewmaProxyWriterTo struct { |
| 52 | io.ReadCloser // *ewmaProxyReader | |
| 53 | wt io.WriterTo // *proxyWriterTo | |
| 54 | bar *Bar | |
| 50 | ewmaProxyReader | |
| 51 | wt proxyWriterTo | |
| 55 | 52 | } |
| 56 | 53 | |
| 57 | func (x *ewmaProxyWriterTo) WriteTo(w io.Writer) (int64, error) { | |
| 54 | func (x ewmaProxyWriterTo) WriteTo(w io.Writer) (int64, error) { | |
| 58 | 55 | start := time.Now() |
| 59 | 56 | n, err := x.wt.WriteTo(w) |
| 60 | 57 | if n > 0 { |
| 63 | 60 | return n, err |
| 64 | 61 | } |
| 65 | 62 | |
| 66 | func (bar *Bar) newProxyReader(r io.Reader) io.ReadCloser { | |
| 67 | rc := toReadCloser(r) | |
| 68 | wt, isWriterTo := r.(io.WriterTo) | |
| 69 | if bar.hasEwmaDecorators { | |
| 70 | if isWriterTo { | |
| 71 | rc = &ewmaProxyWriterTo{rc, wt, bar} | |
| 63 | func (b *Bar) newProxyReader(r io.Reader) (rc io.ReadCloser) { | |
| 64 | pr := proxyReader{toReadCloser(r), b} | |
| 65 | if wt, ok := r.(io.WriterTo); ok { | |
| 66 | pw := proxyWriterTo{pr, wt} | |
| 67 | if b.hasEwmaDecorators { | |
| 68 | rc = ewmaProxyWriterTo{ewmaProxyReader{pr}, pw} | |
| 72 | 69 | } else { |
| 73 | rc = &ewmaProxyReader{rc, bar} | |
| 70 | rc = pw | |
| 74 | 71 | } |
| 75 | } else if isWriterTo { | |
| 76 | rc = &proxyWriterTo{rc, wt, bar} | |
| 72 | } else if b.hasEwmaDecorators { | |
| 73 | rc = ewmaProxyReader{pr} | |
| 77 | 74 | } else { |
| 78 | rc = &proxyReader{rc, bar} | |
| 75 | rc = pr | |
| 79 | 76 | } |
| 80 | 77 | return rc |
| 81 | 78 | } |