streadway / amqp

Go client for AMQP 0.9.1
http://godoc.org/github.com/streadway/amqp
BSD 2-Clause "Simplified" License
4.88k stars 621 forks source link

Reconnect only if the Management Plugin didn't close the connection #466

Closed bentcoder closed 4 years ago

bentcoder commented 4 years ago

Hi,

Currently we are unable to handle *amqp.Error correctly for closed connections. What I mean is, I wish to utilise conn.NotifyClose feature to reconnect which is a straightforward process. However, I want to skip reconnecting only if the connection was closed via the Management Plugin (UI). The problem is, error code for this action is 320 (ConnectionForced) which is same as docker stop, docker-compose down and rabbitmqctl stop. As you can see I cannot use if err.Code == 320 because it covers everything else. I don't want to take err.Reason into account because it won't be reliable resource. What is the best solution for this?

Thanks

err := <-amqp.connection.NotifyClose(make(chan *amqp.Error))
if err != nil {
    if ??????? {
        // Definitely NOT closed by Management Plugin (UI) so reconnect
    }
}
michaelklishin commented 4 years ago

connection.closeframes sent by the RabbitMQ node will contain a reason. As far as the server is concerned it has to force close client connections when the node is shut down as well as other cases. There are no separate error codes for each scenario in the protocol. Some other clients special case server initiated connection closure events but the user has to decide whether the library should recover from them.