-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathproxyreader.go
More file actions
76 lines (64 loc) · 1.34 KB
/
proxyreader.go
File metadata and controls
76 lines (64 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package mpb
import (
"io"
"time"
)
type proxyReadCloser struct {
r io.Reader
bar *Bar
}
func (x proxyReadCloser) Read(p []byte) (int, error) {
n, err := x.r.Read(p)
x.bar.IncrBy(n)
return n, err
}
func (x proxyReadCloser) Close() error {
if rc, ok := x.r.(io.ReadCloser); ok {
return rc.Close()
}
return nil
}
type proxyWriterTo struct {
proxyReadCloser
wt io.WriterTo
}
func (x proxyWriterTo) WriteTo(w io.Writer) (int64, error) {
return x.wt.WriteTo(proxyWriteCloser{w, x.bar})
}
type ewmaProxyReadCloser struct {
r io.Reader
bar *Bar
}
func (x ewmaProxyReadCloser) Read(p []byte) (int, error) {
start := time.Now()
n, err := x.r.Read(p)
x.bar.EwmaIncrBy(n, time.Since(start))
return n, err
}
func (x ewmaProxyReadCloser) Close() error {
if rc, ok := x.r.(io.ReadCloser); ok {
return rc.Close()
}
return nil
}
type ewmaProxyWriterTo struct {
ewmaProxyReadCloser
wt io.WriterTo
}
func (x ewmaProxyWriterTo) WriteTo(w io.Writer) (int64, error) {
return x.wt.WriteTo(ewmaProxyWriteCloser{w, x.bar})
}
func newProxyReader(r io.Reader, b *Bar, hasEwma bool) io.ReadCloser {
if hasEwma {
epr := ewmaProxyReadCloser{r, b}
if wt, ok := r.(io.WriterTo); ok {
return ewmaProxyWriterTo{epr, wt}
}
return epr
}
pr := proxyReadCloser{r, b}
if wt, ok := r.(io.WriterTo); ok {
return proxyWriterTo{pr, wt}
}
return pr
}