-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathwebsocket.go
276 lines (244 loc) · 8.18 KB
/
websocket.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
// Copyright © 2022 Meroxa, Inc.
//
// 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 grpcutil
import (
"bufio"
"context"
"io"
"net/http"
"strings"
"time"
"github.com/conduitio/conduit-commons/cchan"
"github.com/conduitio/conduit/pkg/foundation/log"
"github.com/gorilla/websocket"
)
type inMemoryResponseWriter struct {
io.Writer
header http.Header
}
func newInMemoryResponseWriter(writer io.Writer) *inMemoryResponseWriter {
return &inMemoryResponseWriter{
Writer: writer,
header: http.Header{},
}
}
func (w *inMemoryResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (w *inMemoryResponseWriter) Header() http.Header {
return w.header
}
func (w *inMemoryResponseWriter) WriteHeader(int) {
// we don't have a use for the code
}
func (w *inMemoryResponseWriter) Flush() {}
var (
defaultWriteWait = 10 * time.Second
defaultPongWait = 60 * time.Second
)
// webSocketProxy is a proxy around a http.Handler which
// redirects the response data from the http.Handler
// to a WebSocket connection.
type webSocketProxy struct {
// done is closed to indicate that the proxy should stop working
done <-chan struct{}
// handler is the underlying handler actually handling requests
handler http.Handler
logger log.CtxLogger
upgrader websocket.Upgrader
// Time allowed to write a message to the peer.
writeWait time.Duration
// Time allowed to read the next pong message from the peer.
pongWait time.Duration
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod time.Duration
}
func newWebSocketProxy(ctx context.Context, handler http.Handler, logger log.CtxLogger) *webSocketProxy {
proxy := &webSocketProxy{
handler: handler,
done: ctx.Done(),
logger: logger.WithComponent("grpcutil.webSocketProxy"),
upgrader: websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
writeWait: defaultWriteWait,
pongWait: defaultPongWait,
pingPeriod: (defaultPongWait * 9) / 10,
}
return proxy
}
func (p *webSocketProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !websocket.IsWebSocketUpgrade(r) {
p.handler.ServeHTTP(w, r)
return
}
p.proxy(w, r)
}
// proxy creates a "pipeline" from the underlying response
// to a WebSocket connection. The pipeline is constructed in
// the following way:
//
// underlying response
// -> inMemoryResponseWriter
// -> scanner
// -> messages channel
// -> connection writer
//
// In the case of an error due to which we need to abort the request
// and close the WebSocket connection, we need to cancel the request context
// and stop writing any data to the WebSocket connection. This will
// automatically halt all the "pipeline nodes" after the underlying response.
func (p *webSocketProxy) proxy(w http.ResponseWriter, r *http.Request) {
ctx, cancelCtx := context.WithCancel(r.Context())
defer cancelCtx()
r = r.WithContext(ctx)
go func() {
// we're using ChanOut.Recv() so that the goroutine doesn't wait on
// the proxy to be done even when the request is done
_, _, err := cchan.ChanOut[struct{}](p.done).Recv(ctx)
p.logger.Debug(ctx).Err(err).Msgf("websocket connection will be closed")
cancelCtx()
}()
// Upgrade connection to WebSocket
conn, err := p.upgrader.Upgrade(w, r, http.Header{})
if err != nil {
p.logger.Err(ctx, err).Msg("error upgrading websocket")
return
}
defer conn.Close()
// We use a pipe to read the data being written to the underlying http.Handler
// and then write it to the WebSocket connection.
responseR, responseW := io.Pipe()
response := newInMemoryResponseWriter(responseW)
// Start the "underlying" http.Handler
go func() {
p.handler.ServeHTTP(response, r)
p.logger.Debug(ctx).Err(ctx.Err()).Msg("closing pipes")
responseW.CloseWithError(io.EOF)
}()
messages := make(chan []byte)
// startWebSocketRead and startWebSocketWrite need to cancel the context
// if they encounter an error reading from or writing to the WS connection
go p.startWebSocketRead(ctx, conn, cancelCtx)
go p.readFromHTTPResponse(ctx, responseR, messages)
p.startWebSocketWrite(ctx, messages, conn, cancelCtx)
}
// startWebSocketRead starts a read loop on the proxy's WebSocket connection.
// The read loop will stop if there's been an error reading a message.
func (p *webSocketProxy) startWebSocketRead(ctx context.Context, conn *websocket.Conn, onDone func()) {
defer onDone()
conn.SetReadLimit(512)
err := conn.SetReadDeadline(time.Now().Add(p.pongWait))
if err != nil {
p.logger.Warn(ctx).Err(err).Msgf("couldn't set read deadline %v", p.pongWait)
return
}
conn.SetPongHandler(func(string) error {
err := conn.SetReadDeadline(time.Now().Add(p.pongWait))
if err != nil {
// todo return err?
p.logger.Warn(ctx).Err(err).Msgf("couldn't set read deadline %v", p.pongWait)
}
return nil
})
for {
// The only use we have for reads right now
// is for ping, pong and close messages.
// https://pkg.go.dev/github.com/gorilla/websocket#hdr-Control_Messages
// Also, a read loop can detect client disconnects much quicker:
// https://groups.google.com/g/golang-nuts/c/FFzQO26jEoE/m/mYhcsK20EwAJ
_, _, err := conn.ReadMessage()
if err != nil {
if p.isClosedConnErr(err) {
p.logger.Debug(ctx).Err(err).Msg("closed connection")
} else {
p.logger.Warn(ctx).Err(err).Msg("read error")
}
break
}
}
}
func (p *webSocketProxy) isClosedConnErr(err error) bool {
str := err.Error()
if strings.Contains(str, "use of closed network connection") {
return true
}
return websocket.IsCloseError(
err,
websocket.CloseNormalClosure,
websocket.CloseGoingAway,
websocket.CloseAbnormalClosure,
websocket.CloseNoStatusReceived,
)
}
func (p *webSocketProxy) readFromHTTPResponse(ctx context.Context, responseReader io.Reader, c chan []byte) {
defer close(c)
scanner := bufio.NewScanner(responseReader)
for scanner.Scan() {
if len(scanner.Bytes()) == 0 {
p.logger.Warn(ctx).Err(scanner.Err()).Msg("[write] empty scan")
continue
}
p.logger.Trace(ctx).Msgf("[write] scanned %v", scanner.Text())
c <- scanner.Bytes()
}
if sErr := scanner.Err(); sErr != nil {
p.logger.Err(ctx, sErr).Msg("failed reading data from original response")
c <- []byte(sErr.Error())
}
p.logger.Debug(ctx).Msg("scanner reached end of input data")
}
func (p *webSocketProxy) startWebSocketWrite(ctx context.Context, messages chan []byte, conn *websocket.Conn, cancelFn func()) {
ticker := time.NewTicker(p.pingPeriod)
defer func() {
ticker.Stop()
cancelFn()
for range messages {
// throw away
}
}()
for {
select {
case message, ok := <-messages:
conn.SetWriteDeadline(time.Now().Add(p.writeWait)) //nolint:errcheck // always returns nil
if !ok {
// readFromHTTPResponse closed the channel.
err := conn.WriteMessage(websocket.CloseMessage, []byte{})
if err != nil {
p.logger.Warn(ctx).Err(err).Msg("failed sending close message")
}
return
}
if err := conn.WriteMessage(websocket.TextMessage, message); err != nil {
// NB: if this connection has been closed by the client
// then that will cancel the request context, which in turn
// makes the request return `{"code":1,"message":"context canceled","details":[]}`.
// This proxy will try to write that, but will fail,
// because the connection has already been closed.
e := p.logger.Warn(ctx)
if string(message) == `{"code":1,"message":"context canceled","details":[]}` {
e = p.logger.Trace(ctx)
}
e.Bytes("message", message).Err(err).Msg("failed writing websocket message")
return
}
case <-ticker.C:
conn.SetWriteDeadline(time.Now().Add(p.writeWait)) //nolint:errcheck // always returns nil
if err := conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return
}
}
}
}