forked from walterwanderley/xk6-stomp
/
transaction.go
executable file
·75 lines (69 loc) · 1.84 KB
/
transaction.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
package stomp
import (
"time"
"github.com/go-stomp/stomp/v3"
"github.com/go-stomp/stomp/v3/frame"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/stats"
)
type Transaction struct {
*stomp.Transaction
vu modules.VU
}
func (tx *Transaction) Send(destination, contentType string, body []byte, opts *SendOptions) (err error) {
startedAt := time.Now()
defer func() {
now := time.Now()
reportStats(tx.vu, sendMessageTiming, nil, now, stats.D(now.Sub(startedAt)))
if err != nil {
reportStats(tx.vu, sendMessageErrors, nil, now, 1)
} else {
reportStats(tx.vu, dataSent, nil, now, float64(len(body)))
reportStats(tx.vu, sendMessage, nil, now, 1)
}
}()
if opts == nil {
opts = new(SendOptions)
}
var sendOpts []func(*frame.Frame) error
if opts.Receipt {
sendOpts = append(sendOpts, stomp.SendOpt.Receipt)
}
for k, v := range opts.Headers {
sendOpts = append(sendOpts, stomp.SendOpt.Header(k, v))
}
err = tx.Transaction.Send(destination, contentType, body, sendOpts...)
return
}
func (tx *Transaction) Ack(m *Message) error {
now := time.Now()
if m.Header.Get(frame.Id) == "" {
m.Header.Set(frame.Id, m.Header.Get(frame.Ack))
}
if m.Header.Get(frame.MessageId) == "" {
m.Header.Set(frame.MessageId, m.Header.Get(frame.Ack))
}
err := tx.Transaction.Ack(m.Message)
if err != nil {
reportStats(tx.vu, ackMessageErrors, nil, now, 1)
} else {
reportStats(tx.vu, ackMessage, nil, now, 1)
}
return err
}
func (tx *Transaction) Nack(m *Message) error {
now := time.Now()
if m.Header.Get(frame.Id) == "" {
m.Header.Set(frame.Id, m.Header.Get(frame.Ack))
}
if m.Header.Get(frame.MessageId) == "" {
m.Header.Set(frame.MessageId, m.Header.Get(frame.Ack))
}
err := tx.Transaction.Nack(m.Message)
if err != nil {
reportStats(tx.vu, nackMessageErrors, nil, now, 1)
} else {
reportStats(tx.vu, nackMessage, nil, now, 1)
}
return err
}