-
Notifications
You must be signed in to change notification settings - Fork 9
wip: single active consumer offset tracking #247
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
Merged
l4mby
merged 6 commits into
main
from
feat/239-feature-request-automatic-offset-refresh-on-leadership-claim-in-sac-mode
May 5, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
791d71f
wip: single active consumer offset tracking
5728d8b
feat: add callback to customize single active consumer beahviour duri…
5dac78b
chore: update jsdoc on consumer interface
b6f173b
chore: fix example
fad8daa
chore: add error log and update readme
8c038a1
chore: update stream name in example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| const rabbit = require("rabbitmq-stream-js-client") | ||
| const crypto = require("crypto") | ||
|
|
||
| const wait = (ms) => new Promise((r) => setTimeout(r, ms)) | ||
|
|
||
| async function main() { | ||
| const messagesFromFirstConsumer = [] | ||
| const messagesFromSecondConsumer = [] | ||
|
|
||
| console.log("Connecting...") | ||
| const client = await rabbit.connect({ | ||
| vhost: "/", | ||
| port: 5552, | ||
| hostname: "localhost", | ||
| username: "rabbit", | ||
| password: "rabbit", | ||
| }) | ||
|
|
||
| console.log("Making sure the stream exists...") | ||
| const streamName = "active-consumer-switch-on-single-active-consumer" | ||
| await client.createStream({ stream: streamName, arguments: {} }) | ||
| const consumerRef = `my-consumer-${crypto.randomUUID()}` | ||
|
|
||
| console.log("Creating the publisher and sending 100 messages...") | ||
| const publisher = await client.declarePublisher({ stream: streamName }) | ||
| for (let i = 1; i <= 100; i++) { | ||
| await publisher.send(Buffer.from(`${i}`)) | ||
| } | ||
|
|
||
| console.log("Creating the first consumer, when 50 messages are consumed it saves the offset on the server...") | ||
| const consumer1 = await client.declareConsumer( | ||
| { | ||
| stream: streamName, | ||
| offset: rabbit.Offset.first(), | ||
| singleActive: true, | ||
| consumerRef: consumerRef, | ||
| }, | ||
| async (message) => { | ||
| messagesFromFirstConsumer.push(`Message ${message.content.toString("utf-8")} from ${consumerRef}`) | ||
| if (messagesFromFirstConsumer.length === 50) { | ||
| await consumer1.storeOffset(message.offset) | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| await wait(500) | ||
|
|
||
| console.log("Creating the second consumer, when it becomes active it resumes from the stored offset on the server...") | ||
| await client.declareConsumer( | ||
| { | ||
| stream: streamName, | ||
| offset: rabbit.Offset.first(), | ||
| singleActive: true, | ||
| consumerRef: consumerRef, | ||
| // This callback is executed when the consumer becomes active | ||
| consumerUpdateListener: async (consumerReference, streamName) => { | ||
| const offset = await client.queryOffset({ reference: consumerReference, stream: streamName }) | ||
| return rabbit.Offset.offset(offset) | ||
| }, | ||
| }, | ||
| (message) => { | ||
| messagesFromSecondConsumer.push(`Message ${message.content.toString("utf-8")} from ${consumerRef}`) | ||
| } | ||
| ) | ||
|
|
||
| console.log("Closing the first consumer to trigger the activation of the second one...") | ||
| await client.closeConsumer(consumer1.extendedId) | ||
|
|
||
| await wait(500) | ||
|
|
||
| console.log(`Messages consumed by the first consumer: ${messagesFromFirstConsumer.length}`) | ||
| console.log(`Messages consumed by the second consumer: ${messagesFromSecondConsumer.length}`) | ||
| } | ||
|
|
||
| main() | ||
| .then(() => { | ||
| console.log("done!") | ||
| process.exit(0) | ||
| }) | ||
| .catch((res) => { | ||
| console.log("Error in publishing message!", res) | ||
| process.exit(-1) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.