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
/
queues.go
201 lines (183 loc) · 4.6 KB
/
queues.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
package amqp
import (
"fmt"
amqpDriver "github.com/rabbitmq/amqp091-go"
)
// Queue defines a connection to a point-to-point destination.
type Queue struct {
Version string
Connections *map[int]*amqpDriver.Connection
MaxConnID *int
}
// QueueOptions defines configuration settings for accessing a queue.
type QueueOptions struct {
ConnectionURL string
}
// DeclareOptions provides queue options when declaring (creating) a queue.
type DeclareOptions struct {
ConnectionID int
Name string
Durable bool
DeleteWhenUnused bool
Exclusive bool
NoWait bool
Args amqpDriver.Table
}
// QueueInspectOptions provide options when inspecting a queue.
type QueueInspectOptions struct {
ConnectionID int
}
// QueueDeleteOptions provide options when deleting a queue.
type QueueDeleteOptions struct {
ConnectionID int
}
// QueueBindOptions provides options when binding a queue to an exchange in order to receive message(s).
type QueueBindOptions struct {
ConnectionID int
QueueName string
ExchangeName string
RoutingKey string
NoWait bool
Args amqpDriver.Table
}
// QueueUnbindOptions provides options when unbinding a queue from an exchange to stop receiving message(s).
type QueueUnbindOptions struct {
ConnectionID int
QueueName string
ExchangeName string
RoutingKey string
Args amqpDriver.Table
}
// QueuePurgeOptions provide options when purging (emptying) a queue.
type QueuePurgeOptions struct {
ConnectionID int
}
// GetConn gets an initialised connection by ID, or returns the last initialised one if ID is 0
func (queue *Queue) GetConn(connID int) (*amqpDriver.Connection, error) {
if connID == 0 {
conn := (*queue.Connections)[*queue.MaxConnID]
if conn == nil {
return &amqpDriver.Connection{}, fmt.Errorf("connection not initialised")
}
return conn, nil
}
conn := (*queue.Connections)[connID]
if conn == nil {
return &amqpDriver.Connection{}, fmt.Errorf("connection with ID %d not initialised", connID)
}
return conn, nil
}
// Declare creates a new queue given the provided options.
func (queue *Queue) Declare(options DeclareOptions) (amqpDriver.Queue, error) {
conn, err := queue.GetConn(options.ConnectionID)
if err != nil {
return amqpDriver.Queue{}, err
}
ch, err := conn.Channel()
if err != nil {
return amqpDriver.Queue{}, err
}
defer func() {
_ = ch.Close()
}()
return ch.QueueDeclare(
options.Name,
options.Durable,
options.DeleteWhenUnused,
options.Exclusive,
options.NoWait,
options.Args,
)
}
// Inspect provides queue metadata given queue name.
func (queue *Queue) Inspect(name string, options QueueInspectOptions) (amqpDriver.Queue, error) {
conn, err := queue.GetConn(options.ConnectionID)
if err != nil {
return amqpDriver.Queue{}, err
}
ch, err := conn.Channel()
if err != nil {
return amqpDriver.Queue{}, err
}
defer func() {
_ = ch.Close()
}()
return ch.QueueInspect(name)
}
// Delete removes a queue from the remote server given the queue name.
func (queue *Queue) Delete(name string, options QueueDeleteOptions) error {
conn, err := queue.GetConn(options.ConnectionID)
if err != nil {
return err
}
ch, err := conn.Channel()
if err != nil {
return err
}
defer func() {
_ = ch.Close()
}()
_, err = ch.QueueDelete(
name,
false, // ifUnused
false, // ifEmpty
false, // noWait
)
return err
}
// Bind subscribes a queue to an exchange in order to receive message(s).
func (queue *Queue) Bind(options QueueBindOptions) error {
conn, err := queue.GetConn(options.ConnectionID)
if err != nil {
return err
}
ch, err := conn.Channel()
if err != nil {
return err
}
defer func() {
_ = ch.Close()
}()
return ch.QueueBind(
options.QueueName,
options.RoutingKey,
options.ExchangeName,
options.NoWait,
options.Args,
)
}
// Unbind removes a queue subscription from an exchange to discontinue receiving message(s).
func (queue *Queue) Unbind(options QueueUnbindOptions) error {
conn, err := queue.GetConn(options.ConnectionID)
if err != nil {
return err
}
ch, err := conn.Channel()
if err != nil {
return err
}
defer func() {
_ = ch.Close()
}()
return ch.QueueUnbind(
options.QueueName,
options.RoutingKey,
options.ExchangeName,
options.Args,
)
}
// Purge removes all non-consumed message(s) from the specified queue.
func (queue *Queue) Purge(name string, noWait bool, options QueuePurgeOptions) (int, error) {
conn, err := queue.GetConn(options.ConnectionID)
if err != nil {
return 0, err
}
ch, err := conn.Channel()
if err != nil {
return 0, err
}
defer func() {
_ = ch.Close()
}()
return ch.QueuePurge(name, noWait)
}