forked from facebookarchive/dvara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
404 lines (336 loc) · 8.72 KB
/
proxy.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package dvara
import (
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
"time"
"sync/atomic"
)
const headerLen = 16
var (
errZeroMaxPerClientConnections = errors.New("dvara: MaxPerClientConnections cannot be 0")
errNormalClose = errors.New("dvara: normal close")
errClientReadTimeout = errors.New("dvara: client read timeout")
errServerGoneAway = errors.New("dvara: mongo server gone away")
errExchangerAcquire = errors.New("dvara: exchanger acquire failed")
timeInPast = time.Now()
maxRequestId = int32(1024 * 1024)
)
// ProxyQuery sends stuff from clients to mongo servers.
type Proxy struct {
Log Logger
Manager *ProxyManager
ClientListener net.Listener // Listener for incoming client connections
ProxyAddr string // Address for incoming client connections
MongoAddr string // Address for destination Mongo server
Username string
Password string
wg sync.WaitGroup
closed chan struct{}
maxPerClientConnections *maxPerClientConnections
maxServerConnections int32
maxResponseWait int32
acquire chan chan *exchanger
stopped chan *exchanger
exchangers []*exchanger
running []*exchanger
waiterCnt int32
runningCnt int32
}
// String representation for debugging.
func (p *Proxy) String() string {
return fmt.Sprintf("proxy %s => mongo %s", p.ProxyAddr, p.MongoAddr)
}
// Start the proxy.
func (p *Proxy) Start() error {
if p.Manager.MaxPerClientConnections == 0 {
return errZeroMaxPerClientConnections
}
p.closed = make(chan struct{})
p.maxPerClientConnections = newMaxPerClientConnections(p.Manager.MaxPerClientConnections)
p.maxServerConnections = p.Manager.MaxServerConnections
p.maxResponseWait = p.Manager.MaxResponseWait
p.acquire = make(chan chan *exchanger)
p.stopped = make(chan *exchanger)
p.exchangers = make([]*exchanger, 16)
go p.manage()
go p.clientAcceptLoop()
return nil
}
// Stop the proxy.
func (p *Proxy) Stop() error {
return p.stop(false)
}
func (p *Proxy) stop(hard bool) error {
if err := p.ClientListener.Close(); err != nil {
return err
}
close(p.closed)
if !hard {
p.wg.Wait()
}
return nil
}
// proxyMessage proxies a message, possibly it's response, and possibly a
// follow up call.
func (p *Proxy) proxyMessage(m *requestMessage, client net.Conn) error {
h := m.getHeader()
p.Log.Debugf("proxying message %s from %s for %s", h, client.RemoteAddr(), p)
r := make(chan *exchanger)
p.acquire <- r
exchanger := <-r
if exchanger == nil {
return errExchangerAcquire
}
if !h.OpCode.HasResponse() {
exchanger.requestChan <- &request{message: m, respChan: nil}
return nil
}
res := make(chan *response)
exchanger.requestChan <- &request{message: m, respChan: res}
response := <-res
if response.error != nil {
return response.error
}
rewriter, err := p.getRewriter(m)
if err != nil {
return err
}
if rewriter != nil {
if err = rewriter.Rewrite(response.message); err != nil {
return err
}
}
deadline := time.Now().Add(p.Manager.MessageTimeout)
client.SetDeadline(deadline)
_, err = client.Write(response.message.bytes)
if err != nil {
return err
}
return nil
}
// add or reduce server connections by current waiter count
func (p *Proxy) balance(runningCnt int32, waiterCnt int32) int32 {
needServer := (waiterCnt * 2)/p.maxResponseWait + 1
if needServer > p.maxServerConnections {
needServer = p.maxServerConnections
}
if needServer > runningCnt {
p.Log.Infof("%d resp waiting, balance exchanger %d => %d", waiterCnt, runningCnt, needServer)
for i, exc := range p.exchangers {
if exc == nil {
exc = NewExchanger(p, i)
p.exchangers[i] = exc
}
if exc.status == Idle {
if err := exc.run(); err != nil {
p.Log.Error(err)
continue
}
p.running = append(p.running, exc)
runningCnt += 1
if needServer <= runningCnt {
break
}
}
}
atomic.StoreInt32(&p.runningCnt, runningCnt)
}
if needServer < runningCnt {
p.Log.Infof("%d resp waiting, balance exchanger %d => %d", waiterCnt, runningCnt, needServer)
var k int
for j, e := range p.running {
e.closing()
runningCnt -= 1
k = j
if needServer >= runningCnt {
break
}
}
p.running = p.running[k+1:]
atomic.StoreInt32(&p.runningCnt, runningCnt)
}
return runningCnt
}
func (p *Proxy) removeRunning(exc *exchanger) {
for i, item := range p.running {
if item.index == exc.index {
p.running = append(p.running[:i], p.running[i+1:]...)
break
}
}
atomic.StoreInt32(&p.runningCnt, int32(len(p.running)))
}
func (p *Proxy) manage() {
var index int32
maxWaitCnt := int32(p.maxResponseWait * p.maxServerConnections)
for {
select {
case exchangerChan := <-p.acquire:
runningCnt := atomic.LoadInt32(&p.runningCnt)
waiterCnt := atomic.LoadInt32(&p.waiterCnt)
if waiterCnt >= maxWaitCnt {
exchangerChan <- nil
continue
}
runningCnt = p.balance(runningCnt, waiterCnt)
index = index % runningCnt
exchangerChan <- p.running[index]
index ++
case exchanger := <- p.stopped:
s := exchanger.status
exchanger.stop()
if s == Running {
p.removeRunning(exchanger)
}
}
}
}
// clientAcceptLoop accepts new clients and creates a clientServeLoop for each
// new client that connects to the proxy.
func (p *Proxy) clientAcceptLoop() {
for {
p.wg.Add(1)
c, err := p.ClientListener.Accept()
if err != nil {
p.wg.Done()
if strings.Contains(err.Error(), "use of closed network connection") {
break
}
p.Log.Error(err)
continue
}
go p.clientServeLoop(c)
}
}
// clientServeLoop loops on a single client connected to the proxy and
// dispatches its requests.
func (p *Proxy) clientServeLoop(c net.Conn) {
remoteIP := c.RemoteAddr().(*net.TCPAddr).IP.String()
// enforce per-client max connection limit
if p.maxPerClientConnections.inc(remoteIP) {
c.Close()
p.Log.Errorf("rejecting client connection due to max connections limit: %s", remoteIP)
return
}
// turn on TCP keep-alive and set it to the recommended period of 2 minutes
// http://docs.mongodb.org/manual/faq/diagnostics/#faq-keepalive
if conn, ok := c.(*net.TCPConn); ok {
conn.SetKeepAlivePeriod(2 * time.Minute)
conn.SetKeepAlive(true)
}
p.Log.Infof("client %s connected to %s", c.RemoteAddr(), p)
defer func() {
p.Log.Infof("client %s disconnected from %s", c.RemoteAddr(), p)
p.wg.Done()
if err := c.Close(); err != nil {
p.Log.Error(err)
}
p.maxPerClientConnections.dec(remoteIP)
}()
for {
m, err := p.idleClientReadMessage(c)
if err != nil {
if err != errNormalClose {
p.Log.Error(err)
}
return
}
err = p.proxyMessage(m, c)
if err != nil {
p.Log.Error(err)
return
}
}
}
func (p *Proxy) idleClientReadMessage(c net.Conn) (*requestMessage, error) {
msg, err := p.clientReadMessage(c, p.Manager.ClientIdleTimeout)
if err != nil {
return nil, err
}
return msg, err
}
func (p *Proxy) clientReadMessage(c net.Conn, timeout time.Duration) (*requestMessage, error) {
type requestError struct {
message *requestMessage
error error
}
resChan := make(chan requestError)
c.SetReadDeadline(time.Now().Add(timeout))
go func() {
message, err := readRequestMessage(c)
resChan <- requestError{message: message, error: err}
}()
closed := false
var response requestError
select {
case response = <-resChan:
// all good
case <-p.closed:
closed = true
c.SetReadDeadline(timeInPast)
response = <-resChan
}
// Successfully read a header.
if response.error == nil {
return response.message, nil
}
// Client side disconnected.
if response.error == io.EOF {
return nil, errNormalClose
}
// We hit our ReadDeadline.
if ne, ok := response.error.(net.Error); ok && ne.Timeout() {
if closed {
return nil, errNormalClose
}
return nil, errClientReadTimeout
}
// Some other unknown error.
p.Log.Error(response.error)
return nil, response.error
}
type maxPerClientConnections struct {
max uint
counts map[string]uint
mutex sync.Mutex
}
func newMaxPerClientConnections(max uint) *maxPerClientConnections {
return &maxPerClientConnections{
max: max,
counts: make(map[string]uint),
}
}
func (m *maxPerClientConnections) inc(remoteIP string) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
current := m.counts[remoteIP]
if current >= m.max {
return true
}
m.counts[remoteIP] = current + 1
return false
}
func (m *maxPerClientConnections) dec(remoteIP string) {
m.mutex.Lock()
defer m.mutex.Unlock()
current := m.counts[remoteIP]
// delete rather than having entries with 0 connections
if current == 1 {
delete(m.counts, remoteIP)
} else {
m.counts[remoteIP] = current - 1
}
}
func (m *maxPerClientConnections) count() int {
m.mutex.Lock()
defer m.mutex.Unlock()
res := 0
for _, n := range m.counts {
res += int(n)
}
return res
}