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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wait for 'drain' as well #1636

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 21 additions & 4 deletions src/publisher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,30 @@ export class Publisher {
flush(callback?: EmptyCallback): Promise<void> | void {
const definedCallback = callback ? callback : () => {};

const publishes = [promisify(this.queue.publish).bind(this.queue)()];
Array.from(this.orderedQueues.values()).forEach(q =>
publishes.push(promisify(q.publish).bind(q)())
const toDrain = [this.queue, ...Array.from(this.orderedQueues.values())];

const allDrains = Promise.all(
toDrain.map(
q =>
new Promise<void>(resolve => {
const flushResolver = () => {
resolve();

// flush() maybe called more than once, so remove these
// event listeners after we've completed flush().
q.removeListener('drain', flushResolver);
};
return q.on('drain', flushResolver);
})
)
);

const allPublishes = Promise.all(
toDrain.map(q => promisify(q.publish).bind(q)())
);
const allPublishes = Promise.all(publishes);

allPublishes
.then(() => allDrains)
.then(() => {
definedCallback(null);
})
Expand Down
15 changes: 14 additions & 1 deletion src/publisher/message-queues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,11 @@ export class Queue extends MessageQueue {
}
/**
* Cancels any pending publishes and calls _publish immediately.
*
* @emits Queue#drain when all messages are sent.
*/
publish(callback?: PublishDone): void {
const definedCallback = callback || (() => {});
const {messages, callbacks} = this.batch;

this.batch = new MessageBatch(this.batchOptions);
Expand All @@ -169,7 +172,17 @@ export class Queue extends MessageQueue {
delete this.pending;
}

this._publish(messages, callbacks, callback);
this._publish(messages, callbacks, (err: null | ServiceError) => {
if (err) {
definedCallback(err);
} else if (this.batch.messages.length) {
// Make another go-around, we're trying to drain the queues fully.
this.publish(callback);
} else {
this.emit('drain');
definedCallback(null);
}
});
}
}

Expand Down
17 changes: 16 additions & 1 deletion test/publisher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ describe('Publisher', () => {
sandbox
.stub(FakeQueue.prototype, '_publish')
.callsFake((messages, callbacks, callback) => {
// Simulate the drain taking longer than the publishes. This can
// happen if more messages are queued during the publish().
setTimeout(() => {
publisher.queue.emit('drain');
}, 50);
Copy link
Contributor

Choose a reason for hiding this comment

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

This will fail eventually, I can bet :) Maybe use longer timeout?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This just needs to be on the next tick, really, so I could just change this to nextTick().

if (typeof callback === 'function') callback(null);
});

Expand All @@ -356,7 +361,12 @@ describe('Publisher', () => {
const queue = publisher.orderedQueues.get(
orderingKey
) as unknown as FakeOrderedQueue;
queue.emit('drain');
// Simulate the drain taking longer than the publishes. This can
// happen on some ordered queue scenarios, especially if we have more
// than one queue to empty.
setTimeout(() => {
queue.emit('drain');
}, 50);
if (typeof callback === 'function') callback(null);
});

Expand Down Expand Up @@ -495,6 +505,11 @@ describe('Publisher', () => {
sandbox
.stub(publisher.queue, '_publish')
.callsFake((messages, callbacks, callback) => {
// Simulate the drain taking longer than the publishes. This can
// happen if more messages are queued during the publish().
setTimeout(() => {
publisher.queue.emit('drain');
}, 50);
if (typeof callback === 'function') callback(null);
});

Expand Down