forked from IBM/fluent-forward-go
/
connection.go
381 lines (295 loc) · 8.93 KB
/
connection.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
/*
MIT License
Copyright contributors to the fluent-forward-go project
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package ws
import (
"errors"
"io"
"net"
"sync"
"time"
ext "github.com/aanujj/fluent-forward-go/fluent/client/ws/ext"
"github.com/gorilla/websocket"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
const (
DefaultCloseDeadline = 5 * time.Second
)
type Logger interface {
Println(v ...interface{})
Printf(format string, v ...interface{})
}
type noopLogger struct{}
func (l *noopLogger) Println(_ ...interface{}) {}
func (l *noopLogger) Printf(_ string, _ ...interface{}) {}
type ReadHandler func(conn Connection, messageType int, p []byte, err error) error
type ConnectionOptions struct {
CloseDeadline time.Duration
CloseHandler func(conn Connection, code int, text string) error
PingHandler func(conn Connection, appData string) error
PongHandler func(conn Connection, appData string) error
// TODO: should be a duration and added to `now` before every operation
ReadDeadline time.Time
// ReadHandler handles new messages received on the websocket. If an error
// is received the client MUST call `Close`. An error returned by ReadHandler
// will be retured by `Listen`.
ReadHandler ReadHandler
// TODO: should be a duration and added to `now` before every operation
WriteDeadline time.Time
// Logger is an optional debug log writer.
Logger Logger
}
type ConnState uint8
const (
ConnStateOpen ConnState = 1 << iota
ConnStateListening
ConnStateCloseReceived
ConnStateCloseSent
ConnStateClosed
ConnStateError
)
//counterfeiter:generate . Connection
type Connection interface {
ext.Conn
CloseWithMsg(closeCode int, msg string) error
Closed() bool
ConnState() ConnState
Listen() error
ReadHandler() ReadHandler
SetReadHandler(rh ReadHandler)
Write(data []byte) (int, error)
}
type connection struct {
ext.Conn
logger Logger
closeLock sync.Mutex
listenLock sync.Mutex
writeLock sync.Mutex
stateLock sync.RWMutex
readHandler ReadHandler
done chan struct{}
connState ConnState
closeDeadline time.Duration
}
func NewConnection(conn ext.Conn, opts ConnectionOptions) (Connection, error) {
wsc := &connection{
Conn: conn,
done: make(chan struct{}),
connState: ConnStateOpen,
logger: opts.Logger,
}
if wsc.logger == nil {
wsc.logger = &noopLogger{}
}
if opts.CloseHandler == nil {
opts.CloseHandler = wsc.handleClose
}
wsc.SetCloseHandler(func(code int, text string) error {
return opts.CloseHandler(wsc, code, text)
})
if opts.PingHandler != nil {
wsc.SetPingHandler(func(appData string) error {
return opts.PingHandler(wsc, appData)
})
}
if opts.PongHandler != nil {
wsc.SetPongHandler(func(appData string) error {
return opts.PongHandler(wsc, appData)
})
}
if opts.ReadHandler == nil {
opts.ReadHandler = func(c Connection, _ int, _ []byte, err error) error {
if err != nil {
wsc.logger.Println("Default ReadHandler error:", err)
_ = c.Close()
}
return err
}
}
wsc.SetReadHandler(opts.ReadHandler)
if opts.CloseDeadline == 0 {
opts.CloseDeadline = DefaultCloseDeadline
}
wsc.closeDeadline = opts.CloseDeadline
if err := wsc.SetReadDeadline(opts.ReadDeadline); err != nil {
return nil, err
}
if err := wsc.SetWriteDeadline(opts.WriteDeadline); err != nil {
return nil, err
}
return wsc, nil
}
func (wsc *connection) ConnState() ConnState {
wsc.stateLock.RLock()
defer wsc.stateLock.RUnlock()
return wsc.connState
}
func (wsc *connection) hasConnState(cs ConnState) bool {
wsc.stateLock.RLock()
defer wsc.stateLock.RUnlock()
return wsc.connState&cs != 0
}
func (wsc *connection) setConnState(cs ConnState) {
wsc.stateLock.Lock()
defer wsc.stateLock.Unlock()
wsc.connState |= cs
}
func (wsc *connection) unsetConnState(cs ConnState) {
wsc.stateLock.Lock()
defer wsc.stateLock.Unlock()
if wsc.connState&cs == 0 {
return
}
wsc.connState ^= cs
}
// CloseWithMsg sends a close message to the peer
func (wsc *connection) CloseWithMsg(closeCode int, msg string) error {
wsc.closeLock.Lock()
if wsc.Closed() {
wsc.closeLock.Unlock()
return errors.New("multiple close calls")
}
wsc.unsetConnState(ConnStateOpen)
wsc.closeLock.Unlock()
var err error
if !wsc.hasConnState(ConnStateError) {
wsc.logger.Printf("sending close message: code %d; msg '%s'", closeCode, msg)
// TODO: currently only the tests check this state to confirm handshake;
// need to refactor it out
wsc.setConnState(ConnStateCloseSent)
err = wsc.WriteMessage(
websocket.CloseMessage,
websocket.FormatCloseMessage(
closeCode, msg,
),
)
// if the close message was sent and the connection is listening for incoming
// messages, wait N seconds for a response.
if err == nil && wsc.hasConnState(ConnStateListening) {
wsc.logger.Println("awaiting peer response")
select {
case <-time.After(wsc.closeDeadline):
// sent a close, but never heard back, close anyway
err = errors.New("close deadline expired")
case <-wsc.done:
}
}
}
wsc.setConnState(ConnStateClosed)
wsc.logger.Println("closing the connection")
if cerr := wsc.Conn.Close(); cerr != nil {
err = cerr
}
return err
}
func (wsc *connection) Close() error {
return wsc.CloseWithMsg(websocket.CloseNormalClosure, "closing connection")
}
func (wsc *connection) Closed() bool {
return !wsc.hasConnState(ConnStateOpen)
}
func (wsc *connection) handleClose(_ Connection, code int, msg string) error {
wsc.logger.Printf("close received: code %d; msg '%s'", code, msg)
// TODO: currently only the tests check this state to confirm handshake;
// need to refactor it out
wsc.setConnState(ConnStateCloseReceived)
return nil
}
type connMsg struct {
mt int
message []byte
err error
}
func (wsc *connection) runReadLoop(nextMsg chan connMsg) {
defer func() {
wsc.logger.Println("exiting read loop")
close(nextMsg)
wsc.unsetConnState(ConnStateListening)
close(wsc.done)
}()
msg := connMsg{}
for {
msg.mt, msg.message, msg.err = wsc.Conn.ReadMessage()
if msg.err != nil {
if wsc.hasConnState(ConnStateClosed) && errors.Is(msg.err, net.ErrClosed) {
// healthy close
break
}
var err net.Error
if errors.As(msg.err, &err) || errors.Is(msg.err, net.ErrClosed) ||
websocket.IsCloseError(msg.err, websocket.CloseAbnormalClosure) {
// mark the connection with error state so Close doesn't attempt to
// send closing message to peer
wsc.setConnState(ConnStateError)
}
}
nextMsg <- msg
if msg.err != nil {
break
}
}
}
func (wsc *connection) Listen() error {
wsc.listenLock.Lock()
if wsc.hasConnState(ConnStateListening) {
wsc.listenLock.Unlock()
return errors.New("already listening on this connection")
}
wsc.logger.Println("listening")
wsc.setConnState(ConnStateListening)
wsc.listenLock.Unlock()
nextMsg := make(chan connMsg)
go wsc.runReadLoop(nextMsg)
var err error
for msg := range nextMsg {
if rerr := wsc.readHandler(wsc, msg.mt, msg.message, msg.err); rerr != nil {
if msg.err != nil {
wsc.logger.Println("handler returned error: ", msg.err.Error())
}
// set error only if it is something other than a normal close
if !websocket.IsCloseError(rerr, websocket.CloseNormalClosure) {
err = rerr
}
}
}
return err
}
func (wsc *connection) NextReader() (messageType int, r io.Reader, err error) {
panic("use ReadHandler instead")
}
func (wsc *connection) ReadMessage() (messageType int, p []byte, err error) {
panic("use ReadHandler instead")
}
func (wsc *connection) SetReadHandler(rh ReadHandler) {
wsc.readHandler = rh
}
func (wsc *connection) ReadHandler() ReadHandler {
return wsc.readHandler
}
func (wsc *connection) WriteMessage(messageType int, data []byte) error {
wsc.writeLock.Lock()
defer wsc.writeLock.Unlock()
return wsc.Conn.WriteMessage(messageType, data)
}
func (wsc *connection) Write(data []byte) (int, error) {
if err := wsc.WriteMessage(websocket.BinaryMessage, data); err != nil {
return 0, err
}
return len(data), nil
}