postwait/node-amqp — pull request #253 by zuk (2013-10-30 23:24:00+00:00)
Title: Adds support for detecting when the write socket is full and cannot safely buffer more data

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.

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.