-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
182 lines (148 loc) · 4.22 KB
/
conn.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
package policied
import (
"errors"
"math"
"net"
"sync"
"sync/atomic"
"time"
"unsafe"
"golang.org/x/time/rate"
)
var ErrConnClosed = errors.New("connection is closed")
type WrappedConn struct {
conn net.Conn
bps uint64
maxChunk uint64
chunkSize uint64
BurstFactor *BurstFactor
limiter *rate.Limiter
closed bool
onceCloser sync.Once
globalLimiterFunc func() *rate.Limiter
}
//func WrapConn(conn net.Conn, bps uint64, check chan<- uint32, release <-chan struct{}) net.Conn {
func WrapConn(conn net.Conn, bps uint64, maxChunk uint64, burstFactor *BurstFactor,
glf func() *rate.Limiter) *WrappedConn {
wc := WrappedConn{
conn: conn,
bps: bps, // bytes, not bits
limiter: rate.NewLimiter(rate.Inf, 0),
BurstFactor: burstFactor,
globalLimiterFunc:glf,
}
wc.setRate(bps, maxChunk)
return &wc
}
// internal, called from policed
func (c *WrappedConn) setRate(bps uint64, maxChunk uint64) {
atomic.StoreUint64(&c.bps, bps)
chunk := c.calcChunk(maxChunk)
limit := rate.Limit(bps)
if bps == 0 {
limit = rate.Inf
}
c.setLimiter(rate.NewLimiter(limit, int(chunk)))
}
// SetRate allows to set individual rate per each connection - public interface
func (c *WrappedConn) SetRate(kbps uint64) {
bps := kbps * 1024
atomic.StoreUint64(&c.bps, bps)
maxChunk := atomic.LoadUint64(&c.maxChunk)
chunk := c.calcChunk(maxChunk)
limit := rate.Limit(bps)
if bps == 0 {
limit = rate.Inf
}
c.setLimiter(rate.NewLimiter(limit, int(chunk)))
}
// Write to original connection, limiting with both conn/global rate limits
func (c *WrappedConn) Write(b []byte) (int, error) {
limiter := c.getCurrentLimiter()
chunkSize := atomic.LoadUint64(&c.chunkSize)
bps := atomic.LoadUint64(&c.bps)
if chunkSize == 0 && bps == 0 {
return c.conn.Write(b)
}
// i wish go had cpp iterators...
var wrote int
var i uint64
bytes := uint64(len(b))
for i = 0; i < bytes; i += chunkSize {
now := time.Now()
// first check global limits, then local
if i+chunkSize > bytes {
// send whole chunkSize
toWrite := chunkSize - (i + chunkSize - bytes)
globalLimiter := c.globalLimiterFunc()
globalReservation := globalLimiter.ReserveN(now, int(toWrite))
time.Sleep(globalReservation.DelayFrom(now))
reservation := limiter.ReserveN(now, int(toWrite))
time.Sleep(reservation.DelayFrom(now))
n, err := c.conn.Write(b[i:])
wrote += n
return wrote, err
}
// send partial chunkSize
globalLimiter := c.globalLimiterFunc()
globalReservation := globalLimiter.ReserveN(now, int(chunkSize))
time.Sleep(globalReservation.DelayFrom(now))
reservation := limiter.ReserveN(now, int(chunkSize))
time.Sleep(reservation.DelayFrom(now))
n, err := c.conn.Write(b[i : i+chunkSize])
wrote += n
if err != nil {
return wrote, err
}
}
return wrote, nil
}
// Calculate chunk
func (c *WrappedConn) calcChunk(max uint64) uint64 {
bps := atomic.LoadUint64(&c.bps)
atomic.StoreUint64(&c.maxChunk, max)
var chunkSize uint64
if bps == 0 {
chunkSize = max
} else {
chunkSize = uint64(math.Ceil(float64(bps) * c.BurstFactor.Get()))
}
atomic.StoreUint64(&c.chunkSize, chunkSize)
c.setLimiter(rate.NewLimiter(rate.Limit(bps), int(chunkSize)))
return chunkSize
}
func (c *WrappedConn) Read(b []byte) (n int, err error) {
return c.conn.Read(b)
}
func (c *WrappedConn) Close() error {
if c.closed {
return ErrConnClosed
}
c.onceCloser.Do(func() {
c.closed = true
})
return c.conn.Close()
}
func (c *WrappedConn) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
func (c *WrappedConn) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *WrappedConn) SetDeadline(t time.Time) error {
return c.conn.SetDeadline(t)
}
func (c *WrappedConn) SetReadDeadline(t time.Time) error {
return c.conn.SetReadDeadline(t)
}
func (c *WrappedConn) SetWriteDeadline(t time.Time) error {
return c.conn.SetWriteDeadline(t)
}
// take pointer to current limiter
func (c *WrappedConn) getCurrentLimiter() *rate.Limiter {
return (*rate.Limiter)(atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&c.limiter))))
}
// replace current limiter with new one
func (c *WrappedConn) setLimiter(limiter *rate.Limiter) {
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&c.limiter)), unsafe.Pointer(limiter))
}