Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not block while waiting for previous message to be confirmed #84

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/ChannelWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ export default class ChannelWrapper extends EventEmitter {
// Place to store queued messages.
this._messages = [];

// Place to store published, but not yet confirmed messages
this._unconfirmedMessages = [];

// True if the "worker" is busy sending messages. False if we need to
// start the worker to get stuff done.
this._working = false;
Expand Down Expand Up @@ -231,6 +234,12 @@ export default class ChannelWrapper extends EventEmitter {
// Can happen if channel closes while we're setting up.
return;
}
if (this._unconfirmedMessages.length > 0) {
// requeu any messages that were left unconfirmed when connection was lost
while (this._unconfirmedMessages.length) {
this._messages.push(this._unconfirmedMessages.shift());
}
}

// Since we just connected, publish any queued messages
this._startWorker();
Expand Down Expand Up @@ -278,6 +287,10 @@ export default class ChannelWrapper extends EventEmitter {
// Reject any unsent messages.
this._messages.forEach(message => message.reject(new Error('Channel closed')));
}
if(this._unconfirmedMessages.length !== 0) {
// Reject any unconfirmed messages.
this._unconfirmedMessages.forEach(message => message.reject(new Error('Channel closed')));
}

this._connectionManager.removeListener('connect', this._onConnect);
this._connectionManager.removeListener('disconnect', this._onDisconnect);
Expand Down Expand Up @@ -311,7 +324,8 @@ export default class ChannelWrapper extends EventEmitter {
}

const channel = this._channel;
const message = this._messages[0];
const message = this._messages.shift();
this._unconfirmedMessages.push(message);

Promise.resolve()
.then(() => {
Expand Down Expand Up @@ -348,29 +362,27 @@ export default class ChannelWrapper extends EventEmitter {
}
})();

// Send some more!
this._publishQueuedMessages(workerNumber);

return sendPromise;
})
.then(
result => {
this._messages.shift();
this._unconfirmedMessages.shift();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes messages will be confirmed in the same order they were added to _unconfirmedMessages, but that sounds reasonable.

message.resolve(result);

// Send some more!
this._publishQueuedMessages(workerNumber);
},

err => {
if(!this._channel) {
// Tried to write to a closed channel. Leave the message in the queue and we'll try again when we
// reconnect.
this._messages.unshift(this._unconfirmedMessages.shift());
} else {
// Something went wrong trying to send this message - could be JSON.stringify failed, could be the
// broker rejected the message. Either way, reject it back
this._messages.shift();
this._unconfirmedMessages.shift();
message.reject(err);

// Send some more!
this._publishQueuedMessages(workerNumber);
}
}
)
Expand Down
25 changes: 24 additions & 1 deletion test/ChannelWrapperTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,27 @@ describe('ChannelWrapper', function() {
return p1;
}).then(() => expect(publishCalls).to.equal(2));
});
});

it('should publish enqued messages to the underlying channel without waiting for confirms', function() {
connectionManager.simulateConnect();
let p1, p2;
const channelWrapper = new ChannelWrapper(connectionManager, {
setup(channel) {
channel.publish = sinon.stub();
return Promise.resolve();
}
});

return channelWrapper.waitForConnect()
.then(() => {
p1 = channelWrapper.publish('exchange', 'routingKey', 'msg:1');
p2 = channelWrapper.publish('exchange', 'routingKey', 'msg:2');
return promiseTools.delay(10);
}).then(() => {
const channel = channelWrapper._channel;
expect(channel.publish.calledTwice).to.be.true;
expect(p1).to.not.be.fulfilled;
expect(p2).to.not.be.fulfilled;
});
});
});