forked from chennqqi/goproxy-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iolimiter.go
205 lines (185 loc) · 4.6 KB
/
iolimiter.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package iolimiter
import (
"context"
"io"
"net"
"time"
"golang.org/x/time/rate"
)
const burstLimit = 1000 * 1000 * 1000
type Reader struct {
r io.Reader
limiter *rate.Limiter
ctx context.Context
}
type Writer struct {
w io.Writer
limiter *rate.Limiter
ctx context.Context
}
type conn struct {
net.Conn
r io.Reader
w io.Writer
readLimiter *rate.Limiter
writeLimiter *rate.Limiter
ctx context.Context
}
//NewtRateLimitConn sets rate limit (bytes/sec) to the Conn read and write.
func NewtConn(c net.Conn, bytesPerSec float64) net.Conn {
s := &conn{
Conn: c,
r: c,
w: c,
ctx: context.Background(),
}
s.readLimiter = rate.NewLimiter(rate.Limit(bytesPerSec), burstLimit)
s.readLimiter.AllowN(time.Now(), burstLimit) // spend initial burst
s.writeLimiter = rate.NewLimiter(rate.Limit(bytesPerSec), burstLimit)
s.writeLimiter.AllowN(time.Now(), burstLimit) // spend initial burst
return s
}
func NewRateLimitConn(c net.Conn, readLimit, writeLimit float64) net.Conn {
s := &conn{
Conn: c,
r: c,
w: c,
ctx: context.Background(),
}
s.readLimiter = rate.NewLimiter(rate.Limit(readLimit), burstLimit)
s.readLimiter.AllowN(time.Now(), burstLimit) // spend initial burst
s.writeLimiter = rate.NewLimiter(rate.Limit(writeLimit), burstLimit)
s.writeLimiter.AllowN(time.Now(), burstLimit) // spend initial burst
return s
}
//NewtRateLimitReaderConn sets rate limit (bytes/sec) to the Conn read.
func NewReaderConn(c net.Conn, bytesPerSec float64) net.Conn {
s := &conn{
Conn: c,
r: c,
w: c,
ctx: context.Background(),
}
s.readLimiter = rate.NewLimiter(rate.Limit(bytesPerSec), burstLimit)
s.readLimiter.AllowN(time.Now(), burstLimit) // spend initial burst
return s
}
//NewtRateLimitWriterConn sets rate limit (bytes/sec) to the Conn write.
func NewWriterConn(c net.Conn, bytesPerSec float64) net.Conn {
s := &conn{
Conn: c,
r: c,
w: c,
ctx: context.Background(),
}
s.writeLimiter = rate.NewLimiter(rate.Limit(bytesPerSec), burstLimit)
s.writeLimiter.AllowN(time.Now(), burstLimit) // spend initial burst
return s
}
// Read reads bytes into p.
func (s *conn) Read(p []byte) (int, error) {
if s.readLimiter == nil {
return s.r.Read(p)
}
n, err := s.r.Read(p)
if err != nil {
return n, err
}
if err := s.readLimiter.WaitN(s.ctx, n); err != nil {
return n, err
}
return n, nil
}
// Write writes bytes from p.
func (s *conn) Write(p []byte) (int, error) {
if s.writeLimiter == nil {
return s.w.Write(p)
}
n, err := s.w.Write(p)
if err != nil {
return n, err
}
if err := s.writeLimiter.WaitN(s.ctx, n); err != nil {
return n, err
}
return n, err
}
func (s *conn) Close() error {
if s.Conn != nil {
e := s.Conn.Close()
s.Conn = nil
s.r = nil
s.w = nil
s.readLimiter = nil
s.writeLimiter = nil
s.ctx = nil
return e
}
return nil
}
// NewReader returns a reader that implements io.Reader with rate limiting.
func NewReader(r io.Reader) *Reader {
return &Reader{
r: r,
ctx: context.Background(),
}
}
// NewReaderWithContext returns a reader that implements io.Reader with rate limiting.
func NewReaderWithContext(r io.Reader, ctx context.Context) *Reader {
return &Reader{
r: r,
ctx: ctx,
}
}
// NewWriter returns a writer that implements io.Writer with rate limiting.
func NewWriter(w io.Writer) *Writer {
return &Writer{
w: w,
ctx: context.Background(),
}
}
// NewWriterWithContext returns a writer that implements io.Writer with rate limiting.
func NewWriterWithContext(w io.Writer, ctx context.Context) *Writer {
return &Writer{
w: w,
ctx: ctx,
}
}
// SetRateLimit sets rate limit (bytes/sec) to the reader.
func (s *Reader) SetRateLimit(bytesPerSec float64) {
s.limiter = rate.NewLimiter(rate.Limit(bytesPerSec), burstLimit)
s.limiter.AllowN(time.Now(), burstLimit) // spend initial burst
}
// Read reads bytes into p.
func (s *Reader) Read(p []byte) (int, error) {
if s.limiter == nil {
return s.r.Read(p)
}
n, err := s.r.Read(p)
if err != nil {
return n, err
}
if err := s.limiter.WaitN(s.ctx, n); err != nil {
return n, err
}
return n, nil
}
// SetRateLimit sets rate limit (bytes/sec) to the writer.
func (s *Writer) SetRateLimit(bytesPerSec float64) {
s.limiter = rate.NewLimiter(rate.Limit(bytesPerSec), burstLimit)
s.limiter.AllowN(time.Now(), burstLimit) // spend initial burst
}
// Write writes bytes from p.
func (s *Writer) Write(p []byte) (int, error) {
if s.limiter == nil {
return s.w.Write(p)
}
n, err := s.w.Write(p)
if err != nil {
return n, err
}
if err := s.limiter.WaitN(s.ctx, n); err != nil {
return n, err
}
return n, err
}