-
Notifications
You must be signed in to change notification settings - Fork 340
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
feat: adding concurrency functionality #562
base: canary
Are you sure you want to change the base?
Changes from all commits
c0619e8
0d37992
0daa47d
4760488
e26621a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,11 +63,14 @@ export class Consumer extends TypedEventEmitter { | |
private authenticationErrorTimeout: number; | ||
private pollingWaitTimeMs: number; | ||
private pollingCompleteWaitTimeMs: number; | ||
private concurrencyWaitTimeMs: number; | ||
private heartbeatInterval: number; | ||
private isPolling = false; | ||
private stopRequestedAtTimestamp: number; | ||
public abortController: AbortController; | ||
private extendedAWSErrors: boolean; | ||
private concurrency: number; | ||
private inFlightMessages = 0; | ||
|
||
constructor(options: ConsumerOptions) { | ||
super(options.queueUrl); | ||
|
@@ -92,9 +95,11 @@ export class Consumer extends TypedEventEmitter { | |
options.authenticationErrorTimeout ?? 10000; | ||
this.pollingWaitTimeMs = options.pollingWaitTimeMs ?? 0; | ||
this.pollingCompleteWaitTimeMs = options.pollingCompleteWaitTimeMs ?? 0; | ||
this.concurrencyWaitTimeMs = options.concurrencyWaitTimeMs ?? 50; | ||
this.shouldDeleteMessages = options.shouldDeleteMessages ?? true; | ||
this.alwaysAcknowledge = options.alwaysAcknowledge ?? false; | ||
this.extendedAWSErrors = options.extendedAWSErrors ?? false; | ||
this.concurrency = Math.max(options.concurrency ?? 1, this.batchSize); | ||
this.sqs = | ||
options.sqs || | ||
new SQSClient({ | ||
|
@@ -328,21 +333,47 @@ export class Consumer extends TypedEventEmitter { | |
private async handleSqsResponse( | ||
response: ReceiveMessageCommandOutput, | ||
): Promise<void> { | ||
if (hasMessages(response)) { | ||
if (this.handleMessageBatch) { | ||
await this.processMessageBatch(response.Messages); | ||
} else { | ||
await Promise.all( | ||
response.Messages.map((message: Message) => | ||
this.processMessage(message), | ||
), | ||
); | ||
} | ||
|
||
this.emit("response_processed"); | ||
} else if (response) { | ||
if (!hasMessages(response)) { | ||
this.emit("empty"); | ||
return; | ||
} | ||
|
||
const messages = response.Messages; | ||
|
||
if (this.handleMessageBatch) { | ||
await this.processMessageBatch(messages); | ||
} else { | ||
let waitingMessages = 0; | ||
await Promise.all( | ||
messages.map(async (message) => { | ||
while ( | ||
this.batchSize === 1 && | ||
this.inFlightMessages >= this.concurrency | ||
) { | ||
if (waitingMessages === 0) { | ||
this.emit("concurrency_limit_reached", { | ||
limit: this.concurrency, | ||
waiting: messages.length - this.inFlightMessages, | ||
}); | ||
} | ||
waitingMessages++; | ||
await new Promise((resolve) => | ||
setTimeout(resolve, this.concurrencyWaitTimeMs), | ||
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 need to think if |
||
); | ||
if (this.stopped) return; | ||
} | ||
waitingMessages = Math.max(0, waitingMessages - 1); | ||
this.inFlightMessages++; | ||
try { | ||
await this.processMessage(message); | ||
} finally { | ||
this.inFlightMessages--; | ||
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. It would be nice if it could poll again to maintain maximum concurrency. |
||
} | ||
}), | ||
); | ||
} | ||
|
||
this.emit("response_processed"); | ||
} | ||
|
||
/** | ||
|
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.
We may want a maxConcurrency setting that ensures we're not trying to do silly stuff.