Skip to content

feat: auto-ack in AMQPQueue.subscribe()#189

Merged
baelter merged 4 commits intomainfrom
feat/auto-ack
Mar 5, 2026
Merged

feat: auto-ack in AMQPQueue.subscribe()#189
baelter merged 4 commits intomainfrom
feat/auto-ack

Conversation

@baelter
Copy link
Copy Markdown
Member

@baelter baelter commented Mar 4, 2026

Summary

  • Callback form (subscribe(callback) / subscribe(params, callback)): wraps the callback to msg.ack() on return and msg.nack(requeue) on throw. Pass { noAck: true } to opt out; requeueOnNack: false to discard on error instead of requeuing.
  • Iterator form (subscribe() / subscribe(params?)): acks the previous message when the loop advances to the next. The last message (after break) is left unacked. Call msg.ack() / msg.nack() before advancing to override; pass { noAck: true } to opt out.
  • AMQPMessage.ack/nack/reject are now idempotent — calling them more than once sends only one wire frame, so the library and user code can both ack without coordinating.
  • AMQPConsumer.cancel() is idempotent via a memoised promise, preventing a double-basicCancel race when generator cleanup and sub.cancel() overlap.

Test plan

  • Callback: acks after callback returns
  • Callback: nacks when callback throws (requeueOnNack: false to avoid requeue loop)
  • Callback: no-op when noAck: true
  • Callback: no double-ack if callback already called msg.ack()
  • Iterator: acks previous message when loop advances
  • Iterator: no-op when noAck: true
  • Iterator: no double-ack if loop body already called msg.ack()

Comment thread src/amqp-queue.ts Outdated
ThomasSarlin

This comment was marked as outdated.

Comment thread src/amqp-queue.ts Outdated
@baelter baelter force-pushed the feat/auto-ack branch 2 times, most recently from b5dcce0 to 95644bc Compare March 4, 2026 15:18
@baelter baelter marked this pull request as draft March 4, 2026 15:22
@baelter baelter force-pushed the feat/auto-ack branch 3 times, most recently from 7c02987 to ede9bcd Compare March 4, 2026 16:05
@baelter baelter changed the title feat: add autoAck option to AMQPQueue.subscribe() feat: auto-ack in AMQPQueue.subscribe() Mar 4, 2026
Callback form: wraps the user callback to ack on return and nack+requeue on
throw. Pass `{ noAck: true }` to opt out; `requeueOnNack: false` to discard
on error instead of requeuing.

Iterator form: acks the previous message when the loop advances to the next.
The last message (after `break`) is left unacked. Call `msg.ack()` /
`msg.nack()` before advancing to override.

AMQPMessage.ack/nack/reject are now idempotent — calling them more than once
is safe and sends only one wire frame. This lets callback and library both
ack without coordinating.

AMQPConsumer.cancel() is also idempotent via a memoised promise, preventing
a double-basicCancel race when generator cleanup and sub.cancel() overlap.
@baelter baelter marked this pull request as ready for review March 5, 2026 07:18
@baelter
Copy link
Copy Markdown
Member Author

baelter commented Mar 5, 2026

I think you should start doing drafts until it is ready for reviews

Indeed :) Ready now I think.

@baelter baelter requested a review from ThomasSarlin March 5, 2026 08:54
Comment thread src/amqp-message.ts
messageCount?: number
replyCode?: number
replyText?: string
#acked = false
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is the variable prefixed with #?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's a javascript feature: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_elements
It's stricter than TypeScript's private keyword, which is only a compile-time check. #foo is enforced at runtime by the JS engine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why did we decide on making use of it here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No particular reason. We should probably start using it more.

Comment thread src/amqp-queue.ts Outdated
@baelter baelter requested a review from antondalgren March 5, 2026 10:00
@baelter baelter merged commit 4e1d73c into main Mar 5, 2026
6 checks passed
@baelter baelter deleted the feat/auto-ack branch March 5, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants