postwait / node-amqp

[UNMAINTAINED] node-amqp is an AMQP client for nodejs
MIT License
1.69k stars 357 forks source link

Cleanly disconnect from the server with disconnect() #295

Closed glenjamin closed 10 years ago

glenjamin commented 10 years ago

Sends connection.close, and waits for a connection.close-ok before closing the socket.

connection.end() will still work for backwards compatibility by closing the underlying socket, but is removed from the documentation.

I'm unsure whether I should have done more work around making sure to blackhole any messages received after sending the connection.close to properly follow the spec[1], or to shut down the object state of the channels and things.

As this is only new behaviour it should be perfectly safe to add regardless.

[1] https://www.rabbitmq.com/amqp-0-9-1-reference.html#connection.close

cmoesel commented 10 years ago

My pull request #248 also addresses the clean disconnect problem, but I chose to integrate it into connection.end, rather than create a new function. I do like that your pull request allows previous functionality to remain exactly the same though.

In my testing I discovered that it is important to ignore any received messages (except connectionClose and connectionCloseOk) after you've sent the connection close message, otherwise some errors can occur. So, it's not just a matter of following the spec, but does actually affect real behavior and stability. You can see my pull request for an example of how I chose to do this.

Also-- a word of advice-- the maintainers of this library are unlikely to merge your request if it does not contain any unit tests, so you can improve your chances by adding tests as well. That said, they haven't accepted my pull requests either, so who am I to be giving advice? ;-)

glenjamin commented 10 years ago

Ah, I had a go at finding an existing PR but must have missed that one.

I'll probably flesh this out like you suggest so both options are available to merge - cheers for the feedback.