Codebase list golang-github-vbauerster-mpb / 356b3f7
fixup! refactor newProxyReader Vladimir Bauer 4 years ago
1 changed file(s) with 20 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
1010 bar *Bar
1111 }
1212
13 func (x *proxyReader) Read(p []byte) (int, error) {
13 func (x proxyReader) Read(p []byte) (int, error) {
1414 n, err := x.ReadCloser.Read(p)
1515 x.bar.IncrBy(n)
1616 if err == io.EOF {
2020 }
2121
2222 type proxyWriterTo struct {
23 io.ReadCloser // *proxyReader
24 wt io.WriterTo
25 bar *Bar
23 proxyReader
24 wt io.WriterTo
2625 }
2726
28 func (x *proxyWriterTo) WriteTo(w io.Writer) (int64, error) {
27 func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) {
2928 n, err := x.wt.WriteTo(w)
3029 x.bar.IncrInt64(n)
3130 if err == io.EOF {
3534 }
3635
3736 type ewmaProxyReader struct {
38 io.ReadCloser // *proxyReader
39 bar *Bar
37 proxyReader
4038 }
4139
42 func (x *ewmaProxyReader) Read(p []byte) (int, error) {
40 func (x ewmaProxyReader) Read(p []byte) (int, error) {
4341 start := time.Now()
44 n, err := x.ReadCloser.Read(p)
42 n, err := x.proxyReader.Read(p)
4543 if n > 0 {
4644 x.bar.DecoratorEwmaUpdate(time.Since(start))
4745 }
4947 }
5048
5149 type ewmaProxyWriterTo struct {
52 io.ReadCloser // *ewmaProxyReader
53 wt io.WriterTo // *proxyWriterTo
54 bar *Bar
50 ewmaProxyReader
51 wt proxyWriterTo
5552 }
5653
57 func (x *ewmaProxyWriterTo) WriteTo(w io.Writer) (int64, error) {
54 func (x ewmaProxyWriterTo) WriteTo(w io.Writer) (int64, error) {
5855 start := time.Now()
5956 n, err := x.wt.WriteTo(w)
6057 if n > 0 {
6360 return n, err
6461 }
6562
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}
7269 } else {
73 rc = &ewmaProxyReader{rc, bar}
70 rc = pw
7471 }
75 } else if isWriterTo {
76 rc = &proxyWriterTo{rc, wt, bar}
72 } else if b.hasEwmaDecorators {
73 rc = ewmaProxyReader{pr}
7774 } else {
78 rc = &proxyReader{rc, bar}
75 rc = pr
7976 }
8077 return rc
8178 }