forked from pmalhaire/xk6-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.go
154 lines (145 loc) · 3.28 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
package mqtt
import (
"time"
"github.com/dop251/goja"
"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 []uint8,
retain bool,
timeout uint,
success func(goja.Value) (goja.Value, error),
failure func(goja.Value) (goja.Value, error),
) error {
// sync case no callback added
if success == nil && failure == nil {
return c.publishSync(topic, qos, message, retain, timeout)
}
// 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(timeout) * 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 []uint8,
retain bool,
timeout uint,
) error {
if c.pahoClient == nil || !c.pahoClient.IsConnected() {
rt := c.vu.Runtime()
common.Throw(rt, ErrClient)
return ErrClient
}
token := c.pahoClient.Publish(topic, byte(qos), retain, message)
// sync case
if !token.WaitTimeout(time.Duration(timeout) * 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{
Time: now,
Metric: c.metrics.SentMessages,
Value: float64(1),
})
metrics.PushIfNotDone(ctx, state.Samples, metrics.Sample{
Time: now,
Metric: c.metrics.SentBytes,
Value: msgLen,
})
return nil
}
//nolint:nosnakecase // their choice not mine
func (c *client) newPublishEvent(topic string) *goja.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"), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
must(o.DefineDataProperty("topic", rt.ToValue(topic), goja.FLAG_FALSE, goja.FLAG_FALSE, goja.FLAG_TRUE))
return o
}