-
Notifications
You must be signed in to change notification settings - Fork 38
/
publish.go
174 lines (159 loc) · 3.79 KB
/
publish.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
package mqtt
import (
"time"
"github.com/grafana/sobek"
"go.k6.io/k6/js/common"
"go.k6.io/k6/metrics"
)
// Publish allow to publish one message
//
//nolint:gocognit
func (c *client) Publish(
topic string,
qos int,
message string,
retain bool,
timeout uint,
success func(sobek.Value) (sobek.Value, error),
failure func(sobek.Value) (sobek.Value, error),
) error {
// sync case no callback added
if success == nil && failure == nil {
return c.publishSync(topic, qos, message, retain, timeout)
}
// check timeout value
timeoutValue, err := safeUintToInt64(timeout)
if err != nil {
rt := c.vu.Runtime()
common.Throw(rt, ErrTimeoutToLong)
return ErrTimeoutToLong
}
// async case
callback := c.vu.RegisterCallback()
go func() {
if c.pahoClient == nil || !c.pahoClient.IsConnected() {
callback(func() error {
ev := c.newErrorEvent("publish not connected")
if failure != nil {
if _, err := failure(ev); err != nil {
return err
}
}
return nil
})
return
}
token := c.pahoClient.Publish(topic, byte(qos), retain, message)
if !token.WaitTimeout(time.Duration(timeoutValue) * time.Millisecond) {
callback(func() error {
ev := c.newErrorEvent("publish timeout")
if failure != nil {
if _, err := failure(ev); err != nil {
return err
}
}
return nil
})
return
}
if err := token.Error(); err != nil {
callback(func() error {
ev := c.newErrorEvent(err.Error())
if failure != nil {
if _, err := failure(ev); err != nil {
return err
}
}
return nil
})
return
}
callback(func() error {
err := c.publishMessageMetric(float64(len(message)))
if err != nil {
return err
}
ev := c.newPublishEvent(topic)
if success != nil {
if _, err := success(ev); err != nil {
return err
}
}
return nil
})
}()
return nil
}
func (c *client) publishSync(
topic string,
qos int,
message string,
retain bool,
timeout uint,
) error {
if c.pahoClient == nil || !c.pahoClient.IsConnected() {
rt := c.vu.Runtime()
common.Throw(rt, ErrClient)
return ErrClient
}
// check timeout value
timeoutValue, err := safeUintToInt64(timeout)
if err != nil {
rt := c.vu.Runtime()
common.Throw(rt, ErrTimeoutToLong)
return ErrTimeoutToLong
}
token := c.pahoClient.Publish(topic, byte(qos), retain, message)
// sync case
if !token.WaitTimeout(time.Duration(timeoutValue) * time.Millisecond) {
rt := c.vu.Runtime()
common.Throw(rt, ErrTimeout)
return ErrTimeout
}
if err = token.Error(); err != nil {
rt := c.vu.Runtime()
common.Throw(rt, ErrPublish)
return ErrPublish
}
err = c.publishMessageMetric(float64(len(message)))
if err != nil {
return err
}
return nil
}
func (c *client) publishMessageMetric(msgLen float64) error {
// publish metrics
now := time.Now()
state := c.vu.State()
if state == nil {
return ErrState
}
ctx := c.vu.Context()
if ctx == nil {
return ErrState
}
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
TimeSeries: metrics.TimeSeries{Metric: c.metrics.SentMessages, Tags: c.metrics.TagsAndMeta.Tags},
Time: now,
Value: float64(1),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
TimeSeries: metrics.TimeSeries{Metric: c.metrics.SentBytes, Tags: c.metrics.TagsAndMeta.Tags},
Time: now,
Value: msgLen,
})
return nil
}
//nolint:nosnakecase // their choice not mine
func (c *client) newPublishEvent(topic string) *sobek.Object {
rt := c.vu.Runtime()
o := rt.NewObject()
must := func(err error) {
if err != nil {
common.Throw(rt, err)
}
}
must(o.DefineDataProperty("type", rt.ToValue("publish"), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
must(o.DefineDataProperty("topic", rt.ToValue(topic), sobek.FLAG_FALSE, sobek.FLAG_FALSE, sobek.FLAG_TRUE))
return o
}