This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
amqp.go
223 lines (196 loc) · 5 KB
/
amqp.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
// Package amqp contains AMQP API for a remote server.
package amqp
import (
"context"
"encoding/json"
"fmt"
"time"
amqpDriver "github.com/rabbitmq/amqp091-go"
"github.com/vmihailenco/msgpack/v5"
"go.k6.io/k6/js/modules"
)
const version = "v0.4.1"
// AMQP type holds connection to a remote AMQP server.
type AMQP struct {
Version string
Connections *map[int]*amqpDriver.Connection
MaxConnID *int
Queue *Queue
Exchange *Exchange
}
// Options defines configuration options for an AMQP session.
type Options struct {
ConnectionURL string
}
// PublishOptions defines a message payload with delivery options.
type PublishOptions struct {
ConnectionID int
QueueName string
Body string
Headers amqpDriver.Table
Exchange string
ContentType string
Mandatory bool
Immediate bool
Persistent bool
CorrelationID string
ReplyTo string
Expiration string
MessageID string
Timestamp int64 // unix epoch timestamp in seconds
Type string
UserID string
AppID string
}
// ConsumeOptions defines options for use when consuming a message.
type ConsumeOptions struct {
Consumer string
AutoAck bool
Exclusive bool
NoLocal bool
NoWait bool
Args amqpDriver.Table
}
// ListenerType is the message handler implemented within JavaScript.
type ListenerType func(string) error
// ListenOptions defines options for subscribing to message(s) within a queue.
type ListenOptions struct {
ConnectionID int
Listener ListenerType
QueueName string
Consumer string
AutoAck bool
Exclusive bool
NoLocal bool
NoWait bool
Args amqpDriver.Table
}
const messagepack = "application/x-msgpack"
// Start establishes a session with an AMQP server given the provided options.
func (amqp *AMQP) Start(options Options) (int, error) {
conn, err := amqpDriver.Dial(options.ConnectionURL)
*amqp.MaxConnID++
(*amqp.Connections)[*amqp.MaxConnID] = conn
return *amqp.MaxConnID, err
}
// GetConn gets an initialised connection by ID, or returns the last initialised one if ID is 0
func (amqp *AMQP) GetConn(connID int) (*amqpDriver.Connection, error) {
if connID == 0 {
conn := (*amqp.Connections)[*amqp.MaxConnID]
if conn == nil {
return &amqpDriver.Connection{}, fmt.Errorf("connection not initialised")
}
return conn, nil
}
conn := (*amqp.Connections)[connID]
if conn == nil {
return &amqpDriver.Connection{}, fmt.Errorf("connection with ID %d not initialised", connID)
}
return conn, nil
}
// Publish delivers the payload using options provided.
func (amqp *AMQP) Publish(options PublishOptions) error {
conn, err := amqp.GetConn(options.ConnectionID)
if err != nil {
return err
}
ch, err := conn.Channel()
if err != nil {
return err
}
defer func() {
_ = ch.Close()
}()
publishing := amqpDriver.Publishing{
Headers: options.Headers,
ContentType: options.ContentType,
}
if options.ContentType == messagepack {
var jsonParsedBody interface{}
if err = json.Unmarshal([]byte(options.Body), &jsonParsedBody); err != nil {
return err
}
publishing.Body, err = msgpack.Marshal(jsonParsedBody)
if err != nil {
return err
}
} else {
publishing.Body = []byte(options.Body)
}
if options.Persistent {
publishing.DeliveryMode = amqpDriver.Persistent
}
// Well, I guess 1970-01-01 isn't allowed now.
if options.Timestamp != 0 {
publishing.Timestamp = time.Unix(options.Timestamp, 0)
}
publishing.CorrelationId = options.CorrelationID
publishing.ReplyTo = options.ReplyTo
publishing.Expiration = options.Expiration
publishing.MessageId = options.MessageID
publishing.Type = options.Type
publishing.UserId = options.UserID
publishing.AppId = options.AppID
return ch.PublishWithContext(
context.Background(), // TODO: use vu context
options.Exchange,
options.QueueName,
options.Mandatory,
options.Immediate,
publishing,
)
}
// Listen binds to an AMQP queue in order to receive message(s) as they are received.
func (amqp *AMQP) Listen(options ListenOptions) error {
conn, err := amqp.GetConn(options.ConnectionID)
if err != nil {
return err
}
ch, err := conn.Channel()
if err != nil {
return err
}
defer func() {
_ = ch.Close()
}()
msgs, err := ch.Consume(
options.QueueName,
options.Consumer,
options.AutoAck,
options.Exclusive,
options.NoLocal,
options.NoWait,
options.Args,
)
if err != nil {
return err
}
go func() {
for d := range msgs {
err = options.Listener(string(d.Body))
}
}()
return err
}
func init() {
connections := make(map[int]*amqpDriver.Connection)
maxConnID := 0
queue := Queue{
Connections: &connections,
MaxConnID: &maxConnID,
}
exchange := Exchange{
Connections: &connections,
MaxConnID: &maxConnID,
}
generalAMQP := AMQP{
Version: version,
Connections: &connections,
MaxConnID: &maxConnID,
Queue: &queue,
Exchange: &exchange,
}
modules.Register("k6/x/amqp", &generalAMQP)
modules.Register("k6/x/amqp/queue", &queue)
modules.Register("k6/x/amqp/exchange", &exchange)
}