-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbackend.go
335 lines (292 loc) · 7.44 KB
/
backend.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
package main
import (
"io"
"net"
"sync"
"sync/atomic"
"time"
"github.com/litl/shuttle/client"
"github.com/litl/shuttle/log"
)
type Backend struct {
sync.Mutex
Name string
Addr string
CheckAddr string
up bool
Weight int
Sent int64
Rcvd int64
Errors int64
Conns int64
Active int64
HTTPActive int64
Network string
// these are loaded from the service, so a backend doesn't need to access
// the service struct at all.
dialTimeout time.Duration
rwTimeout time.Duration
checkInterval time.Duration
rise int
riseCount int
checkOK int
fall int
fallCount int
checkFail int
startCheck sync.Once
// stop the health-check loop
stopCheck chan interface{}
// so we only need to ResolveUDPAddr once
udpAddr *net.UDPAddr
}
// The json stats we return for the backend
type BackendStat struct {
Name string `json:"name"`
Addr string `json:"address"`
CheckAddr string `json:"check_address"`
Up bool `json:"up"`
Weight int `json:"weight"`
Sent int64 `json:"sent"`
Rcvd int64 `json:"received"`
Errors int64 `json:"errors"`
Conns int64 `json:"connections"`
Active int64 `json:"active"`
HTTPActive int64 `json:"http_active"`
CheckOK int `json:"check_success"`
CheckFail int `json:"check_fail"`
}
func NewBackend(cfg client.BackendConfig) *Backend {
b := &Backend{
Name: cfg.Name,
Addr: cfg.Addr,
CheckAddr: cfg.CheckAddr,
Weight: cfg.Weight,
Network: cfg.Network,
stopCheck: make(chan interface{}),
}
// don't want a weight of 0
if b.Weight == 0 {
b.Weight = 1
}
if b.Network == "" {
b.Network = "tcp"
}
switch b.Network {
case "udp", "udp4", "udp6":
var err error
b.udpAddr, err = net.ResolveUDPAddr(b.Network, b.Addr)
if err != nil {
log.Errorf("ERROR: %s", err.Error())
b.up = false
}
}
return b
}
// Copy the backend state into a BackendStat struct.
func (b *Backend) Stats() BackendStat {
b.Lock()
defer b.Unlock()
stats := BackendStat{
Name: b.Name,
Addr: b.Addr,
CheckAddr: b.CheckAddr,
Up: b.up,
Weight: b.Weight,
Sent: atomic.LoadInt64(&b.Sent),
Rcvd: atomic.LoadInt64(&b.Rcvd),
Errors: atomic.LoadInt64(&b.Errors),
Conns: atomic.LoadInt64(&b.Conns),
Active: atomic.LoadInt64(&b.Active),
HTTPActive: atomic.LoadInt64(&b.HTTPActive),
CheckOK: b.checkOK,
CheckFail: b.checkFail,
}
return stats
}
func (b *Backend) Up() bool {
b.Lock()
up := b.up
b.Unlock()
return up
}
// Return the struct for marshaling into a json config
func (b *Backend) Config() client.BackendConfig {
b.Lock()
defer b.Unlock()
cfg := client.BackendConfig{
Name: b.Name,
Addr: b.Addr,
CheckAddr: b.CheckAddr,
Weight: b.Weight,
}
return cfg
}
// Backends and Servers Stringify themselves directly into their config format.
func (b *Backend) String() string {
return string(marshal(b.Config()))
}
func (b *Backend) Start() {
go b.startCheck.Do(b.healthCheck)
}
func (b *Backend) Stop() {
close(b.stopCheck)
}
func (b *Backend) check() {
if b.CheckAddr == "" {
return
}
up := true
if c, e := net.DialTimeout("tcp", b.CheckAddr, b.dialTimeout); e == nil {
c.(*net.TCPConn).SetLinger(0)
c.Close()
} else {
log.Debug("Check error:", e)
up = false
}
b.Lock()
defer b.Unlock()
if up {
log.Debugf("Check OK for %s/%s", b.Name, b.CheckAddr)
b.fallCount = 0
b.riseCount++
b.checkOK++
if b.riseCount >= b.rise {
if !b.up {
log.Debugf("Marking backend %s Up", b.Name)
}
b.up = true
}
} else {
log.Debugf("Check failed for %s/%s", b.Name, b.CheckAddr)
b.riseCount = 0
b.fallCount++
b.checkFail++
if b.fallCount >= b.fall {
if b.up {
log.Debugf("Marking backend %s Down", b.Name)
}
b.up = false
}
}
}
// Periodically check the status of this backend
func (b *Backend) healthCheck() {
t := time.NewTicker(b.checkInterval)
for {
select {
case <-b.stopCheck:
log.Debug("Stopping backend", b.Name)
t.Stop()
return
case <-t.C:
b.check()
}
}
}
// use to identify embedded TCPConns
type closeReader interface {
CloseRead() error
}
func (b *Backend) Proxy(srvConn, cliConn net.Conn) {
log.Debugf("Initiating proxy: %s/%s-%s/%s",
cliConn.RemoteAddr(),
cliConn.LocalAddr(),
srvConn.LocalAddr(),
srvConn.RemoteAddr(),
)
// Backend is a pointer receiver so we can get the address of the fields,
// but all updates will be done atomically.
bConn := &shuttleConn{
TCPConn: srvConn.(*net.TCPConn),
rwTimeout: b.rwTimeout,
read: &b.Rcvd,
written: &b.Sent,
}
// TODO: No way to force shutdown. Do we need it, or should we always just
// let a connection run out?
atomic.AddInt64(&b.Conns, 1)
atomic.AddInt64(&b.Active, 1)
defer atomic.AddInt64(&b.Active, -1)
// channels to wait on close event
backendClosed := make(chan bool, 1)
clientClosed := make(chan bool, 1)
go broker(bConn, cliConn, clientClosed, &b.Sent, &b.Errors)
go broker(cliConn, bConn, backendClosed, &b.Rcvd, &b.Errors)
// wait for one half of the proxy to exit, then trigger a shutdown of the
// other half by calling CloseRead(). This will break the read loop in the
// broker and fully close the connection.
var waitFor chan bool
select {
case <-clientClosed:
log.Debugf("Client %s/%s closed connection", cliConn.RemoteAddr(), cliConn.LocalAddr())
// the client closed first, so any more packets here are invalid, and
// we can SetLinger(0) to recycle the port faster.
bConn.TCPConn.SetLinger(0)
bConn.CloseRead()
waitFor = backendClosed
case <-backendClosed:
log.Debugf("Server %s/%s closed connection", srvConn.RemoteAddr(), srvConn.LocalAddr())
cliConn.(closeReader).CloseRead()
waitFor = clientClosed
}
// wait for the other connection to close
<-waitFor
}
// This does the actual data transfer.
// The broker only closes the Read side.
func broker(dst, src net.Conn, srcClosed chan bool, written, errors *int64) {
_, err := io.Copy(dst, src)
if err != nil {
atomic.AddInt64(errors, 1)
log.Printf("Copy error: %s", err)
}
if err := src.Close(); err != nil {
atomic.AddInt64(errors, 1)
log.Printf("Close error: %s", err)
}
srcClosed <- true
}
// A net.Conn that sets a deadline for every read or write operation.
// This will allow the server to close connections that are broken at the
// network level.
type shuttleConn struct {
*net.TCPConn
rwTimeout time.Duration
// count bytes read and written through this connection
written *int64
read *int64
// decrement when closed
connected *int64
}
func (c *shuttleConn) Read(b []byte) (int, error) {
if c.rwTimeout > 0 {
err := c.TCPConn.SetReadDeadline(time.Now().Add(c.rwTimeout))
if err != nil {
return 0, err
}
}
n, err := c.TCPConn.Read(b)
atomic.AddInt64(c.read, int64(n))
return n, err
}
func (c *shuttleConn) Write(b []byte) (int, error) {
if c.rwTimeout > 0 {
err := c.TCPConn.SetWriteDeadline(time.Now().Add(c.rwTimeout))
if err != nil {
return 0, err
}
}
n, err := c.TCPConn.Write(b)
atomic.AddInt64(c.written, int64(n))
return n, err
}
func (c *shuttleConn) Close() error {
if c.connected != nil {
atomic.AddInt64(c.connected, -1)
}
return c.TCPConn.Close()
}
// Empty function to override the ReadFrom in *net.TCPConn
// io.Copy will attempt to use ReadFrom when it can, but there's no bennefit
// for a TCPConn->TCPConn, and it prevents us from collecting Read/Write stats.
func (c *shuttleConn) ReadFrom() {}