forked from cortesi/devd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslowdown.go
185 lines (161 loc) · 4.86 KB
/
slowdown.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Package slowdown provides an implementation of net.Listener that limits
// bandwidth.
package slowdown
import (
"io"
"net"
"time"
"github.com/juju/ratelimit"
)
// The maximum rate you should specify for readrate or writerate.If this is too
// high, the token bucket implementation seems to break down.
var MaxRate uint = (1024 * 1024) * 1000
var blockSize = int64(1024)
var capacity = int64(blockSize * 4)
type slowReader struct {
reader io.Reader
bucket *ratelimit.Bucket
}
func (sr *slowReader) Read(b []byte) (n int, err error) {
read := 0
for read < len(b) {
sr.bucket.Wait(blockSize)
upper := int64(read) + blockSize
if upper > int64(len(b)) {
upper = int64(len(b))
}
slice := b[read:upper]
n, err := sr.reader.Read(slice)
read += n
if err != nil || n < len(slice) {
return read, err
}
}
return read, nil
}
type slowWriter struct {
writer io.Writer
bucket *ratelimit.Bucket
}
func (w *slowWriter) Write(b []byte) (n int, err error) {
written := 0
for written < len(b) {
w.bucket.Wait(blockSize)
upper := int64(written) + blockSize
if upper > int64(len(b)) {
upper = int64(len(b))
}
n, err := w.writer.Write(b[written:upper])
written += n
if err != nil {
return written, err
}
}
return written, nil
}
// SlowConn is a slow connection
type SlowConn struct {
conn net.Conn
listener *SlowListener
reader *slowReader
writer *slowWriter
}
func newSlowConn(conn net.Conn, listener *SlowListener) *SlowConn {
return &SlowConn{
conn,
listener,
&slowReader{conn, listener.readbucket},
&slowWriter{conn, listener.writebucket},
}
}
// Read reads data from the connection.
// Read can be made to time out and return a Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetReadDeadline.
func (sc *SlowConn) Read(b []byte) (n int, err error) {
return sc.reader.reader.Read(b)
}
// Write writes data to the connection.
// Write can be made to time out and return a Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetWriteDeadline.
func (sc *SlowConn) Write(b []byte) (n int, err error) {
return sc.writer.Write(b)
}
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (sc *SlowConn) Close() error {
return sc.conn.Close()
}
// LocalAddr returns the local network address.
func (sc *SlowConn) LocalAddr() net.Addr {
return sc.conn.LocalAddr()
}
// RemoteAddr returns the remote network address.
func (sc *SlowConn) RemoteAddr() net.Addr {
return sc.conn.RemoteAddr()
}
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future I/O, not just
// the immediately following call to Read or Write.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
func (sc *SlowConn) SetDeadline(t time.Time) error {
return sc.conn.SetDeadline(t)
}
// SetReadDeadline sets the deadline for future Read calls.
// A zero value for t means Read will not time out.
func (sc *SlowConn) SetReadDeadline(t time.Time) error {
return sc.conn.SetReadDeadline(t)
}
// SetWriteDeadline sets the deadline for future Write calls.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
func (sc *SlowConn) SetWriteDeadline(t time.Time) error {
return sc.conn.SetWriteDeadline(t)
}
// SlowListener is a listener that limits global IO over all connections
type SlowListener struct {
listener net.Listener
readbucket *ratelimit.Bucket
writebucket *ratelimit.Bucket
}
// NewSlowListener creates a SlowListener with specified read and write rates.
// Both the readrate and the writerate are specified in bytes per second. A
// value of 0 disables throttling.
func NewSlowListener(listener net.Listener, readrate uint, writerate uint) net.Listener {
if readrate == 0 {
readrate = MaxRate
}
if writerate == 0 {
writerate = MaxRate
}
return &SlowListener{
listener: listener,
readbucket: ratelimit.NewBucketWithRate(float64(readrate), capacity),
writebucket: ratelimit.NewBucketWithRate(float64(writerate), capacity),
}
}
// Accept waits for and returns the next connection to the listener.
func (l *SlowListener) Accept() (net.Conn, error) {
conn, err := l.listener.Accept()
if err != nil {
return nil, err
}
return newSlowConn(conn, l), nil
}
// Close closes the listener.
func (l *SlowListener) Close() error {
return l.listener.Close()
}
// Addr returns the listener's network address.
func (l *SlowListener) Addr() net.Addr {
return l.listener.Addr()
}