postwait / node-amqp

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

Adds support for detecting when the write socket is full and cannot safely buffer more data #253

Closed zuk closed 8 years ago

zuk commented 10 years ago

This implements a potential mechanism for preventing a serious memory overflow issue in the way Connection sends data to the amqp server. The problem in the current implementation is that if data is written (published) to the underlying socket faster than the socket can push it out to the amqp server, the additional data gets buffered into memory. This buffer can grow indefinitely. (See #57)

This patch exposes the underlying socket's buffer status, so that a decision can be made upstream on whether to keep pushing data despite the overflow.

When the socket is full, the Connection's isBackedUp property is set to true. When this happens, the client should stop sending data or start buffering it in some safe way. For example:

if (queue.connection.isBackedUp) {
  // can't safely send message without growing memory queue
} else {
  conn.publish("some-queue", message);
}

isBackedUp is automatically set to false when the socket becomes safely writable again.

Additionally, a writeBufferSize option (number of bytes) can be provided to the Connection constructor to set the highWaterMark for the underlying socket. This should effectively set the maximum buffer size.