-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathproxywriter.go
More file actions
76 lines (64 loc) · 1.38 KB
/
proxywriter.go
File metadata and controls
76 lines (64 loc) · 1.38 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 proxyWriteCloser struct {
w io.Writer
bar *Bar
}
func (x proxyWriteCloser) Write(p []byte) (int, error) {
n, err := x.w.Write(p)
x.bar.IncrBy(n)
return n, err
}
func (x proxyWriteCloser) Close() error {
if wc, ok := x.w.(io.WriteCloser); ok {
return wc.Close()
}
return nil
}
type proxyReaderFrom struct {
proxyWriteCloser
rf io.ReaderFrom
}
func (x proxyReaderFrom) ReadFrom(r io.Reader) (int64, error) {
return x.rf.ReadFrom(proxyReadCloser{r, x.bar})
}
type ewmaProxyWriteCloser struct {
w io.Writer
bar *Bar
}
func (x ewmaProxyWriteCloser) Write(p []byte) (int, error) {
start := time.Now()
n, err := x.w.Write(p)
x.bar.EwmaIncrBy(n, time.Since(start))
return n, err
}
func (x ewmaProxyWriteCloser) Close() error {
if wc, ok := x.w.(io.WriteCloser); ok {
return wc.Close()
}
return nil
}
type ewmaProxyReaderFrom struct {
ewmaProxyWriteCloser
rf io.ReaderFrom
}
func (x ewmaProxyReaderFrom) ReadFrom(r io.Reader) (int64, error) {
return x.rf.ReadFrom(ewmaProxyReadCloser{r, x.bar})
}
func newProxyWriter(w io.Writer, b *Bar, hasEwma bool) io.WriteCloser {
if hasEwma {
epw := ewmaProxyWriteCloser{w, b}
if rf, ok := w.(io.ReaderFrom); ok {
return ewmaProxyReaderFrom{epw, rf}
}
return epw
}
pw := proxyWriteCloser{w, b}
if rf, ok := w.(io.ReaderFrom); ok {
return proxyReaderFrom{pw, rf}
}
return pw
}