2025-02-17 21:04:13 +08:00
|
|
|
package rabbitmqamqp
|
2025-02-07 18:00:14 +08:00
|
|
|
|
|
|
|
|
import (
|
2025-06-18 20:46:55 +08:00
|
|
|
"errors"
|
2025-02-07 18:00:14 +08:00
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-18 20:46:55 +08:00
|
|
|
// ErrMaxReconnectAttemptsReached typed error when the MaxReconnectAttempts is reached
|
|
|
|
|
var ErrMaxReconnectAttemptsReached = errors.New("max reconnect attempts reached, connection will not be recovered")
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-05 16:46:28 +08:00
|
|
|
func (c *RecoveryConfiguration) Clone() *RecoveryConfiguration {
|
|
|
|
|
cloned := &RecoveryConfiguration{
|
|
|
|
|
ActiveRecovery: c.ActiveRecovery,
|
|
|
|
|
BackOffReconnectInterval: c.BackOffReconnectInterval,
|
|
|
|
|
MaxReconnectAttempts: c.MaxReconnectAttempts,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cloned
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-07 18:00:14 +08:00
|
|
|
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{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-27 20:58:59 +08:00
|
|
|
func (e *entitiesTracker) storeOrReplaceProducer(entity iEntityIdentifier) {
|
2025-02-07 18:00:14 +08:00
|
|
|
e.publishers.Store(entity.Id(), entity)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-27 20:58:59 +08:00
|
|
|
func (e *entitiesTracker) removeProducer(entity iEntityIdentifier) {
|
2025-02-07 18:00:14 +08:00
|
|
|
e.publishers.Delete(entity.Id())
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-27 20:58:59 +08:00
|
|
|
func (e *entitiesTracker) storeOrReplaceConsumer(entity iEntityIdentifier) {
|
2025-02-07 18:00:14 +08:00
|
|
|
e.consumers.Store(entity.Id(), entity)
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-27 20:58:59 +08:00
|
|
|
func (e *entitiesTracker) removeConsumer(entity iEntityIdentifier) {
|
2025-02-07 18:00:14 +08:00
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|