-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[Service Bus] Add message batching #10305
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
162 changes: 162 additions & 0 deletions
162
sdk/servicebus/Azure.Messaging.ServiceBus/src/Amqp/AmqpMessageBatch.cs
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,162 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Globalization; | ||
| using System.Linq; | ||
| using Azure.Core; | ||
| using Azure.Messaging.ServiceBus.Core; | ||
| using Microsoft.Azure.Amqp; | ||
|
|
||
| namespace Azure.Messaging.ServiceBus.Amqp | ||
| { | ||
| /// <summary> | ||
| /// A set of messages with known size constraints, based on messages to be sent | ||
| /// using an AMQP-based transport. | ||
| /// </summary> | ||
| /// | ||
| internal class AmqpMessageBatch : TransportMessageBatch | ||
| { | ||
| /// <summary>The amount of bytes to reserve as overhead for a small message.</summary> | ||
| private const byte OverheadBytesSmallMessage = 5; | ||
|
|
||
| /// <summary>The amount of bytes to reserve as overhead for a large message.</summary> | ||
| private const byte OverheadBytesLargeMessage = 8; | ||
|
|
||
| /// <summary>The maximum number of bytes that a message may be to be considered small.</summary> | ||
| private const byte MaximumBytesSmallMessage = 255; | ||
|
|
||
| /// <summary>A flag that indicates whether or not the instance has been disposed.</summary> | ||
| private bool _disposed = false; | ||
|
|
||
| /// <summary>The size of the batch, in bytes, as it will be sent via the AMQP transport.</summary> | ||
| private long _sizeBytes = 0; | ||
|
|
||
| /// <summary> | ||
| /// The maximum size allowed for the batch, in bytes. This includes the messages in the batch as | ||
| /// well as any overhead for the batch itself when sent to the Queue/Topic. | ||
| /// </summary> | ||
| /// | ||
| public override long MaximumSizeInBytes { get; } | ||
|
|
||
| /// <summary> | ||
| /// The size of the batch, in bytes, as it will be sent to the Queue/Topic | ||
| /// service. | ||
| /// </summary> | ||
| /// | ||
| public override long SizeInBytes => _sizeBytes; | ||
|
|
||
| /// <summary> | ||
| /// The count of messages contained in the batch. | ||
| /// </summary> | ||
| /// | ||
| public override int Count => BatchMessages.Count; | ||
|
|
||
| /// <summary> | ||
| /// The set of options to apply to the batch. | ||
| /// </summary> | ||
| /// | ||
| private CreateBatchOptions Options { get; } | ||
|
|
||
| /// <summary> | ||
| /// The set of messages that have been added to the batch. | ||
| /// </summary> | ||
| /// | ||
| private List<ServiceBusMessage> BatchMessages { get; } = new List<ServiceBusMessage>(); | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AmqpMessageBatch"/> class. | ||
| /// </summary> | ||
| /// | ||
| /// <param name="options">The set of options to apply to the batch.</param> | ||
| /// | ||
| public AmqpMessageBatch(CreateBatchOptions options) | ||
| { | ||
| Argument.AssertNotNull(options, nameof(options)); | ||
| Argument.AssertNotNull(options.MaximumSizeInBytes, nameof(options.MaximumSizeInBytes)); | ||
|
|
||
| Options = options; | ||
| MaximumSizeInBytes = options.MaximumSizeInBytes.Value; | ||
|
|
||
| // Initialize the size by reserving space for the batch envelope. | ||
|
|
||
| using AmqpMessage envelope = AmqpMessageConverter.BatchSBMessagesAsAmqpMessage(Enumerable.Empty<ServiceBusMessage>()); | ||
| _sizeBytes = envelope.SerializedMessageSize; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Attempts to add a message to the batch, ensuring that the size | ||
| /// of the batch does not exceed its maximum. | ||
| /// </summary> | ||
| /// | ||
| /// <param name="message">The message to attempt to add to the batch.</param> | ||
| /// | ||
| /// <returns><c>true</c> if the message was added; otherwise, <c>false</c>.</returns> | ||
| /// | ||
| public override bool TryAdd(ServiceBusMessage message) | ||
| { | ||
| Argument.AssertNotNull(message, nameof(message)); | ||
| Argument.AssertNotDisposed(_disposed, nameof(ServiceBusMessageBatch)); | ||
|
|
||
| AmqpMessage amqpMessage = AmqpMessageConverter.SBMessageToAmqpMessage(message); | ||
|
|
||
| try | ||
| { | ||
| // Calculate the size for the message, based on the AMQP message size and accounting for a | ||
| // bit of reserved overhead size. | ||
|
|
||
| var size = _sizeBytes | ||
| + amqpMessage.SerializedMessageSize | ||
| + (amqpMessage.SerializedMessageSize <= MaximumBytesSmallMessage | ||
| ? OverheadBytesSmallMessage | ||
| : OverheadBytesLargeMessage); | ||
|
|
||
| if (size > MaximumSizeInBytes) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| _sizeBytes = size; | ||
| BatchMessages.Add(message); | ||
|
|
||
| return true; | ||
| } | ||
| finally | ||
| { | ||
| amqpMessage?.Dispose(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Represents the batch as an enumerable set of transport-specific | ||
| /// representations of a message. | ||
| /// </summary> | ||
| /// | ||
| /// <typeparam name="T">The transport-specific message representation being requested.</typeparam> | ||
| /// | ||
| /// <returns>The set of messages as an enumerable of the requested type.</returns> | ||
| /// | ||
| public override IEnumerable<T> AsEnumerable<T>() | ||
| { | ||
| if (typeof(T) != typeof(ServiceBusMessage)) | ||
| { | ||
| throw new FormatException(string.Format(CultureInfo.CurrentCulture, Resources1.UnsupportedTransportEventType, typeof(T).Name)); | ||
| } | ||
|
|
||
| return (IEnumerable<T>)BatchMessages; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Performs the task needed to clean up resources used by the <see cref="AmqpMessageBatch" />. | ||
| /// </summary> | ||
| /// | ||
| public override void Dispose() | ||
| { | ||
| _disposed = true; | ||
|
|
||
| BatchMessages.Clear(); | ||
| _sizeBytes = 0; | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.