amqplib icon indicating copy to clipboard operation
amqplib copied to clipboard

Connection recovery helper.

Open hairyhum opened this issue 7 years ago • 9 comments

The helper function to simplify connection recovery via event emitter.

This function uses a callback, which can be called multiple times, hence cannot be promisified.

TODO:

  • [x] Multiple hosts support
  • [x] Documentation PR

hairyhum avatar Jun 19 '18 16:06 hairyhum

@squaremo this is a simpler approach to connection recovery. This approach provides a developer with examples and some helper functions to gracefully handle connection recovery without changing much code and keeping the existing objects around.

First commit allows to create consume callbacks without a channel bound in them, which can be used to re-subscribe.

Last commit fixes a bug, when error callback can be called twice. By chance it was discovered in the tests for recovery, but can be reproduced with just a simple testcase like this, which should run after connect.js tests:

test("dummy", function(done) {
    this.timeout(15000);
    setTimeout(function(){
        done(null);
    }, 5000);
});

hairyhum avatar Jun 20 '18 15:06 hairyhum

Daniil, this looks like an elegant solution to connection recovery. I will try to have a thorough look through it in the next few days.

First commit allows to create consume callbacks without a channel bound in them, which can be used to re-subscribe.

I don't quite get this -- is the idea that if you install a consumer callback that closes over a ch variable, if you reinstall that consumer with a fresh channel, it will still call the old channel?

squaremo avatar Jun 21 '18 06:06 squaremo

The callback will be called with the actual channel in the arguments, the channel which is processing the consumed message. This will make consume function reusable because the channel argument will be the active channel.

function process_msg(msg, ch){
    ...
    ch.ack(msg);
}

my_channel.consume(queue, process_msg);

another_channel.consume(queue, process_msg);

The benefit here is that you can write a consume function separately and reuse it. It's not necessary required for connection recovery, but makes some code much easier to write. I can make a separate PR with that change, if you like.

hairyhum avatar Jun 21 '18 09:06 hairyhum

This will make consume function reusable because the channel argument will be the active channel.

Ah I see -- it's not critical to the recovery, just a convenience. You can also achieve this with bind, like

function process_msg(ch, msg) {
 // ...
  ch.ack(msg);
}

my_channel.consume(queue, process_msg.bind(my_queue));
another_queue.consume(queue, process_msg.bind(another_queue));

I am not up with the JavaScript fashions enough to know which (if either) is considered more idiomatic.

squaremo avatar Jul 09 '18 22:07 squaremo

Hi, what is the status of this PR?

usebaz avatar Jul 28 '18 09:07 usebaz

@squaremo can you please update if you need any changes to that?

hairyhum avatar Jul 31 '18 16:07 hairyhum

Hi, sorry, I wanted to fix the problems with NodeJS 9 and 10 (two different problems, gah) before getting back to this -- at least so we can have passing CI. That's taken longer in elapsed time than I hoped. I will try to take a look here again soon, so you at least have some feedback.

On Tue, 31 Jul 2018, 17:32 Daniil Fedotov, [email protected] wrote:

@squaremo https://github.com/squaremo can you please update if you need any changes to that?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/squaremo/amqp.node/pull/432#issuecomment-409284551, or mute the thread https://github.com/notifications/unsubscribe-auth/AADyUf6byGx-LXTupfj3E2i2UhWM9y6Oks5uMIaUgaJpZM4Ut2yI .

squaremo avatar Aug 01 '18 21:08 squaremo

@hairyhum this looks slightly abandoned :). is the code here still up-to-date, or obsolete/incompatible by now?

hardcodet avatar Jul 23 '19 15:07 hardcodet

@hardcodet I didn't update this code and don't know if it's still up-to-date with the rest of the project. In theory it should be relatively easily rebasable.

hairyhum avatar Aug 01 '19 14:08 hairyhum

Tidy up

cressie176 avatar Apr 11 '24 18:04 cressie176