-
Notifications
You must be signed in to change notification settings - Fork 7
/
channel.go
274 lines (221 loc) · 5.72 KB
/
channel.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
package cable
import (
"encoding/json"
"reflect"
"strings"
"sync"
"time"
"github.com/grafana/sobek"
"github.com/sirupsen/logrus"
"go.k6.io/k6/js/modules"
)
type Channel struct {
client *Client
identifier string
logger *logrus.Entry
confCh chan bool
ackMu sync.Mutex
readCh chan *cableMsg
asyncHandlers []sobek.Callable
ignoreReads bool
createdAt time.Time
ackedAt time.Time
}
func NewChannel(c *Client, identifier string) *Channel {
return &Channel{
client: c,
identifier: identifier,
logger: c.logger,
readCh: make(chan *cableMsg, 2048),
confCh: make(chan bool, 1),
createdAt: time.Now(),
}
}
// Perform sends passed action with additional data to the channel
func (ch *Channel) Perform(action string, attr sobek.Value) error {
rt := ch.client.vu.Runtime()
obj := attr.ToObject(rt).Export().(map[string]interface{})
obj["action"] = action
data, err := json.Marshal(obj)
if err != nil {
return err
}
return ch.client.send(&cableMsg{
Command: "message",
Identifier: ch.identifier,
Data: string(data),
})
}
// IgnoreReads allows skipping collecting incoming messages (in case you only care about the subscription)
func (ch *Channel) IgnoreReads() {
ch.ignoreReads = true
}
// Receive checks channels messages query for message, sugar for ReceiveN(1, attrs)
func (ch *Channel) Receive(attr sobek.Value) interface{} {
results := ch.ReceiveN(1, attr)
if len(results) == 0 {
return nil
}
return results[0]
}
// ReceiveN checks channels messages query for provided number of messages satisfying provided condition.
func (ch *Channel) ReceiveN(n int, cond sobek.Value) []interface{} {
var results []interface{}
timeout := ch.client.recTimeout
timer := time.NewTimer(timeout)
matcher, err := ch.buildMatcher(cond)
if err != nil {
panic(err)
}
i := 0
for {
select {
case msg := <-ch.readCh:
timer.Reset(timeout)
if !matcher.Match(msg.Message) {
continue
}
results = append(results, msg.Message)
i++
if i >= n {
return results
}
case <-timer.C:
ch.logger.Warn("receive timeout exceeded; consider increasing receiveTimeoutMs configuration option")
return results
}
}
}
// ReceiveAll fethes all messages for a given number of seconds.
func (ch *Channel) ReceiveAll(sec int, cond sobek.Value) []interface{} {
var results []interface{}
timeout := time.Duration(sec) * time.Second
timer := time.NewTimer(timeout)
matcher, err := ch.buildMatcher(cond)
if err != nil {
panic(err)
}
for {
select {
case msg := <-ch.readCh:
if !matcher.Match(msg.Message) {
continue
}
results = append(results, msg.Message)
case <-timer.C:
return results
}
}
}
// Register callback to receive messages asynchronously
func (ch *Channel) OnMessage(fn sobek.Value) {
f, isFunc := sobek.AssertFunction(fn)
if !isFunc {
panic("argument must be a function")
}
ch.asyncHandlers = append(ch.asyncHandlers, f)
}
func (ch *Channel) AckDuration() int64 {
ch.ackMu.Lock()
defer ch.ackMu.Unlock()
return ch.ackedAt.Sub(ch.createdAt).Milliseconds()
}
func (ch *Channel) handleAck(val bool, when time.Time) {
ch.ackMu.Lock()
defer ch.ackMu.Unlock()
ch.ackedAt = when
ch.confCh <- val
}
func (ch *Channel) handleIncoming(msg *cableMsg) {
ch.handleAsync(msg)
if ch.ignoreReads {
return
}
ch.readCh <- msg
}
func (ch *Channel) handleAsync(msg *cableMsg) {
if msg == nil {
return
}
ch.client.mu.Lock()
defer ch.client.mu.Unlock()
if ch.client.disconnected {
return
}
for _, h := range ch.asyncHandlers {
_, err := h(sobek.Undefined(), ch.client.vu.Runtime().ToValue(msg.Message))
if err != nil {
if !strings.Contains(err.Error(), "context canceled") {
ch.logger.Errorf("can't call provided function: %s", err)
}
}
}
}
type Matcher interface {
Match(msg interface{}) bool
}
type FuncMatcher struct {
vu modules.VU
f sobek.Callable
}
func (m *FuncMatcher) Match(msg interface{}) bool {
result, err := m.f(sobek.Undefined(), m.vu.Runtime().ToValue(msg))
if err != nil {
m.vu.State().Logger.Errorf("can't call provided function: %v", err)
}
return result.ToBoolean()
}
type StringMatcher struct {
expected string
}
func (m *StringMatcher) Match(msg interface{}) bool {
msgStr, ok := msg.(string)
if !ok {
return false
}
return m.expected == msgStr
}
type AttrMatcher struct {
expected map[string]interface{}
}
func (m *AttrMatcher) Match(msg interface{}) bool {
msgObj, ok := msg.(map[string]interface{})
if !ok {
return false
}
for k, v := range m.expected {
if !reflect.DeepEqual(v, msgObj[k]) {
return false
}
}
return true
}
type PassthruMatcher struct{}
func (PassthruMatcher) Match(_ interface{}) bool {
return true
}
// buildMatcher returns the corresponding matcher depending on condition type:
// - when condition is nil, match is always successful
// - when condition is a func, result of func(msg) is used as a result of match
// - when condition is a string, match is successful when message matches provided string
// - when condition is an object, match is successful when message includes all object attributes
func (ch *Channel) buildMatcher(cond sobek.Value) (Matcher, error) {
if cond == nil || sobek.IsUndefined(cond) || sobek.IsNull(cond) {
return &PassthruMatcher{}, nil
}
if _, ok := cond.(*sobek.Symbol); ok {
return &StringMatcher{cond.String()}, nil
}
userFunc, isFunc := sobek.AssertFunction(cond)
if isFunc {
return &FuncMatcher{ch.client.vu, userFunc}, nil
}
// we need to pass object through json unmarshalling to use same types for numbers
jsonAttr, err := cond.ToObject(ch.client.vu.Runtime()).MarshalJSON()
if err != nil {
return nil, err
}
var matcher map[string]interface{}
_ = json.Unmarshal(jsonAttr, &matcher)
return &AttrMatcher{matcher}, nil
}