-
Notifications
You must be signed in to change notification settings - Fork 7
/
backend.go
267 lines (234 loc) · 7.02 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
package main
import (
"errors"
log "github.com/sirupsen/logrus"
"net"
"sync/atomic"
"time"
)
var notConnectedError error = errors.New("not connected")
var requestTimeoutError error = errors.New("Request is timeout")
var circuitBreakError error = errors.New("circuit break the backend")
type requestWithResponseCallback struct {
request *Message
requestTimeoutTime time.Time
responseCallback ResponseCallback
}
func newRequestWithResponseCallback(request *Message,
requestTimeoutTime time.Time,
responseCallback ResponseCallback) *requestWithResponseCallback {
return &requestWithResponseCallback{request: request,
requestTimeoutTime: requestTimeoutTime,
responseCallback: responseCallback}
}
type Backend interface {
Send(request *Message, requestTimeoutTime time.Time, callback ResponseCallback)
GetAddr() string
IsConnected() bool
Stop()
}
type CircuitbreakBackend struct {
backend Backend
successiveFailureTimes int32
failedTimes int32
pauseDuration time.Duration
resumeTime time.Time
}
func NewCircuitbreakBackend(backend Backend,
successiveFailureTimes int32,
pauseDuration time.Duration) *CircuitbreakBackend {
return &CircuitbreakBackend{backend: backend,
successiveFailureTimes: successiveFailureTimes,
failedTimes: 0,
pauseDuration: pauseDuration,
resumeTime: time.Now()}
}
func (c *CircuitbreakBackend) Send(request *Message, requestTimeoutTime time.Time, callback ResponseCallback) {
if c.resumeTime.After(time.Now()) {
callback(nil, circuitBreakError)
return
}
c.backend.Send(request, requestTimeoutTime, func(response *Message, err error) {
if err == nil {
atomic.StoreInt32(&c.failedTimes, 0)
callback(response, err)
} else if atomic.AddInt32(&c.failedTimes, 1) >= c.successiveFailureTimes {
c.resumeTime = time.Now().Add(c.pauseDuration)
callback(nil, err)
} else {
callback(nil, err)
}
})
}
func (c *CircuitbreakBackend) GetAddr() string {
return c.backend.GetAddr()
}
func (c *CircuitbreakBackend) IsConnected() bool {
return c.backend.IsConnected()
}
func (c *CircuitbreakBackend) Stop() {
c.backend.Stop()
}
type TcpBackend struct {
addr string
readiness Readiness
stop int32
conn net.Conn
connected int32
requests chan *requestWithResponseCallback
responseCallbacks *ResponseCallbackMgr
}
func NewBackend(backendInfo *BackendInfo) Backend {
tcpBackend := NewTcpBackend(backendInfo)
if backendInfo.CircuitBreaker != nil {
return NewCircuitbreakBackend(tcpBackend,
int32(backendInfo.CircuitBreaker.SuccessiveFailures),
convertDuration(backendInfo.CircuitBreaker.PauseTime, time.Duration(5)*time.Second))
} else {
return tcpBackend
}
}
// NewTcpBackend create a thrift backend
func NewTcpBackend(backendInfo *BackendInfo) *TcpBackend {
backend := &TcpBackend{addr: backendInfo.Addr,
readiness: createReadiness(backendInfo.Addr, backendInfo.Readiness),
stop: 0,
conn: NewErrorConn(),
connected: 0,
requests: make(chan *requestWithResponseCallback, 1000),
responseCallbacks: NewResponseCallbackMgr()}
go backend.startAfterReady()
go backend.cleanTimeoutResponse()
return backend
}
func (b *TcpBackend) startAfterReady() {
for !b.IsStopped() {
if b.readiness.IsReady() {
log.WithFields(log.Fields{"address": b.addr}).Info("Server is ready")
go b.start()
go b.startWriteMessage()
break
}
time.Sleep(time.Duration(2) * time.Second)
}
}
func (b *TcpBackend) cleanTimeoutResponse() {
cleanInterval := time.Duration(10) * time.Millisecond
for !b.IsStopped() {
b.responseCallbacks.RemoveTimeout(func(callback ResponseCallback) {
callback(nil, requestTimeoutError)
})
time.Sleep(cleanInterval)
}
}
func (b *TcpBackend) GetAddr() string {
return b.addr
}
func (b *TcpBackend) start() {
for !b.IsStopped() {
log.WithFields(log.Fields{"address": b.addr}).Info("try to connect to backend server")
conn, err := net.Dial("tcp", b.addr)
if err == nil {
b.conn = conn
b.setConnected(true)
go b.startReadMessage()
log.WithFields(log.Fields{"address": b.addr}).Info("Connect to backend server successfully")
break
} else {
log.WithFields(log.Fields{"address": b.addr}).Error("Fail to connect backend server")
}
time.Sleep(5 * time.Second)
}
}
func (b *TcpBackend) startReadMessage() {
buffer := make([]byte, 4096)
respBuffer := NewMessageBuffer()
for {
n, err := b.conn.Read(buffer)
if err != nil {
b.setConnected(false)
log.WithFields(log.Fields{"address": b.addr}).Error("Fail to read response from backend server")
break
}
respBuffer.Add(buffer[0:n])
b.processResponseBuffer(respBuffer)
}
if !b.IsStopped() {
go b.startAfterReady()
}
}
func (b *TcpBackend) IsConnected() bool {
return atomic.LoadInt32(&b.connected) != 0
}
func (b *TcpBackend) setConnected(connected bool) {
if connected {
atomic.StoreInt32(&b.connected, 1)
} else {
atomic.StoreInt32(&b.connected, 0)
}
}
// processResponseBuffer process the response from backend server
func (b *TcpBackend) processResponseBuffer(respBuffer *MessageBuffer) {
for {
response, err := respBuffer.ExtractMessage()
if err != nil {
break
}
seqId, err := response.GetSeqId()
if err == nil {
respCb, ok := b.getResponseCallback(seqId)
if ok {
respCb(response, nil)
} else {
log.Error("Fail to find response callback by seqId")
}
} else {
log.Error("Fail to get the seqId from response")
}
}
}
func (b *TcpBackend) getResponseCallback(seqId int) (ResponseCallback, bool) {
return b.responseCallbacks.Remove(seqId)
}
// Stop stop the backend
func (b *TcpBackend) Stop() {
if atomic.CompareAndSwapInt32(&b.stop, 0, 1) {
log.WithFields(log.Fields{"address": b.addr}).Info("Stop backend")
defer b.conn.Close()
} else {
log.WithFields(log.Fields{"address": b.addr}).Info("TcpBackend is already stopped")
}
}
func (b *TcpBackend) IsStopped() bool {
return atomic.LoadInt32(&b.stop) != 0
}
func (b *TcpBackend) Send(request *Message, requestTimeoutTime time.Time, callback ResponseCallback) {
if !b.IsConnected() {
callback(nil, notConnectedError)
} else {
b.requests <- newRequestWithResponseCallback(request, requestTimeoutTime, callback)
}
}
func (b *TcpBackend) startWriteMessage() {
for !b.IsStopped() {
select {
case requestWithResponseCb, ok := <-b.requests:
if !ok {
log.WithFields(log.Fields{"address": b.addr}).Error("Fail to send request to backend server")
return
}
seqId, _ := requestWithResponseCb.request.GetSeqId()
b.responseCallbacks.Add(seqId,
requestWithResponseCb.responseCallback,
requestWithResponseCb.requestTimeoutTime)
err := requestWithResponseCb.request.Write(b.conn)
if err == nil {
log.WithFields(log.Fields{"address": b.addr}).Info("Succeed to send request to backend server")
} else {
log.WithFields(log.Fields{"address": b.addr}).Error("Fail to send the request to backend server")
requestWithResponseCb.responseCallback(nil, err)
return
}
}
}
}