-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathamqp_connection_recovery.go
More file actions
268 lines (235 loc) · 6.91 KB
/
amqp_connection_recovery.go
File metadata and controls
268 lines (235 loc) · 6.91 KB
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
package rabbitmqamqp
import (
"errors"
"slices"
"sync"
"time"
)
// ErrMaxReconnectAttemptsReached typed error when the MaxReconnectAttempts is reached
var ErrMaxReconnectAttemptsReached = errors.New("max reconnect attempts reached, connection will not be recovered")
type RecoveryConfiguration struct {
/*
ActiveRecovery Define if the recovery is activated.
If is not activated the connection will not try to createSender.
*/
ActiveRecovery bool
/*
BackOffReconnectInterval The time to wait before trying to createSender after a connection is closed.
time will be increased exponentially with each attempt.
Default is 5 seconds, each attempt will double the time.
The minimum value is 1 second. Avoid setting a value low values since it can cause a high
number of reconnection attempts.
*/
BackOffReconnectInterval time.Duration
/*
MaxReconnectAttempts The maximum number of reconnection attempts.
Default is 5.
The minimum value is 1.
*/
MaxReconnectAttempts int
}
func (c *RecoveryConfiguration) Clone() *RecoveryConfiguration {
cloned := &RecoveryConfiguration{
ActiveRecovery: c.ActiveRecovery,
BackOffReconnectInterval: c.BackOffReconnectInterval,
MaxReconnectAttempts: c.MaxReconnectAttempts,
}
return cloned
}
func NewRecoveryConfiguration() *RecoveryConfiguration {
return &RecoveryConfiguration{
ActiveRecovery: true,
BackOffReconnectInterval: 5 * time.Second,
MaxReconnectAttempts: 5,
}
}
type entitiesTracker struct {
publishers sync.Map
consumers sync.Map
}
func newEntitiesTracker() *entitiesTracker {
return &entitiesTracker{
publishers: sync.Map{},
consumers: sync.Map{},
}
}
func (e *entitiesTracker) storeOrReplaceProducer(entity iEntityIdentifier) {
e.publishers.Store(entity.Id(), entity)
}
func (e *entitiesTracker) removeProducer(entity iEntityIdentifier) {
e.publishers.Delete(entity.Id())
}
func (e *entitiesTracker) storeOrReplaceConsumer(entity iEntityIdentifier) {
e.consumers.Store(entity.Id(), entity)
}
func (e *entitiesTracker) removeConsumer(entity iEntityIdentifier) {
e.consumers.Delete(entity.Id())
}
func (e *entitiesTracker) CleanUp() {
e.publishers.Range(func(key, value interface{}) bool {
e.publishers.Delete(key)
return true
})
e.consumers.Range(func(key, value interface{}) bool {
e.consumers.Delete(key)
return true
})
}
type queueRecoveryRecord struct {
queueName string
queueType TQueueType
autoDelete *bool
exclusive *bool
arguments map[string]any
}
func (q *queueRecoveryRecord) toIQueueSpecification() IQueueSpecification {
switch q.queueType {
case Quorum:
return &QuorumQueueSpecification{
Name: q.queueName,
Arguments: q.arguments,
}
case Classic:
return &ClassicQueueSpecification{
Name: q.queueName,
IsAutoDelete: *q.autoDelete,
IsExclusive: *q.exclusive,
Arguments: q.arguments,
}
case Stream:
return &StreamQueueSpecification{
Name: q.queueName,
Arguments: q.arguments,
}
case Jms:
return &JMSQueueSpecification{
Name: q.queueName,
Arguments: q.arguments,
}
case Delayed:
return &DelayedQueueSpecification{
Name: q.queueName,
IsAutoDelete: *q.autoDelete,
IsExclusive: *q.exclusive,
Arguments: q.arguments,
}
default:
return &DefaultQueueSpecification{
Name: q.queueName,
IsAutoDelete: *q.autoDelete,
IsExclusive: *q.exclusive,
Arguments: q.arguments,
}
}
}
type exchangeRecoveryRecord struct {
exchangeName string
exchangeType TExchangeType
autoDelete bool
arguments map[string]any
}
func (e *exchangeRecoveryRecord) toIExchangeSpecification() IExchangeSpecification {
switch e.exchangeType {
case Direct:
return &DirectExchangeSpecification{
Name: e.exchangeName,
IsAutoDelete: e.autoDelete,
Arguments: e.arguments,
}
case Topic:
return &TopicExchangeSpecification{
Name: e.exchangeName,
IsAutoDelete: e.autoDelete,
Arguments: e.arguments,
}
case FanOut:
return &FanOutExchangeSpecification{
Name: e.exchangeName,
IsAutoDelete: e.autoDelete,
Arguments: e.arguments,
}
case Headers:
return &HeadersExchangeSpecification{
Name: e.exchangeName,
IsAutoDelete: e.autoDelete,
Arguments: e.arguments,
}
default:
return &CustomExchangeSpecification{
Name: e.exchangeName,
IsAutoDelete: e.autoDelete,
ExchangeTypeName: string(e.exchangeType),
Arguments: e.arguments,
}
}
}
type bindingRecoveryRecord struct {
sourceExchange string
destination string
isDestinationQueue bool
bindingKey string
arguments map[string]any
path string
}
func (b *bindingRecoveryRecord) toIBindingSpecification() IBindingSpecification {
if b.isDestinationQueue {
return &ExchangeToQueueBindingSpecification{
SourceExchange: b.sourceExchange,
DestinationQueue: b.destination,
BindingKey: b.bindingKey,
Arguments: b.arguments,
}
}
return &ExchangeToExchangeBindingSpecification{
SourceExchange: b.sourceExchange,
DestinationExchange: b.destination,
BindingKey: b.bindingKey,
Arguments: b.arguments,
}
}
type topologyRecoveryRecords struct {
queues []queueRecoveryRecord
exchanges []exchangeRecoveryRecord
bindings []bindingRecoveryRecord
}
func newTopologyRecoveryRecords() *topologyRecoveryRecords {
return &topologyRecoveryRecords{
queues: make([]queueRecoveryRecord, 0),
exchanges: make([]exchangeRecoveryRecord, 0),
bindings: make([]bindingRecoveryRecord, 0),
}
}
func (t *topologyRecoveryRecords) addQueueRecord(record queueRecoveryRecord) {
t.queues = append(t.queues, record)
}
func (t *topologyRecoveryRecords) removeQueueRecord(queueName string) {
t.queues = slices.DeleteFunc(t.queues, func(r queueRecoveryRecord) bool {
return r.queueName == queueName
})
}
func (t *topologyRecoveryRecords) addExchangeRecord(record exchangeRecoveryRecord) {
t.exchanges = append(t.exchanges, record)
}
func (t *topologyRecoveryRecords) removeExchangeRecord(exchangeName string) {
t.exchanges = slices.DeleteFunc(t.exchanges, func(r exchangeRecoveryRecord) bool {
return r.exchangeName == exchangeName
})
}
func (t *topologyRecoveryRecords) addBindingRecord(record bindingRecoveryRecord) {
t.bindings = append(t.bindings, record)
}
func (t *topologyRecoveryRecords) removeBindingRecord(bindingPath string) {
t.bindings = slices.DeleteFunc(t.bindings, func(r bindingRecoveryRecord) bool {
return r.path == bindingPath
})
}
func (t *topologyRecoveryRecords) removeBindingRecordBySourceExchange(sourceExchange string) {
t.bindings = slices.DeleteFunc(t.bindings, func(r bindingRecoveryRecord) bool {
return r.sourceExchange == sourceExchange
})
}
func (t *topologyRecoveryRecords) removeBindingRecordByDestinationQueue(destinationQueue string) {
t.bindings = slices.DeleteFunc(t.bindings, func(r bindingRecoveryRecord) bool {
return r.destination == destinationQueue
})
}