Skip to content

Commit

Permalink
Simpler detect oversized batch (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeminutillo authored Jun 29, 2023
1 parent 60b0b95 commit 3fe6c52
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/Transport/Sending/MessageDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace NServiceBus.Transport.AzureServiceBus
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Transactions;
Expand All @@ -8,6 +9,8 @@

class MessageDispatcher : IDispatchMessages
{
const int MaxMessageThresholdForTransaction = 100;

readonly MessageSenderRegistry messageSenderRegistry;
readonly string topicName;

Expand All @@ -27,6 +30,8 @@ public Task Dispatch(TransportOperations outgoingMessages, TransportTransaction
var unicastTransportOperations = outgoingMessages.UnicastTransportOperations;
var multicastTransportOperations = outgoingMessages.MulticastTransportOperations;

AssertBelowMaxMessageThresholdForTransaction(unicastTransportOperations, multicastTransportOperations, committableTransaction);

var tasks = new List<Task>(unicastTransportOperations.Count + multicastTransportOperations.Count);

foreach (var transportOperation in unicastTransportOperations)
Expand Down Expand Up @@ -66,6 +71,40 @@ public Task Dispatch(TransportOperations outgoingMessages, TransportTransaction
return tasks.Count == 1 ? tasks[0] : Task.WhenAll(tasks);
}

/// <summary>
/// Throws an exception if attempting to send more than 100 messages in a transaction.
/// This prevents the transport from experiencing this issue https://github.com/Azure/azure-sdk-for-net/issues/37265
/// </summary>
static void AssertBelowMaxMessageThresholdForTransaction(List<UnicastTransportOperation> unicastTransportOperations, List<MulticastTransportOperation> multicastTransportOperations, Transaction transaction)
{
var totalNumberOfOperations = unicastTransportOperations.Count + multicastTransportOperations.Count;
if (transaction == null || totalNumberOfOperations <= MaxMessageThresholdForTransaction)
{
return;
}

var numberOfTransactionalOperations = 0;
foreach (var transportOperation in unicastTransportOperations)
{
if (transportOperation.RequiredDispatchConsistency == DispatchConsistency.Default)
{
numberOfTransactionalOperations++;
}
}
foreach (var transportOperation in multicastTransportOperations)
{
if (transportOperation.RequiredDispatchConsistency == DispatchConsistency.Default)
{
numberOfTransactionalOperations++;
}
}

if (numberOfTransactionalOperations > MaxMessageThresholdForTransaction)
{
throw new Exception($"The number of outgoing messages ({numberOfTransactionalOperations}) exceeds the limits permitted by Azure Service Bus ({MaxMessageThresholdForTransaction}) in a single transaction");
}
}

static async Task DispatchOperation(ServiceBusSender sender, ServiceBusMessage message, CommittableTransaction transactionToUse)
{
using (var scope = transactionToUse.ToScope())
Expand Down

0 comments on commit 3fe6c52

Please sign in to comment.