-
Notifications
You must be signed in to change notification settings - Fork 11
Make messages stream breakable automatically #72
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
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,9 @@ import { | |
type ConsumeOptions, | ||
type CorruptedMessageHandler, | ||
type GroupAssignment, | ||
type Offsets | ||
type Offsets, | ||
type MessagesStreamBreakModeValue, | ||
MessagesStreamBreakModes | ||
} from './types.ts' | ||
|
||
// Don't move this function as being in the same file will enable V8 to remove. | ||
|
@@ -46,6 +48,7 @@ export function defaultCorruptedMessageHandler (): boolean { | |
export class MessagesStream<Key, Value, HeaderKey, HeaderValue> extends Readable { | ||
#consumer: Consumer<Key, Value, HeaderKey, HeaderValue> | ||
#mode: string | ||
#breakMode: MessagesStreamBreakModeValue | ||
#fallbackMode: string | ||
#options: ConsumeOptions<Key, Value, HeaderKey, HeaderValue> | ||
#topics: string[] | ||
|
@@ -82,6 +85,7 @@ export class MessagesStream<Key, Value, HeaderKey, HeaderValue> extends Readable | |
super({ objectMode: true, highWaterMark: options.highWaterMark ?? defaultConsumerOptions.highWaterMark }) | ||
this.#consumer = consumer | ||
this.#mode = mode ?? MessagesStreamModes.LATEST | ||
this.#breakMode = options.breakMode ?? MessagesStreamBreakModes.MANUAL | ||
this.#fallbackMode = fallbackMode ?? MessagesStreamFallbackModes.LATEST | ||
this.#offsetsToCommit = new Map() | ||
this.#topics = structuredClone(options.topics) | ||
|
@@ -283,6 +287,13 @@ export class MessagesStream<Key, Value, HeaderKey, HeaderValue> extends Readable | |
return super[Symbol.asyncIterator]() | ||
} | ||
|
||
collect (): Promise<Message<Key, Value, HeaderKey, HeaderValue>[]> { | ||
if (this.#breakMode === MessagesStreamBreakModes.MANUAL) { | ||
throw new UserError('Cannot collect messages when the stream is in MANUAL break mode.') | ||
} | ||
return Array.fromAsync(this) | ||
} | ||
|
||
_construct (callback: (error?: Error) => void) { | ||
this.#refreshOffsets(callback as Callback<void>) | ||
} | ||
|
@@ -402,6 +413,10 @@ export class MessagesStream<Key, Value, HeaderKey, HeaderValue> extends Readable | |
} | ||
|
||
this.#pushRecords(metadata, topicIds, response, requestedOffsets) | ||
|
||
if (this.#breakMode === MessagesStreamBreakModes.AFTER_FIRST_BATCH && this.#inflightNodes.size === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be generalized to collecting the first n batches. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. |
||
this.push(null) | ||
} | ||
}) | ||
} | ||
}) | ||
|
@@ -520,7 +535,7 @@ export class MessagesStream<Key, Value, HeaderKey, HeaderValue> extends Readable | |
this.#autocommit() | ||
} | ||
|
||
if (canPush && !(this.#shouldClose || this.closed || this.destroyed)) { | ||
if (canPush && this.#breakMode !== MessagesStreamBreakModes.AFTER_FIRST_BATCH && !(this.#shouldClose || this.closed || this.destroyed)) { | ||
process.nextTick(() => { | ||
this.#fetch() | ||
}) | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I would add this. As you noted, a user could just do
Array.fromAsync()
, which would do the trick. It would be good to document it in the docs.The place where we should add this would be in the
consumer
ascollectNBatch()
, which would be easier.