-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp.go
40 lines (33 loc) · 815 Bytes
/
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
package main
import (
"fmt"
"time"
"github.com/streadway/amqp"
)
func handleConnection(url string, sessConn chan *amqp.Connection) {
logger.Info(fmt.Sprintf("Try to connect to: %s", url))
conn, err := amqp.Dial(url)
if err != nil {
logger.Error(err)
time.Sleep(5 * time.Second)
handleConnection(url, sessConn)
} else {
go func() {
logger.Info(fmt.Sprintf("Connected to: %s", url))
sessConn <- conn
}()
sessErrConn := make(chan *amqp.Error)
notifyOnClose := conn.NotifyClose(sessErrConn)
go func() {
for errClose := range notifyOnClose {
logger.Error(fmt.Sprintf("Connection error: %s", errClose))
ticker := time.NewTicker(5 * time.Second)
select {
case <-ticker.C:
logger.Info("Reconnecting...")
handleConnection(url, sessConn)
}
}
}()
}
}