-
Notifications
You must be signed in to change notification settings - Fork 7
/
amqp.go
348 lines (305 loc) · 8.96 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package shoveler
import (
"errors"
"math/rand"
"net/url"
"os"
"strings"
"time"
"github.com/streadway/amqp"
)
// This should run in a new go co-routine.
func StartAMQP(config *Config, queue *ConfirmationQueue) {
// Get the configuration URL
amqpURL := config.AmqpURL
tokenStat, err := os.Stat(config.AmqpToken)
if err != nil {
log.Fatalln("Failed to stat token file:", err)
}
tokenAge := tokenStat.ModTime()
tokenContents, err := readToken(config.AmqpToken)
if err != nil {
log.Fatalln("Failed to read token, cannot recover")
}
// Set the username/password
amqpURL.User = url.UserPassword("shoveler", tokenContents)
amqpQueue := New(*amqpURL)
// Constantly check for new messages
messagesQueue := make(chan []byte)
triggerReconnect := make(chan bool)
go readMsg(messagesQueue, queue)
go CheckTokenFile(config, tokenAge, triggerReconnect)
// Listen to the channel for messages
for {
select {
case <-triggerReconnect:
log.Debugln("Triggering reconnect")
amqpQueue, err = reconnectAmqp(amqpURL, amqpQueue)
if err != nil {
log.Errorln("Failed to reconnect to AMQP:", err)
}
case msg := <-messagesQueue:
// Handle a new message to put on the message queue
TryPush:
for {
err = amqpQueue.Push(config.AmqpExchange, msg)
if err != nil {
// How to handle a failure to push?
// The UnsafePush function already should have tried to reconnect
log.Errorln("Failed to push message:", err)
// Try again in 1 second
// Sleep for random amount between 1 and 5 seconds
// Watch for new token files
randSleep := rand.Intn(4000) + 1000
log.Debugln("Sleeping for", randSleep/1000, "seconds")
select {
case <-triggerReconnect:
log.Debugln("Triggering reconnect from within failure")
amqpQueue, err = reconnectAmqp(amqpURL, amqpQueue)
if err != nil {
log.Errorln("Failed to reconnect to AMQP:", err)
}
case <-time.After(time.Duration(randSleep) * time.Millisecond):
continue TryPush
}
}
break TryPush
}
}
}
}
// reconnectAmqp reconnects to AMQP if something fails or if the token changes.
// This is safer than just reconnecting, as it will ensure that
// resources from the previous connection are cleaned up.
func reconnectAmqp(amqpURL *url.URL, curSession *Session) (*Session, error) {
// close the current session
curSession.Close()
// Create a new session and return it
newSession := New(*amqpURL)
return newSession, nil
}
// Listen to the channel for messages
func CheckTokenFile(config *Config, tokenAge time.Time, triggerReconnect chan<- bool) {
// Create a timer to check for changes in the token file ever 10 seconds
amqpURL := config.AmqpURL
checkTokenFile := time.NewTicker(10 * time.Second)
for {
<-checkTokenFile.C
log.Debugln("Checking the age of the token file...")
// Recheck the age of the token file
tokenStat, err := os.Stat(config.AmqpToken)
if err != nil {
log.Fatalln("Failed to stat token file", config.AmqpToken, "error:", err)
}
newTokenAge := tokenStat.ModTime()
if newTokenAge.After(tokenAge) {
tokenAge = newTokenAge
log.Debugln("Token file was updated, recreating AMQP connection...")
// New Token, reload the connection
tokenContents, err := readToken(config.AmqpToken)
if err != nil {
log.Fatalln("Failed to read token, cannot recover")
}
// Set the username/password
amqpURL.User = url.UserPassword("shoveler", tokenContents)
triggerReconnect <- true
}
}
}
// Read a message from the queue
func readMsg(messagesQueue chan<- []byte, queue *ConfirmationQueue) {
for {
msg, err := queue.Dequeue()
if err != nil {
log.Errorln("Failed to read from queue:", err)
continue
}
messagesQueue <- msg
}
}
// Read the token from the token location
func readToken(tokenLocation string) (string, error) {
// Get the token password
// Read in the token file
tokenContents, err := os.ReadFile(tokenLocation)
if err != nil {
log.Errorln("Unable to read file:", tokenLocation)
return "", err
}
tokenContentsStr := strings.TrimSpace(string(tokenContents))
return tokenContentsStr, nil
}
// Copied from the amqp documentation at: https://pkg.go.dev/github.com/streadway/amqp
type Session struct {
url url.URL
connection *amqp.Connection
channel *amqp.Channel
done chan bool
notifyConnClose chan *amqp.Error
notifyChanClose chan *amqp.Error
notifyConfirm chan amqp.Confirmation
isReady bool
}
var (
errNotConnected = errors.New("not connected to a server")
errAlreadyClosed = errors.New("already closed: not connected to the server")
errShutdown = errors.New("session is shutting down")
)
// New creates a new consumer state instance, and automatically
// attempts to connect to the server.
func New(url url.URL) *Session {
session := Session{
url: url,
done: make(chan bool),
}
go session.handleReconnect()
return &session
}
// handleReconnect will wait for a connection error on
// notifyConnClose, and then continuously attempt to reconnect.
func (session *Session) handleReconnect() {
for {
session.isReady = false
log.Debugln("Attempting to connect")
conn, err := session.connect()
RabbitmqReconnects.Inc()
if err != nil {
log.Warningln("Failed to connect. Retrying:", err.Error())
select {
case <-session.done:
return
case <-time.After(reconnectDelay):
}
continue
}
if done := session.handleReInit(conn); done {
break
}
}
}
// connect will create a new AMQP connection
func (session *Session) connect() (*amqp.Connection, error) {
log.Debugln("Connecting to URL:", session.url.String())
conn, err := amqp.Dial(session.url.String())
if err != nil {
return nil, err
}
session.changeConnection(conn)
log.Debugln("Connected!")
return conn, nil
}
// handleReconnect will wait for a channel error
// and then continuously attempt to re-initialize both channels
func (session *Session) handleReInit(conn *amqp.Connection) bool {
for {
session.isReady = false
err := session.init(conn)
if err != nil {
log.Warningln("Failed to initialize channel. Retrying...")
select {
case <-session.done:
return true
case <-time.After(reInitDelay):
}
continue
}
select {
case <-session.done:
return true
case err := <-session.notifyConnClose:
log.Warningln("Connection closed. Reconnecting...", err)
return false
case err := <-session.notifyChanClose:
log.Warningln("Channel closed. Re-running init...", err)
}
}
}
// init will initialize channel & declare queue
func (session *Session) init(conn *amqp.Connection) error {
ch, err := conn.Channel()
if err != nil {
return err
}
err = ch.Confirm(false)
if err != nil {
return err
}
session.changeChannel(ch)
session.isReady = true
log.Debugln("Setup!")
return nil
}
// changeConnection takes a new connection to the queue,
// and updates the close listener to reflect this.
func (session *Session) changeConnection(connection *amqp.Connection) {
session.connection = connection
session.notifyConnClose = make(chan *amqp.Error)
session.connection.NotifyClose(session.notifyConnClose)
}
// changeChannel takes a new channel to the queue,
// and updates the channel listeners to reflect this.
func (session *Session) changeChannel(channel *amqp.Channel) {
session.channel = channel
session.notifyChanClose = make(chan *amqp.Error)
session.notifyConfirm = make(chan amqp.Confirmation, 1)
session.channel.NotifyClose(session.notifyChanClose)
}
// Push will push data onto the queue, and wait for a confirm.
// If no confirms are received until within the resendTimeout,
// it continuously re-sends messages until a confirm is received.
// This will block until the server sends a confirm. Errors are
// only returned if the push action itself fails, see UnsafePush.
func (session *Session) Push(exchange string, data []byte) error {
if !session.isReady {
return errors.New("failed to push push: not connected")
}
for {
err := session.UnsafePush(exchange, data)
if err != nil {
log.Warningln("Push failed. Retrying...")
select {
case <-session.done:
return errShutdown
case <-time.After(resendDelay):
}
continue
}
return nil
}
}
// UnsafePush will push to the queue without checking for
// confirmation. It returns an error if it fails to connect.
// No guarantees are provided for whether the server will
// recieve the message.
func (session *Session) UnsafePush(exchange string, data []byte) error {
if !session.isReady {
return errNotConnected
}
return session.channel.Publish(
exchange, // Exchange
"", // Routing key
false, // Mandatory
false, // Immediate
amqp.Publishing{
ContentType: "text/plain",
Body: data,
},
)
}
// Close will cleanly shutdown the channel and connection.
func (session *Session) Close() error {
if !session.isReady {
return errAlreadyClosed
}
close(session.done)
err := session.channel.Close()
if err != nil {
return err
}
err = session.connection.Close()
if err != nil {
return err
}
session.isReady = false
return nil
}