don't embed time.Now() value inside ewmaProxy...
Vladimir Bauer
4 years ago
| 37 | 37 | type ewmaProxyReader struct { |
| 38 | 38 | io.ReadCloser // *proxyReader |
| 39 | 39 | bar *Bar |
| 40 | iT time.Time | |
| 41 | 40 | } |
| 42 | 41 | |
| 43 | 42 | func (x *ewmaProxyReader) Read(p []byte) (int, error) { |
| 43 | start := time.Now() | |
| 44 | 44 | n, err := x.ReadCloser.Read(p) |
| 45 | 45 | if n > 0 { |
| 46 | x.bar.DecoratorEwmaUpdate(time.Since(x.iT)) | |
| 47 | x.iT = time.Now() | |
| 46 | x.bar.DecoratorEwmaUpdate(time.Since(start)) | |
| 48 | 47 | } |
| 49 | 48 | return n, err |
| 50 | 49 | } |
| 53 | 52 | io.ReadCloser // *ewmaProxyReader |
| 54 | 53 | wt io.WriterTo // *proxyWriterTo |
| 55 | 54 | bar *Bar |
| 56 | iT time.Time | |
| 57 | 55 | } |
| 58 | 56 | |
| 59 | 57 | func (x *ewmaProxyWriterTo) WriteTo(w io.Writer) (int64, error) { |
| 58 | start := time.Now() | |
| 60 | 59 | n, err := x.wt.WriteTo(w) |
| 61 | 60 | if n > 0 { |
| 62 | x.bar.DecoratorEwmaUpdate(time.Since(x.iT)) | |
| 63 | x.iT = time.Now() | |
| 61 | x.bar.DecoratorEwmaUpdate(time.Since(start)) | |
| 64 | 62 | } |
| 65 | 63 | return n, err |
| 66 | 64 | } |
| 70 | 68 | rc = &proxyReader{rc, bar} |
| 71 | 69 | |
| 72 | 70 | if wt, isWriterTo := r.(io.WriterTo); bar.hasEwmaDecorators { |
| 73 | now := time.Now() | |
| 74 | rc = &ewmaProxyReader{rc, bar, now} | |
| 71 | rc = &ewmaProxyReader{rc, bar} | |
| 75 | 72 | if isWriterTo { |
| 76 | rc = &ewmaProxyWriterTo{rc, wt, bar, now} | |
| 73 | rc = &ewmaProxyWriterTo{rc, wt, bar} | |
| 77 | 74 | } |
| 78 | 75 | } else if isWriterTo { |
| 79 | 76 | rc = &proxyWriterTo{rc, wt, bar} |