-
Notifications
You must be signed in to change notification settings - Fork 22
[sender] Add SendAsBatch() method with multiple batch support and deprecate SendMessageBatch() #279
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
Conversation
|
@karenychen 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs. I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review. |
…e SendMessageBatch Co-authored-by: karenychen <[email protected]>
…thod Co-authored-by: karenychen <[email protected]>
karenychen
left a comment
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.
@copilot follow this following example to see how we can send multiple batches. Try to combine sendSingleBatch and sendMultipleBatches to avoid redundant code
// Create a message batch. It will automatically be sized for the Service Bus
// namespace's maximum message size.
currentMessageBatch, err := sender.NewMessageBatch(context.TODO(), nil)
if err != nil {
panic(err)
}
messagesToSend := []*azservicebus.Message{
// any messages that you'd want to send would be here, or sourced from
// somewhere else.
}
for i := 0; i < len(messagesToSend); i++ {
// Add a message to our message batch. This can be called multiple times.
err = currentMessageBatch.AddMessage(messagesToSend[i], nil)
if errors.Is(err, azservicebus.ErrMessageTooLarge) {
if currentMessageBatch.NumMessages() == 0 {
// This means the message itself is too large to be sent, even on its own.
// This will require intervention from the user.
panic("Single message is too large to be sent in a batch.")
}
fmt.Printf("Message batch is full. Sending it and creating a new one.\n")
// send what we have since the batch is full
err := sender.SendMessageBatch(context.TODO(), currentMessageBatch, nil)
if err != nil {
panic(err)
}
// Create a new batch and retry adding this message to our batch.
newBatch, err := sender.NewMessageBatch(context.TODO(), nil)
if err != nil {
panic(err)
}
currentMessageBatch = newBatch
// rewind the counter and attempt to add the message again (this batch
// was full so it didn't go out with the previous SendMessageBatch call).
i--
} else if err != nil {
panic(err)
}
}
// check if any messages are remaining to be sent.
if currentMessageBatch.NumMessages() > 0 {
err := sender.SendMessageBatch(context.TODO(), currentMessageBatch, nil)
if err != nil {
panic(err)
}
}…te code duplication Co-authored-by: karenychen <[email protected]>
... I've successfully refactored the
The key improvement is using This follows the exact pattern from the Azure Service Bus documentation and significantly reduces code complexity. Commit: ab8056e Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
v2/sender.go
Outdated
| // | ||
| // Example usage: | ||
| // // For backward compatibility (single batch only) | ||
| // err := sender.SendAsBatch(ctx, messages, &SendAsBatchOptions{AllowMultipleBatch: false}) | ||
| // | ||
| // // For large message arrays (multiple batches allowed) | ||
| // err := sender.SendAsBatch(ctx, messages, &SendAsBatchOptions{AllowMultipleBatch: true}) | ||
| // | ||
| // // With nil options (defaults to AllowMultipleBatch: false) | ||
| // err := sender.SendAsBatch(ctx, messages, nil) |
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.
@copilot the usages are pretty simple, let's remove the example usage here
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.
Removed the example usage documentation from the SendAsBatch method comments. Commit: 50c7834
…Batch method Co-authored-by: karenychen <[email protected]>
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.
Pull Request Overview
This PR introduces a new SendAsBatch() method that addresses Azure Service Bus batch size limitations by enabling automatic splitting of large message arrays into multiple batches, while maintaining backward compatibility with the existing SendMessageBatch() method.
- Adds
SendAsBatch()method with configurableSendAsBatchOptionsto control single vs. multiple batch behavior - Deprecates
SendMessageBatch()which now internally delegates toSendAsBatch()with single-batch mode - Implements comprehensive error handling for batch creation failures, message size limits, and multi-batch scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| v2/sender.go | Implements the new SendAsBatch() method with batch splitting logic and deprecates SendMessageBatch() |
| v2/sender_test.go | Adds comprehensive test coverage for the new method including edge cases and error scenarios |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
…f sending empty batches Co-authored-by: karenychen <[email protected]>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
This PR addresses the need to send large message arrays (up to 5000+ messages) that exceed Azure Service Bus batch size limits by implementing a new
SendAsBatch()method with configurable batching behavior.Changes Made
New SendAsBatch Method
Added
SendAsBatch(ctx, messages, options)method to theSenderinterface with a newSendAsBatchOptionsstruct:Usage Examples:
Key Features
AllowMultipleBatch: true, large message arrays are automatically split across multiple batches as neededSendTimeoutconfiguration for both single and multiple batch modesImplementation Details
The method follows the standard Azure Service Bus pattern of creating batches, adding messages until
ErrMessageTooLargeis encountered, then either failing (single batch mode) or sending the current batch and creating a new one (multiple batch mode).Backward Compatibility
SendMessageBatch(): The existing method now internally callsSendAsBatch()withAllowMultipleBatch: false, maintaining identical behavior except for empty message arraysTesting
Added comprehensive test coverage covering:
Problem Solved
The original
SendMessageBatch()method failed completely if the message array exceeded the maximum batch size. This implementation allows consumers to send large numbers of messages efficiently without manually handling the batching logic themselves, while providing more sensible error handling for edge cases.Fixes #278.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.