-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconnect.go
52 lines (43 loc) · 805 Bytes
/
reconnect.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
package smplmsg
import (
"time"
"github.com/pkg/errors"
"github.com/streadway/amqp"
)
func (c *client) monitorConnection() {
for {
select {
case <-c.endMonitoring:
return
case <-c.amqpCh.errCh:
err := c.reconnect()
if err != nil {
time.Sleep(c.retryTimeout)
err := c.reconnect()
if err != nil {
panic(err)
}
}
}
}
}
func (c *client) reconnect() error {
conn, err := amqp.Dial(c.uri)
if err != nil {
return err
}
ch, err := conn.Channel()
if err != nil {
return errors.Wrap(err, "failed to open channel")
}
c.amqpCh = &amqpCh{
conn: conn,
ch: ch,
errCh: conn.NotifyClose(make(chan *amqp.Error)),
}
err = declareExchange(ch, c.exchange)
if err != nil {
return errors.Wrap(err, "failed to redeclare exchange")
}
return nil
}