-
Notifications
You must be signed in to change notification settings - Fork 7
/
cable.go
170 lines (142 loc) · 4.42 KB
/
cable.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
package cable
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"time"
"go.k6.io/k6/js/common"
"go.k6.io/k6/lib"
"go.k6.io/k6/metrics"
"github.com/gorilla/websocket"
"github.com/grafana/sobek"
"github.com/sirupsen/logrus"
)
// errCableInInitContext is returned when cable used in the init context
var errCableInInitContext = common.NewInitContextError("using cable in the init context is not supported")
// Connect connects to the websocket, creates and starts client, and returns it to the js.
func (c *Cable) Connect(cableUrl string, opts sobek.Value) (*Client, error) {
state := c.vu.State()
if state == nil {
return nil, errCableInInitContext
}
cOpts, err := parseOptions(c.vu.Runtime(), opts)
if err != nil {
return nil, err
}
wsd := createDialer(state, cOpts.handshakeTimeout())
connectionStart := time.Now()
headers := cOpts.header()
if headers.Get("ORIGIN") == "" {
uri, perr := url.Parse(cableUrl)
if perr == nil {
var scheme string
if uri.Scheme == "wss" {
scheme = "https"
} else {
scheme = "http"
}
origin := fmt.Sprintf("%s://%s", scheme, uri.Host)
headers.Set("ORIGIN", origin)
}
}
if cOpts.codec() == JSONCodec {
headers.Set("Sec-WebSocket-Protocol", "actioncable-v1-json")
} else if cOpts.codec() == MsgPackCodec {
headers.Set("Sec-WebSocket-Protocol", "actioncable-v1-msgpack")
} else if cOpts.codec() == ProtobufCodec {
headers.Set("Sec-WebSocket-Protocol", "actioncable-v1-protobuf")
}
level, err := logrus.ParseLevel(cOpts.LogLevel)
if err == nil {
if logger, ok := state.Logger.(*logrus.Logger); ok {
logger.SetLevel(level)
}
}
logger := state.Logger.WithField("source", "cable")
conn, httpResponse, connErr := wsd.DialContext(c.vu.Context(), cableUrl, headers)
connectionEnd := time.Now()
tagsAndMeta := state.Tags.GetCurrentValues()
if state.Options.SystemTags.Has(metrics.TagIP) && conn != nil && conn.RemoteAddr() != nil {
if ip, _, err := net.SplitHostPort(conn.RemoteAddr().String()); err == nil {
tagsAndMeta.SetSystemTagOrMeta(metrics.TagIP, ip)
}
}
if httpResponse != nil {
if state.Options.SystemTags.Has(metrics.TagStatus) {
tagsAndMeta.SetSystemTagOrMeta(metrics.TagStatus, strconv.Itoa(httpResponse.StatusCode))
}
if state.Options.SystemTags.Has(metrics.TagSubproto) {
tagsAndMeta.SetSystemTagOrMeta(metrics.TagSubproto, httpResponse.Header.Get("Sec-WebSocket-Protocol"))
}
}
if state.Options.SystemTags.Has(metrics.TagURL) {
tagsAndMeta.SetSystemTagOrMetaIfEnabled(state.Options.SystemTags, metrics.TagURL, cableUrl)
}
metrics.PushIfNotDone(c.vu.Context(), state.Samples, metrics.ConnectedSamples{
Samples: []metrics.Sample{
{
TimeSeries: metrics.TimeSeries{
Metric: state.BuiltinMetrics.WSSessions,
Tags: tagsAndMeta.Tags,
},
Time: connectionStart,
Metadata: tagsAndMeta.Metadata,
Value: 1,
},
{
TimeSeries: metrics.TimeSeries{
Metric: state.BuiltinMetrics.WSConnecting,
Tags: tagsAndMeta.Tags,
},
Time: connectionStart,
Metadata: tagsAndMeta.Metadata,
Value: metrics.D(connectionEnd.Sub(connectionStart)),
},
},
Tags: tagsAndMeta.Tags,
Time: connectionStart,
})
if connErr != nil {
logger.Errorf("failed to connect: %v", connErr)
return nil, nil
}
client := Client{
vu: c.vu,
conn: conn,
codec: cOpts.codec(),
logger: logger,
channels: make(map[string]*Channel),
readCh: make(chan *cableMsg, 1024),
errorCh: make(chan error, 1024),
closeCh: make(chan int, 1),
recTimeout: cOpts.receiveTimeout(),
sampleTags: tagsAndMeta.Tags,
samplesOutput: state.Samples,
}
err = client.start()
if err != nil {
logger.Errorf("failed to initialize Action Cable connection: %v", err)
return nil, nil
}
return &client, nil
}
func createDialer(state *lib.State, handshakeTimeout time.Duration) websocket.Dialer {
// 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{
HandshakeTimeout: handshakeTimeout,
// Pass a custom net.DialContext function to websocket.Dialer that will substitute
// the underlying net.Conn with k6 tracked netext.Conn
NetDialContext: state.Dialer.DialContext,
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
}
return wsd
}