amqp-node / amqplib

AMQP 0-9-1 library and client for Node.JS

Home Page:https://amqp-node.github.io/amqplib/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

no error event on createChannel since V10.0.0

SSANSH opened this issue · comments

commented

Since the version V10.0.0 and the update of the code "Use Native promises (#689, thank you @mohd-akram and @kibertoad)"

// Do the remarkably simple channel open handshake async open() { const ch = await this.allocate.bind(this)(); return ch.rpc(defs.ChannelOpen, {outOfBand: ""}, defs.ChannelOpenOk); }

When I create channel like this currentChannel = await conn.createChannel();

and listen for event close

The error event is never emit in case "No channels left to allocate" using :

currentChannel.on('error', (error) => { ....

You can reproduce by comment line if (next < 0 || next > this.channelMax) into amqplib/lib/connection.js, calling createChannel and listen for error event.

Thanks.

Hi @SSANSH,

Something odd here. It does not make sense that createChannel would return a channel object with an event handler, if the channel could not be allocated. It also makes no sense that an error event would be emitted on a previously created and working channel, if a call to create a new channel failed.

The following code behaves as I would expect, and the same for me with amqplib v0.9.1 and v0.10.3 using node 18...

const amqplib = require('amqplib');

(async () => {
  const connection = await amqplib.connect('amqp://localhost:5672?channelMax=1');
  connection.on('error', (error) => {
    console.log('Connection Error', { error });
  })

  await createChannel();
  await createChannel();

  async function createChannel()  {
    try {
      const channel = await connection.createChannel();
      channel.on('close', (error) => {
        console.log('Channel Close Event', { error });
      });
      channel.on('error', (error) => {
        console.log('Channel Error Event', { error });
      })
      console.log('Channel Created OK')
    } catch (error) {
      console.log('Caught Error', { error });
    }
  }

})();
Channel Created OK
Caught Error {
  error: Error: No channels left to allocate
      at C.freshChannel (/Users/steve/Development/amqp-node/amqplib-726/node_modules/amqplib/lib/connection.js:459:11)
      at C.allocate (/Users/steve/Development/amqp-node/amqplib-726/node_modules/amqplib/lib/channel.js:52:29)
      at tryCatcher (/Users/steve/Development/amqp-node/amqplib-726/node_modules/bluebird/js/release/util.js:16:23)
      at Promise.attempt.Promise.try (/Users/steve/Development/amqp-node/amqplib-726/node_modules/bluebird/js/release/method.js:39:29)
      at Channel.open (/Users/steve/Development/amqp-node/amqplib-726/node_modules/amqplib/lib/channel_model.js:65:23)
      at ChannelModel.createChannel (/Users/steve/Development/amqp-node/amqplib-726/node_modules/amqplib/lib/channel_model.js:31:19)
      at createChannel (/Users/steve/Development/amqp-node/amqplib-726/index.js:15:38)
      at /Users/steve/Development/amqp-node/amqplib-726/index.js:11:9
}