-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconnection.go
447 lines (404 loc) · 11.8 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Copyright 2015 go-swagger maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swaggersocket
import (
"bufio"
"context"
"errors"
"io"
"net/http"
"sync"
"time"
"net"
"github.com/gorilla/websocket"
)
// Logger is the generic logger interface for in-app logging
type Logger interface {
Print(...interface{})
Printf(string, ...interface{})
Println(...interface{})
}
type concurrentWriter struct {
writer io.WriteCloser
wmutex *sync.Mutex
}
func (cw *concurrentWriter) Write(p []byte) (int, error) {
cw.wmutex.Lock()
defer cw.wmutex.Unlock()
return cw.writer.Write(p)
}
func (cw *concurrentWriter) Close() error {
cw.wmutex.Lock()
defer cw.wmutex.Unlock()
return cw.writer.Close()
}
func newConcurrentWriter(writer io.WriteCloser, mutex *sync.Mutex) io.WriteCloser {
return &concurrentWriter{
writer: writer,
wmutex: mutex,
}
}
// ConnectionType is the socket connection type, it can either be a serverside connection or a clientside connection
type ConnectionType int
const (
// ServerSide means this connection is owned by a websocket-server
ServerSide ConnectionType = iota
// ClientSide means this connection is owned by a websocket-client
ClientSide
)
const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second
// Time allowed to read the next pong message from the peer.
pongWait = 20 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
// Maximum message size allowed from peer.
maxMessageSize = 512
closeWriteWait = 10 * time.Second
readResponseTimeout = 10 * time.Second
)
// ConnectionOpts are the connection options
type connectionOpts struct {
Conn *websocket.Conn
ID string
KeepAlive bool
PingHandler func(string) error
PongHandler func(string) error
AppData []byte
Logger Logger
}
// SocketConnection is a wrapper around the websocket connection to handle http
type SocketConnection struct {
socketserver *WebsocketServer
socketclient *WebsocketClient
connType ConnectionType
conn *websocket.Conn
id string
heartBeat *heartbeat
// The close notificationChannel is only created if this connection serves an api
closeNotificationCh chan bool
closeHandlerCh chan bool
once sync.Once
hdlr http.Handler
log Logger
wmutex *sync.Mutex
}
// NewSocketConnection creates a new socket connection
func NewSocketConnection(opts connectionOpts) *SocketConnection {
// Default ping handler is to send back a pong control message with the same application data
// Default pong handler is to do nothing
// websocket protocol mentions that the pong message should reply back with the exact appData recieved from the ping message
if opts.PingHandler != nil {
opts.Conn.SetPingHandler(opts.PingHandler)
}
if opts.PongHandler != nil {
opts.Conn.SetPongHandler(opts.PongHandler)
}
sockconn := &SocketConnection{
conn: opts.Conn,
id: opts.ID,
closeNotificationCh: nil,
closeHandlerCh: nil,
log: opts.Logger,
wmutex: &sync.Mutex{},
}
if opts.KeepAlive {
// create a new heartbeat object
sockconn.heartBeat = newHeartBeat(sockconn, heartbeatPeriod, pingwriteWait, opts.AppData)
}
opts.Conn.SetCloseHandler(func(code int, text string) error {
sockconn.log.Println("Close message recieved from peer")
sockconn.log.Println("cleaning up connection resources")
sockconn.cleanupConnection()
return nil
})
return sockconn
}
func (c *SocketConnection) nextWriter(msgType int) (io.WriteCloser, error) {
c.wmutex.Lock()
defer c.wmutex.Unlock()
wrtr, err := c.conn.NextWriter(msgType)
if err != nil {
return nil, err
}
return newConcurrentWriter(wrtr, c.wmutex), nil
}
// remoteAddr returns the remote address of the connection
func (c *SocketConnection) remoteAddr() net.Addr {
return c.conn.UnderlyingConn().RemoteAddr()
}
func (c *SocketConnection) setType(t ConnectionType) {
c.connType = t
}
func (c *SocketConnection) setSocketServer(s *WebsocketServer) {
c.socketserver = s
}
func (c *SocketConnection) setSocketClient(s *WebsocketClient) {
c.socketclient = s
}
func (c *SocketConnection) handleFailure() {
if c.closeHandlerCh != nil {
c.closeHandlerCh <- true
}
if c.closeNotificationCh != nil {
// this will block until the apiserver loop reads the value
c.once.Do(func() { c.closeNotificationCh <- true })
}
// stop the heartbeat protocol.
if c.heartBeat != nil {
c.heartBeat.stop()
}
if c.connType == ServerSide {
// remove this connection from the server connectionMap
c.socketserver.unregisterConnection(c)
if c.socketserver.hasSubscriber.isSet() {
c.socketserver.eventStream <- ConnectionEvent{
EventType: ConnectionFailure,
ConnectionId: c.id,
}
}
}
c.conn.Close()
if c.connType == ClientSide {
// reconnect with exponential backoff
c.socketclient.Connect()
// reattach the handler
if c.hdlr != nil {
c.socketclient.conn.serve(context.Background(), c.hdlr)
}
}
}
// cleanup connection prepares for closing the connection. It acts as the close handler for the websocket connection
func (c *SocketConnection) cleanupConnection() {
// the sequence of operations is very imprtant
defer c.log.Printf("cleaned up connection")
if c.closeHandlerCh != nil {
c.closeHandlerCh <- true
}
if c.closeNotificationCh != nil {
// this will block until the apiserver loop reads the value
c.once.Do(func() { c.closeNotificationCh <- true })
}
// stop the heartbeat protocol.
if c.heartBeat != nil {
c.heartBeat.stop()
}
if c.connType == ServerSide {
// remove this connection from the server connectionMap
c.socketserver.unregisterConnection(c)
if c.socketserver.hasSubscriber.isSet() {
c.socketserver.eventStream <- ConnectionEvent{
EventType: ConnectionFailure,
ConnectionId: c.id,
}
}
}
c.conn.Close()
}
// Close provides a graceful termination of the connection
func (c *SocketConnection) Close() error {
if c.closeHandlerCh != nil {
c.closeHandlerCh <- true
}
if c.closeNotificationCh != nil {
// this will block until the apiserver loop exits
// this is very important to happen before the heartbeat stop
// causes race if the socket server wants to close the connection
c.once.Do(func() { c.closeNotificationCh <- true })
}
if c.heartBeat != nil {
c.heartBeat.stop()
}
if c.connType == ServerSide {
// remove this connection from the server connectionMap
c.socketserver.unregisterConnection(c)
if c.socketserver.hasSubscriber.isSet() {
c.socketserver.eventStream <- ConnectionEvent{
EventType: ConnectionClosed,
ConnectionId: c.id,
}
}
}
// some more stuff to do before closing the connection
// write a close control message to the peer so that the peer can cleanup the connection
c.conn.WriteControl(websocket.CloseMessage, nil, time.Now().Add(closeWriteWait))
// close the underlying network connection
if err := c.conn.Close(); err != nil {
c.log.Printf("closing websocket connection: %v", err)
return err
}
return nil
}
// ID returns the connection id
func (c *SocketConnection) ID() string {
return c.id
}
// RoundTrip implements the http RoundTripper interface. Must be safe for concurrent use by multiple goroutines
func (c *SocketConnection) RoundTrip(req *http.Request) (*http.Response, error) {
if err := c.WriteRequest(req); err != nil {
return nil, err
}
resp, err := c.ReadResponse()
if err != nil {
return nil, err
}
return resp, nil
}
// WriteRequest writes a request to the underlying connection
func (c *SocketConnection) WriteRequest(req *http.Request) error {
var err error
var w io.WriteCloser
if req.Header.Get("X-Correlation-Id") == "" {
return errors.New("X-Correlation-Id header must be present")
}
if w, err = c.nextWriter(websocket.TextMessage); err == nil {
defer w.Close()
if err = req.Write(w); err == nil {
return nil
}
}
c.log.Printf("error: %v", err)
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
c.handleFailure()
}
return err
}
// readRequest is how the connection read an http request and returns an http response
func (c *SocketConnection) readRequest(ctx context.Context) (*response, error) {
var reader io.Reader
var err error
var req *http.Request
if _, reader, err = c.conn.NextReader(); err == nil {
if req, err = http.ReadRequest(bufio.NewReader(reader)); err == nil {
ctx, cancelCtx := context.WithCancel(ctx)
req = req.WithContext(ctx)
w := &response{
conn: c,
cancelCtx: cancelCtx,
req: req,
reqBody: req.Body,
handlerHeader: make(http.Header),
contentLength: -1,
}
w.cw.res = w
bufw, _ := c.nextWriter(websocket.TextMessage)
w.cw.writer = bufw
w.w = newBufioWriterSize(&w.cw, bufferBeforeChunkingSize)
return w, nil
}
}
return nil, err
}
// ResponseReader is a specialized reader that reads streams on websockets
type ResponseReader struct {
c *SocketConnection
r io.Reader
}
func (rr *ResponseReader) Read(p []byte) (int, error) {
if rr.r == nil {
_, reader, err := rr.c.conn.NextReader()
if err != nil {
return 0, err
}
rr.r = reader
}
count, err := rr.r.Read(p)
// this is a fake EOF sent because of a flush at the server side
if count == 0 && err == io.EOF {
_, reader, err := rr.c.conn.NextReader()
if err != nil {
rr.c.log.Printf("reading error: %v", err)
return 0, err
}
rr.r = reader
// the correct count and EOF if any will be sent from here
return rr.r.Read(p)
}
return count, err
}
func newResponseReader(c *SocketConnection) io.Reader {
return &ResponseReader{
c: c,
}
}
// ReadResponse reads a response from the underlying connection
func (c *SocketConnection) ReadResponse() (*http.Response, error) {
// should add a timeout to readResponse
respCh := make(chan *http.Response)
defer close(respCh)
var err error
var resp *http.Response
go func() {
if resp, err = http.ReadResponse(bufio.NewReader(newResponseReader(c)), nil); err == nil {
respCh <- resp
return
}
c.log.Printf("read response: %v", err)
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
c.handleFailure()
}
}()
select {
case <-time.After(readResponseTimeout):
return nil, errors.New("time out")
case r := <-respCh:
return r, nil
}
}
func (c *SocketConnection) Serve(ctx context.Context, hdlr http.Handler) {
go c.serve(ctx, hdlr)
}
func (c *SocketConnection) serve(ctx context.Context, hdlr http.Handler) {
ctx, cancelCtx := context.WithCancel(ctx)
c.hdlr = hdlr
defer cancelCtx()
defer c.log.Println("exiting api-server loop")
c.closeNotificationCh = make(chan bool)
defer close(c.closeNotificationCh)
requestCh := make(chan *response)
defer close(requestCh)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for {
select {
case <-c.closeNotificationCh:
return
case resp := <-requestCh:
// you can listen to the closenotification channel inside the handler because the response writer implements the closeNotfiy interface. This is useful for long running handlers such as log --follow
go func() {
hdlr.ServeHTTP(resp, resp.req)
resp.cancelCtx()
resp.finishRequest()
}()
requestCh <- nil
}
}
}()
go func() {
defer wg.Done()
for {
resp, err := c.readRequest(ctx)
if err != nil {
c.log.Println(err)
return
}
requestCh <- resp
<-requestCh
}
}()
wg.Wait()
}