/
outc.go
282 lines (250 loc) · 7.11 KB
/
outc.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
package out
import (
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/apundir/wsbalancer/common"
"github.com/gorilla/websocket"
)
// messageCache is used to cache messages which couldn't be written
// to backend server due to IO error. These are sent first as
// and when the connection is established again to backend.
type messageCache struct {
cache []*common.WsMessage
}
func newMessageCache(oldCache *messageCache) *messageCache {
nc := &messageCache{}
if oldCache == nil {
nc.cache = make([]*common.WsMessage, 0)
} else {
nc.cache = oldCache.clear()
}
return nc
}
func (um *messageCache) add(msg *common.WsMessage) {
um.cache = append(um.cache, msg)
}
func (um *messageCache) clear() []*common.WsMessage {
ret := um.cache
um.cache = make([]*common.WsMessage, 0)
return ret
}
// outc - outgoing client object
type outc struct {
// websocket connection object
con *websocket.Conn
// For sending messages to F/E
send chan<- *common.WsMessage
// For receiving messages from F/E
receive <-chan *common.WsMessage
// how often a ping be sent to backend server
pingWait time.Duration
// for tracking this connection is being cleaned up
shuttingDown uint32
// unique id for this connection
id string
// Identifier for upstream server
backendID string
// channel for signaling closure of websocket
done chan struct{}
// channel for signaling to backend that this connection is done
bkDone chan<- struct{}
// headers that shall be passed form backend to frontend AS-IS
passHeaders http.Header
// for safeguarding concurrent closure
closeOnce *sync.Once
// for storing unsent messages in event of IO failure
cache *messageCache
// for safeguarding concurrent notification to frontned
notifyFEOnce *sync.Once
}
func newOutc(c *websocket.Conn, rcv <-chan *common.WsMessage,
snd chan<- *common.WsMessage, pingTime time.Duration,
bkID string, conDone chan<- struct{},
passHeaders http.Header, oldCon *outc) *outc {
oc := &outc{
con: c,
receive: rcv,
send: snd,
pingWait: pingTime,
backendID: bkID,
bkDone: conDone,
passHeaders: passHeaders,
notifyFEOnce: &sync.Once{},
closeOnce: &sync.Once{},
shuttingDown: 0,
done: make(chan struct{}, 4),
}
var oldCache *messageCache = nil
if oldCon != nil {
oldCache = oldCon.cache
}
oc.cache = newMessageCache(oldCache)
return oc
}
// Start - start read and write routines
func (oc *outc) Start() {
go oc.startReading()
go oc.startWriting()
}
// Close - closes this connection
func (oc *outc) Close() error {
oc.closeOnce.Do(func() {
olog.Debugf("[%v] closing BE WS connection", oc.id)
atomic.StoreUint32(&oc.shuttingDown, 1)
oc.con.WriteControl(websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
time.Now().Add(time.Second))
oc.con.Close()
oc.done <- struct{}{}
olog.Debugf("[%v] BE connection closed", oc.id)
})
return nil
}
// Subprotocol returns the negotiated protocol for the connection.
func (oc *outc) Subprotocol() string {
return oc.con.Subprotocol()
}
// AdditionalHeaders headers from backend that shall be passed
// onto frontend AS-IS, may include Cookie etc. Subprotocol
// should NOT be part of these headers.
func (oc *outc) AdditionalHeaders() http.Header {
return oc.passHeaders
}
// BackendID returns the unique id for the backend server
func (oc *outc) BackendID() string {
return oc.backendID
}
// GetID - returns unique id for this connection.
func (oc *outc) GetID() string {
return oc.id
}
// notifyBackend - notifies backend about closure of this connection and
// handle any panic if raised.
func (oc *outc) notifyBackend() {
if oc.bkDone == nil {
return
}
defer func() {
if r := recover(); r != nil {
olog.Warnf("[%v] Recovered from panic during notifyBackend", oc.id, r)
}
oc.bkDone = nil
}()
// this may panic in case the channel is already closed
oc.bkDone <- struct{}{}
}
// startReading - reads from upstream connection and write it to proxy's channel
func (oc *outc) startReading() {
defer func() {
oc.Close()
oc.notifyBackend()
olog.Debugf("[%v] backend reader routine done", oc.id)
}()
for {
mt, message, err := oc.con.ReadMessage()
if atomic.LoadUint32(&oc.shuttingDown) == 1 {
return // in cleanup phase, no further action needed
}
if err != nil {
oc.notifyFrontend(err, "reading")
return
}
msg := &common.WsMessage{
MessageType: mt,
Data: message,
}
if msg.IsControl() {
// it's neither TextMessage nor BinaryMessage
continue
}
oc.send <- msg
olog.Tracef("[%v] client<-proxy: %s", oc.id, msg)
}
}
func (oc *outc) notifyFrontend(err error, direction string) {
oc.notifyFEOnce.Do(func() {
oc._notifyFrontend(err, direction)
})
}
func (oc *outc) _notifyFrontend(err error, direction string) {
pxMsgType := common.RemoteConnectionNormalClosure
// identify if this is non websocket error, e.g. network disconnect
_, wceOk := err.(*websocket.CloseError)
if !wceOk || websocket.IsCloseError(err, websocket.CloseAbnormalClosure, websocket.CloseGoingAway,
websocket.CloseInternalServerErr, websocket.CloseNoStatusReceived,
websocket.CloseTryAgainLater) {
// these close codes demands a new upstream connections shall be established
pxMsgType = common.RemoteConnectionAbnormalClosure
olog.Infof("[%v] backend abnormally closed while %v, error: %v", oc.id, direction, err)
} else {
olog.Infof("[%v] backend normally closed while %v, error: %v", oc.id, direction, err)
}
msg := &common.WsMessage{
MessageType: pxMsgType,
Data: []byte(err.Error()),
}
oc.send <- msg
olog.Tracef("[%v] client<-proxy: %s", oc.id, msg)
}
// startWriting - start the writing routine which read from FE channel
// and write it to BE server connection.
func (oc *outc) startWriting() {
ticker := time.NewTicker(oc.pingWait)
defer func() {
ticker.Stop()
oc.Close()
olog.Debugf("[%v] backend writer routine done", oc.id)
}()
// cleanup existing cache by sending all messages form that first
if !oc.clearCache() {
return
}
for {
select {
case msg := <-oc.receive:
if msg == nil || msg.MessageType == common.RemoteConnectionNormalClosure {
// well, client is gone, cleanup upstream connection
olog.Debugf("[%v] received control message: %v", oc.id, msg)
return
}
if wrr := oc.con.WriteMessage(msg.MessageType, msg.Data); wrr != nil {
oc.notifyFrontend(wrr, "writing")
oc.cache.add(msg)
return
}
olog.Tracef("[%v] client->proxy: %s", oc.id, msg)
case <-ticker.C:
err := oc.con.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
if err != nil {
oc.notifyFrontend(err, "writing ping")
return
}
case <-oc.done:
return
}
}
}
func (oc *outc) clearCache() bool {
cached := oc.cache.clear()
olog.Debugf("[%v] clearing %v cached messages", oc.id, len(cached))
cacheClearError := false
for _, am := range cached {
if cacheClearError {
oc.cache.add(am)
continue
}
wrr := oc.con.WriteMessage(am.MessageType, am.Data)
if wrr == nil {
continue
}
cacheClearError = true
oc.cache.add(am)
oc.notifyFrontend(wrr, "writing cache")
}
if !cacheClearError {
return true
}
return false
}