Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes amqp.node to get ECONNRESET from a RabbitMQ server?

I have an instance of RabbitMQ (default configurations) running on Windows 8 environment. My node version is 5.1.0 and I am trying to establish a connection between them using amqp.node library in order to pass messages.

When I try the sample code:

var q = 'tasks';

function bail(err) {
  console.error(err);
  process.exit(1);
}

// Publisher
function publisher(conn) {
  conn.createChannel(on_open);
  function on_open(err, ch) {
    if (err != null) bail(err);
    ch.assertQueue(q);
    ch.sendToQueue(q, new Buffer('something to do'));
  }
}

// Consumer
function consumer(conn) {
  var ok = conn.createChannel(on_open);
  function on_open(err, ch) {
    if (err != null) bail(err);
    ch.assertQueue(q);
    ch.consume(q, function(msg) {
      if (msg !== null) {
        console.log(msg.content.toString());
        ch.ack(msg);
      }
    });
  }
}

require('amqplib/callback_api')
  .connect('amqp://guest:guest@localhost:5672', function(err, conn) {
    if (err != null) bail(err);
    consumer(conn);
    publisher(conn);        
  });

I get it working okay.

But if I move this code, exactly how it is, into my project, I get this error:

ECCONRESET syscall: read.

My app runs with express.js and oauth2.0. Even if I put the code right before any other statements and modules' requires, it does not work.

I searched about this error and I found some problems related to load balance, but I am running it locally and the sample code works okay.

Another problem that I found could be related to TCP connection, I changed the handshake timout option inside RabbitMQ server's config file to 10000ms, but nothing changed.

I am using the same url with guest user: amqp://guest:guest@localhost:5672, which works on the sample code.

The log from RabbitMQ shows that a connection is done but a few seconds later, it states that the connection was closed unexpectedly:

=INFO REPORT==== 24-Nov-2015::18:09:52 ===
accepting AMQP connection <0.2644.0> (127.0.0.1:51866 -> 127.0.0.1:5672)

=ERROR REPORT==== 24-Nov-2015::18:10:12 ===
closing AMQP connection <0.2644.0> (127.0.0.1:51866 -> 127.0.0.1:5672):
{handshake_timeout,frame_header}

So my questions are: is there any conflict between amqp.node and other library that drops the connection with the server? How can I debug this?

like image 846
bpinhosilva Avatar asked Jan 07 '23 03:01

bpinhosilva


1 Answers

This error is related to connection and channels. So in your code you are creating a connection and channel but not closing them. That creates this error (ECONNRESET) when the no. of connection and channel limit is exhausted RabbitMQ will stop accepting new network connections. Closing the channel and connection will solve this error. Example Code:

amqp.connect('amqp://localhost')
.then(function(conn) {
    return when(conn.createChannel().then(function(ch) {
        var q = 'hello';
        var msg = 'Hello World!';

        var ok = ch.assertQueue(q, {durable: true});

        return ok.then(function(_qok) {
            ch.sendToQueue(q, new Buffer(msg), {deliveryMode: true});
            console.log(" [x] Sent '%s'", msg);
            return ch.close();
        });
    })).ensure(function() {
        conn.close();
    });
})
.then(null, console.warn);
like image 200
Sukhjeet Singh Mangat Avatar answered Mar 02 '23 01:03

Sukhjeet Singh Mangat