abreits / amqp-ts

AmqpSimple, a RabbitMQ tested Amqp library written in and for Typescript
MIT License
133 stars 37 forks source link

Missing reconnect and 'close' event #27

Closed mrjensen closed 5 years ago

mrjensen commented 6 years ago

Hi and thank you for the project 👍

I was testing some error scenarios and found unexpected behaviour when using the Connection class' reconnectStrategy.

  1. reconnect doesn't happen if the connection is closed due to "CONNECTION-FORCED"
  2. after a successful reconnect, the 'close' callback isn't invoked

amqp-ts seems to handle closing connections differently in scenario 1 and 2. Scenario 1 works as expected while scenario 2 doesn't reconnect, but leaves consumers hanging as if nothing happened.

By hooking up the 'close' callback, I see that only Scenario 1 results in a isFatalError=true from the dependency amqplib. The 'close' callback also allows me to detect that Scenario 2 doesn't reconnect and results in isFatalError=false.

If Scenario 1 happens, the 'close' callback is only invoked on the first 'close' event. Since I have to handle Scenario 2 manually this unfortunately renders the reconnect feature unuseful for me, since I can't handle the manual reconnects if a successful reconnect has already happened.

Scenario 1: reconnects as expected ✅

  1. amqp-ts connects (successfully) to a rabbit-instance in a 3-node cluster
  2. terminate the rabbitmq instance the client connected to (with kill -9 <PID>)
  3. amqp-ts reconnects
  4. (repeat step 2 and 3) Setting up a callback on 'close' shows that amqplib's isFatalError returns true.

Scenario 2: reconnect doesn't happen on CONNECTION-FORCED ❌

  1. amqp-ts connects (successfully) to a rabbit-instance in a 3-node cluster
  2. restart the rabbitmq service (Ubuntu) the client connected to (with sudo systemctl restart rabbitmq-server.service)
  3. no reconnect
  4. 'close' callback is invoked and consumer is hanging without consuming events Setting up a callback on 'close' shows that amqplib's isFatalError returns false and error: "Connection closed: 320 (CONNECTION-FORCED) with message "CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'".

Scenario 3: 'close' callback isn't invoked after succesfull reconnect (scenario 1) ❌

  1. amqp-ts connects (successfully) to a rabbit-instance in a 3-node cluster
  2. terminate the rabbitmq instance the client connected to (with kill -9 <PID>)
  3. amqp-ts reconnects
  4. restart the rabbitmq service (Ubuntu) the client connected to (with sudo systemctl restart rabbitmq-server.service)
  5. 'close' callback is NOT invoked and consumer is hanging without consuming events