forked from ivanmashin/rabbitmq
/
connection.go
154 lines (124 loc) · 3.29 KB
/
connection.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
package rabbitmq
import (
"context"
"errors"
"fmt"
"github.com/Maksumys/go-hare/pkg/backoff"
amqp "github.com/rabbitmq/amqp091-go"
"log/slog"
"sync"
"sync/atomic"
)
type ConnectionFactory func() (*amqp.Connection, error)
func NewConnection(conn *amqp.Connection, factory ConnectionFactory, logger *slog.Logger) *Connection {
c := &Connection{
Connection: conn,
connectionFactory: factory,
notifyClose: make([]chan error, 0),
notifyReconnect: make([]chan error, 0),
mu: sync.RWMutex{},
logger: logger,
}
go c.watchReconnect(context.Background())
return c
}
type Connection struct {
*amqp.Connection
connectionFactory func() (*amqp.Connection, error)
reconnecting atomic.Bool
notifyClose []chan error
notifyReconnect []chan error
mu sync.RWMutex
logger *slog.Logger
}
func (c *Connection) Close() {
defer func() {
if r := recover(); r != nil {
c.logger.Warn(fmt.Sprintf("RabbitMQServer closeConn recovered: %v", r))
}
}()
currentlyReconnecting := !c.reconnecting.CompareAndSwap(false, true)
if currentlyReconnecting {
return
}
err := c.Connection.Close()
if err != nil {
c.logger.Debug(fmt.Sprintf("RabbitMQServer closeConn Close failed: %v", err))
}
}
func (c *Connection) NotifyClose(ch chan error) chan error {
c.mu.Lock()
defer c.mu.Unlock()
c.notifyClose = append(c.notifyClose, ch)
return ch
}
func (c *Connection) NotifyReconnect(ch chan error) chan error {
c.mu.Lock()
defer c.mu.Unlock()
c.notifyReconnect = append(c.notifyReconnect, ch)
return ch
}
func (c *Connection) IsAlive() bool {
return !c.reconnecting.Load()
}
func (c *Connection) watchReconnect(ctx context.Context) {
for {
errClose := <-c.Connection.NotifyClose(make(chan *amqp.Error, 1))
if errClose != nil {
go c.logger.Warn("RabbitMQServer ListenAndServe NotifyClose")
err := c.attemptReconnect(ctx)
if err != nil {
go c.logger.Warn(fmt.Sprintf("RabbitMQServer ListenAndServe attemptReconnecting failed: %v", err))
c.broadcastReconnect(err)
return
}
go c.logger.Info("RabbitMQServer ListenAndServe reconnected successfully")
c.broadcastReconnect(nil)
continue
}
c.broadcastClose(nil)
return
}
}
func (c *Connection) attemptReconnect(ctx context.Context) error {
backoff := backoff.NewDefaultSigmoidBackoff()
currentlyReconnecting := !c.reconnecting.CompareAndSwap(false, true)
if currentlyReconnecting {
return nil
}
for {
err := backoff.Retry(ctx)
if err != nil {
return errors.Join(err, errors.New("RabbitMQServer attemptReconnecting failed"))
}
c.Connection, err = c.connectionFactory()
if err != nil {
go c.logger.Info(fmt.Sprintf("RabbitMQServer attemptReconnecting failed: %s", err))
continue
}
c.reconnecting.Swap(false)
return nil
}
}
func (c *Connection) broadcastClose(err error) {
c.mu.Lock()
defer c.mu.Unlock()
channels := c.notifyClose
c.notifyClose = make([]chan error, 0)
c.notifyReconnect = make([]chan error, 0)
for _, ch := range channels {
ch <- err
close(ch)
}
}
func (c *Connection) broadcastReconnect(err error) {
c.mu.Lock()
defer c.mu.Unlock()
channels := c.notifyReconnect
c.notifyReconnect = make([]chan error, 0)
c.notifyClose = make([]chan error, 0)
for _, ch := range channels {
ch <- err
close(ch)
}
}