-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ws.go
502 lines (417 loc) · 13.3 KB
/
ws.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2017 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package ws
import (
"context"
"crypto/tls"
"errors"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/dop251/goja"
"github.com/gorilla/websocket"
"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/stats"
)
// ErrWSInInitContext is returned when websockets are using in the init context
var ErrWSInInitContext = common.NewInitContextError("using websockets in the init context is not supported")
type WS struct{}
type pingDelta struct {
ping time.Time
pong time.Time
}
type Socket struct {
ctx context.Context
conn *websocket.Conn
eventHandlers map[string][]goja.Callable
scheduled chan goja.Callable
done chan struct{}
shutdownOnce sync.Once
msgSentTimestamps []time.Time
msgReceivedTimestamps []time.Time
pingSendTimestamps map[string]time.Time
pingSendCounter int
pingTimestamps []pingDelta
}
type WSHTTPResponse struct {
URL string `json:"url"`
Status int `json:"status"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
Error string `json:"error"`
}
const writeWait = 10 * time.Second
func New() *WS {
return &WS{}
}
func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTPResponse, error) {
rt := common.GetRuntime(ctx)
state := common.GetState(ctx)
if state == nil {
return nil, ErrWSInInitContext
}
// The params argument is optional
var callableV, paramsV goja.Value
switch len(args) {
case 2:
paramsV = args[0]
callableV = args[1]
case 1:
paramsV = goja.Undefined()
callableV = args[0]
default:
return nil, errors.New("invalid number of arguments to ws.connect")
}
// Get the callable (required)
setupFn, isFunc := goja.AssertFunction(callableV)
if !isFunc {
return nil, errors.New("last argument to ws.connect must be a function")
}
// Leave header to nil by default so we can pass it directly to the Dialer
var header http.Header
tags := state.Options.RunTags.CloneTags()
// Parse the optional second argument (params)
if !goja.IsUndefined(paramsV) && !goja.IsNull(paramsV) {
params := paramsV.ToObject(rt)
for _, k := range params.Keys() {
switch k {
case "headers":
header = http.Header{}
headersV := params.Get(k)
if goja.IsUndefined(headersV) || goja.IsNull(headersV) {
continue
}
headersObj := headersV.ToObject(rt)
if headersObj == nil {
continue
}
for _, key := range headersObj.Keys() {
header.Set(key, headersObj.Get(key).String())
}
case "tags":
tagsV := params.Get(k)
if goja.IsUndefined(tagsV) || goja.IsNull(tagsV) {
continue
}
tagObj := tagsV.ToObject(rt)
if tagObj == nil {
continue
}
for _, key := range tagObj.Keys() {
tags[key] = tagObj.Get(key).String()
}
}
}
}
if state.Options.SystemTags["url"] {
tags["url"] = url
}
if state.Options.SystemTags["group"] {
tags["group"] = state.Group.Path
}
// Pass a custom net.Dial function to websocket.Dialer that will substitute
// the underlying net.Conn with our own tracked netext.Conn
netDial := func(network, address string) (net.Conn, error) {
return state.Dialer.DialContext(ctx, network, address)
}
// Overriding the NextProtos to avoid talking http2
var tlsConfig *tls.Config
if state.TLSConfig != nil {
tlsConfig = state.TLSConfig.Clone()
tlsConfig.NextProtos = []string{"http/1.1"}
}
wsd := websocket.Dialer{
NetDial: netDial,
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
}
start := time.Now()
conn, httpResponse, connErr := wsd.Dial(url, header)
connectionEnd := time.Now()
connectionDuration := stats.D(connectionEnd.Sub(start))
socket := Socket{
ctx: ctx,
conn: conn,
eventHandlers: make(map[string][]goja.Callable),
pingSendTimestamps: make(map[string]time.Time),
scheduled: make(chan goja.Callable),
done: make(chan struct{}),
}
if state.Options.SystemTags["ip"] && conn.RemoteAddr() != nil {
if ip, _, err := net.SplitHostPort(conn.RemoteAddr().String()); err == nil {
tags["ip"] = ip
}
}
// Run the user-provided set up function
if _, err := setupFn(goja.Undefined(), rt.ToValue(&socket)); err != nil {
return nil, err
}
if connErr != nil {
// Pass the error to the user script before exiting immediately
socket.handleEvent("error", rt.ToValue(connErr))
return nil, connErr
}
wsResponse, wsRespErr := wrapHTTPResponse(httpResponse)
if wsRespErr != nil {
return nil, wsRespErr
}
wsResponse.URL = url
defer func() { _ = conn.Close() }()
if state.Options.SystemTags["status"] {
tags["status"] = strconv.Itoa(httpResponse.StatusCode)
}
if state.Options.SystemTags["subproto"] {
tags["subproto"] = httpResponse.Header.Get("Sec-WebSocket-Protocol")
}
// The connection is now open, emit the event
socket.handleEvent("open")
// Pass ping/pong events through the main control loop
pingChan := make(chan string)
pongChan := make(chan string)
conn.SetPingHandler(func(msg string) error { pingChan <- msg; return nil })
conn.SetPongHandler(func(pingID string) error { pongChan <- pingID; return nil })
readDataChan := make(chan []byte)
readCloseChan := make(chan int)
readErrChan := make(chan error)
// Wraps a couple of channels around conn.ReadMessage
go readPump(conn, readDataChan, readErrChan, readCloseChan)
// This is the main control loop. All JS code (including error handlers)
// should only be executed by this thread to avoid race conditions
for {
select {
case pingData := <-pingChan:
// Handle pings received from the server
// - trigger the `ping` event
// - reply with pong (needed when `SetPingHandler` is overwritten)
err := socket.conn.WriteControl(websocket.PongMessage, []byte(pingData), time.Now().Add(writeWait))
if err != nil {
socket.handleEvent("error", rt.ToValue(err))
}
socket.handleEvent("ping")
case pingID := <-pongChan:
// Handle pong responses to our pings
socket.trackPong(pingID)
socket.handleEvent("pong")
case readData := <-readDataChan:
socket.msgReceivedTimestamps = append(socket.msgReceivedTimestamps, time.Now())
socket.handleEvent("message", rt.ToValue(string(readData)))
case readErr := <-readErrChan:
socket.handleEvent("error", rt.ToValue(readErr))
case readClose := <-readCloseChan:
// handle server close
socket.handleEvent("close", rt.ToValue(readClose))
case scheduledFn := <-socket.scheduled:
if _, err := scheduledFn(goja.Undefined()); err != nil {
return nil, err
}
case <-ctx.Done():
// VU is shutting down during an interrupt
// socket events will not be forwarded to the VU
_ = socket.closeConnection(websocket.CloseGoingAway)
case <-socket.done:
// This is the final exit point normally triggered by closeConnection
end := time.Now()
sessionDuration := stats.D(end.Sub(start))
sampleTags := stats.IntoSampleTags(&tags)
stats.PushIfNotCancelled(ctx, state.Samples, stats.ConnectedSamples{
Samples: []stats.Sample{
{Metric: metrics.WSSessions, Time: start, Tags: sampleTags, Value: 1},
{Metric: metrics.WSConnecting, Time: start, Tags: sampleTags, Value: connectionDuration},
{Metric: metrics.WSSessionDuration, Time: start, Tags: sampleTags, Value: sessionDuration},
},
Tags: sampleTags,
Time: start,
})
for _, msgSentTimestamp := range socket.msgSentTimestamps {
stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{
Metric: metrics.WSMessagesSent,
Time: msgSentTimestamp,
Tags: sampleTags,
Value: 1,
})
}
for _, msgReceivedTimestamp := range socket.msgReceivedTimestamps {
stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{
Metric: metrics.WSMessagesReceived,
Time: msgReceivedTimestamp,
Tags: sampleTags,
Value: 1,
})
}
for _, pingDelta := range socket.pingTimestamps {
stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{
Metric: metrics.WSPing,
Time: pingDelta.pong,
Tags: sampleTags,
Value: stats.D(pingDelta.pong.Sub(pingDelta.ping)),
})
}
return wsResponse, nil
}
}
}
func (s *Socket) On(event string, handler goja.Value) {
if handler, ok := goja.AssertFunction(handler); ok {
s.eventHandlers[event] = append(s.eventHandlers[event], handler)
}
}
func (s *Socket) handleEvent(event string, args ...goja.Value) {
if handlers, ok := s.eventHandlers[event]; ok {
for _, handler := range handlers {
if _, err := handler(goja.Undefined(), args...); err != nil {
common.Throw(common.GetRuntime(s.ctx), err)
}
}
}
}
func (s *Socket) Send(message string) {
// NOTE: No binary message support for the time being since goja doesn't
// support typed arrays.
rt := common.GetRuntime(s.ctx)
writeData := []byte(message)
if err := s.conn.WriteMessage(websocket.TextMessage, writeData); err != nil {
s.handleEvent("error", rt.ToValue(err))
}
s.msgSentTimestamps = append(s.msgSentTimestamps, time.Now())
}
func (s *Socket) Ping() {
rt := common.GetRuntime(s.ctx)
deadline := time.Now().Add(writeWait)
pingID := strconv.Itoa(s.pingSendCounter)
data := []byte(pingID)
err := s.conn.WriteControl(websocket.PingMessage, data, deadline)
if err != nil {
s.handleEvent("error", rt.ToValue(err))
return
}
s.pingSendTimestamps[pingID] = time.Now()
s.pingSendCounter++
}
func (s *Socket) trackPong(pingID string) {
pongTimestamp := time.Now()
if _, ok := s.pingSendTimestamps[pingID]; !ok {
// We received a pong for a ping we didn't send; ignore
// (this shouldn't happen with a compliant server)
return
}
pingTimestamp := s.pingSendTimestamps[pingID]
s.pingTimestamps = append(s.pingTimestamps, pingDelta{pingTimestamp, pongTimestamp})
}
func (s *Socket) SetTimeout(fn goja.Callable, timeoutMs int) {
// Starts a goroutine, blocks once on the timeout and pushes the callable
// back to the main loop through the scheduled channel
go func() {
select {
case <-time.After(time.Duration(timeoutMs) * time.Millisecond):
s.scheduled <- fn
case <-s.done:
return
}
}()
}
func (s *Socket) SetInterval(fn goja.Callable, intervalMs int) {
// Starts a goroutine, blocks forever on the ticker and pushes the callable
// back to the main loop through the scheduled channel
go func() {
ticker := time.NewTicker(time.Duration(intervalMs) * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.scheduled <- fn
case <-s.done:
return
}
}
}()
}
func (s *Socket) Close(args ...goja.Value) {
code := websocket.CloseGoingAway
if len(args) > 0 {
code = int(args[0].ToInteger())
}
_ = s.closeConnection(code)
}
// Attempts to close the websocket gracefully
func (s *Socket) closeConnection(code int) error {
var err error
s.shutdownOnce.Do(func() {
rt := common.GetRuntime(s.ctx)
writeErr := s.conn.WriteControl(websocket.CloseMessage,
websocket.FormatCloseMessage(code, ""),
time.Now().Add(writeWait),
)
if writeErr != nil {
// Just call the handler, we'll try to close the connection anyway
s.handleEvent("error", rt.ToValue(err))
err = writeErr
}
// trigger `close` event when the client closes the connection
s.handleEvent("close", rt.ToValue(code))
_ = s.conn.Close()
// Stops the main control loop
close(s.done)
})
return err
}
// Wraps conn.ReadMessage in a channel
func readPump(conn *websocket.Conn, readChan chan []byte, errorChan chan error, closeChan chan int) {
defer func() { _ = conn.Close() }()
for {
_, message, err := conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
closeChan <- err.(*websocket.CloseError).Code
} else if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
// Emit the error if it is not CloseNormalClosure
// and the error is not originated from closing the socket ourselves with `CloseGoingAway`
errorChan <- err
}
//CloseGoingAway errors are ignored
return
}
readChan <- message
}
}
// Wrap the raw HTTPResponse we received to a WSHTTPResponse we can pass to the user
func wrapHTTPResponse(httpResponse *http.Response) (*WSHTTPResponse, error) {
wsResponse := WSHTTPResponse{
Status: httpResponse.StatusCode,
}
body, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
err = httpResponse.Body.Close()
if err != nil {
return nil, err
}
wsResponse.Body = string(body)
wsResponse.Headers = make(map[string]string, len(httpResponse.Header))
for k, vs := range httpResponse.Header {
wsResponse.Headers[k] = strings.Join(vs, ", ")
}
return &wsResponse, nil
}