@@ -129,42 +129,35 @@ func (r *rabbitMQConn) reconnect(secure bool, config *amqp.Config) {
129
129
chanNotifyClose := make (chan * amqp.Error )
130
130
channel := r .ExchangeChannel .channel
131
131
channel .NotifyClose (chanNotifyClose )
132
- channelNotifyReturn := make (chan amqp.Return )
133
- channel .NotifyReturn (channelNotifyReturn )
134
-
135
- // block until closed
136
- select {
137
- case result , ok := <- channelNotifyReturn :
138
- if ! ok {
139
- // Channel closed, probably also the channel or connection.
132
+ // To avoid deadlocks it is necessary to consume the messages from all channels.
133
+ for notifyClose != nil || chanNotifyClose != nil {
134
+ // block until closed
135
+ select {
136
+ case err := <- chanNotifyClose :
137
+ if logger .V (logger .ErrorLevel , logger .DefaultLogger ) {
138
+ logger .Error (err )
139
+ }
140
+ // block all resubscribe attempt - they are useless because there is no connection to rabbitmq
141
+ // create channel 'waitConnection' (at this point channel is nil or closed, create it without unnecessary checks)
142
+ r .Lock ()
143
+ r .connected = false
144
+ r .waitConnection = make (chan struct {})
145
+ r .Unlock ()
146
+ chanNotifyClose = nil
147
+ case err := <- notifyClose :
148
+ if logger .V (logger .ErrorLevel , logger .DefaultLogger ) {
149
+ logger .Error (err )
150
+ }
151
+ // block all resubscribe attempt - they are useless because there is no connection to rabbitmq
152
+ // create channel 'waitConnection' (at this point channel is nil or closed, create it without unnecessary checks)
153
+ r .Lock ()
154
+ r .connected = false
155
+ r .waitConnection = make (chan struct {})
156
+ r .Unlock ()
157
+ notifyClose = nil
158
+ case <- r .close :
140
159
return
141
160
}
142
- // Do what you need with messageFailing.
143
- if logger .V (logger .ErrorLevel , logger .DefaultLogger ) {
144
- logger .Errorf ("notify error reason: %s, description: %s" , result .ReplyText , result .Exchange )
145
- }
146
- case err := <- chanNotifyClose :
147
- if logger .V (logger .ErrorLevel , logger .DefaultLogger ) {
148
- logger .Error (err )
149
- }
150
- // block all resubscribe attempt - they are useless because there is no connection to rabbitmq
151
- // create channel 'waitConnection' (at this point channel is nil or closed, create it without unnecessary checks)
152
- r .Lock ()
153
- r .connected = false
154
- r .waitConnection = make (chan struct {})
155
- r .Unlock ()
156
- case err := <- notifyClose :
157
- if logger .V (logger .ErrorLevel , logger .DefaultLogger ) {
158
- logger .Error (err )
159
- }
160
- // block all resubscribe attempt - they are useless because there is no connection to rabbitmq
161
- // create channel 'waitConnection' (at this point channel is nil or closed, create it without unnecessary checks)
162
- r .Lock ()
163
- r .connected = false
164
- r .waitConnection = make (chan struct {})
165
- r .Unlock ()
166
- case <- r .close :
167
- return
168
161
}
169
162
}
170
163
}
0 commit comments