Skip to content
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

Avoid ServiceBusSenderOptions allocations #44856

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Azure.Core.Amqp.Shared;
using Microsoft.Azure.Amqp;
using Microsoft.Azure.Amqp.Encoding;
using Microsoft.Azure.Amqp.Framing;
Expand Down Expand Up @@ -35,10 +34,10 @@ public class AmqpAnnotatedMessageConverterTests
new double[] { 3.1415926 },
new decimal(3.1415926),
new decimal[] { new decimal(3.1415926) },
DateTimeOffset.Parse("3/24/21").UtcDateTime,
new DateTime[] {DateTimeOffset.Parse("3/24/21").UtcDateTime },
DateTimeOffset.Parse("3/24/21"),
new DateTimeOffset[] {DateTimeOffset.Parse("3/24/21") },
DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime,
new DateTime[] {DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime },
DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture),
new DateTimeOffset[] {DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture) },
TimeSpan.FromSeconds(5),
new TimeSpan[] {TimeSpan.FromSeconds(5)},
new Uri("http://localHost"),
Expand All @@ -53,7 +52,7 @@ public class AmqpAnnotatedMessageConverterTests
new Dictionary<string, short> {{ "key", 1 } },
new Dictionary<string, double> {{ "key", 3.1415926 } },
new Dictionary<string, decimal> {{ "key", new decimal(3.1415926) } },
new Dictionary<string, DateTime> {{ "key", DateTimeOffset.Parse("3/24/21").UtcDateTime } },
new Dictionary<string, DateTime> {{ "key", DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime } },
// for some reason dictionaries with DateTimeOffset, Timespan, or Uri values are not supported in AMQP lib
// new Dictionary<string, DateTimeOffset> {{ "key", DateTimeOffset.Parse("3/24/21") } },
// new Dictionary<string, TimeSpan> {{ "key", TimeSpan.FromSeconds(5) } },
Expand All @@ -69,7 +68,7 @@ public class AmqpAnnotatedMessageConverterTests
Enumerable.Repeat(new object[] { long.MaxValue }, 2),
Enumerable.Repeat(new object[] { 1 }, 2),
Enumerable.Repeat(new object[] { 3.1415926, true }, 2),
Enumerable.Repeat(new object[] { DateTimeOffset.Parse("3/24/21").UtcDateTime, true }, 2),
Enumerable.Repeat(new object[] { DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime, true }, 2),
new List<IList<object>> { new List<object> { "first", 1}, new List<object> { "second", 2 } }
};

Expand Down
2 changes: 2 additions & 0 deletions sdk/servicebus/Azure.Messaging.ServiceBus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

- Updated the `Microsoft.Azure.Amqp` dependency to 2.6.7, which contains a fix for decoding messages with a null format code as the body.

- Instances of `ServiceBusSender` created with no explicit `ServiceBusSenderOptions` value allocate less memory.

## 7.18.0-beta.1 (2024-05-08)

### Features Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
Expand Down Expand Up @@ -256,7 +255,7 @@ private ServiceBusClient(
/// The <see cref="ServiceBusClient"/> was constructed with a connection string containing the "EntityPath" token
/// that has a different value than the <paramref name="queueOrTopicName"/> value specified here.
/// </exception>
public virtual ServiceBusSender CreateSender(string queueOrTopicName) => CreateSender(queueOrTopicName, new ServiceBusSenderOptions());
public virtual ServiceBusSender CreateSender(string queueOrTopicName) => CreateSender(queueOrTopicName, null);

/// <summary>
/// Creates a <see cref="ServiceBusSender"/> instance that can be used for sending messages to a specific
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -120,10 +119,10 @@ internal ServiceBusSender(
Argument.AssertNotNullOrWhiteSpace(entityPath, nameof(entityPath));
connection.ThrowIfClosed();

options = options?.Clone() ?? new ServiceBusSenderOptions();
var identifier = string.IsNullOrEmpty(options?.Identifier) ? DiagnosticUtilities.GenerateIdentifier(EntityPath) : options.Identifier;

EntityPath = entityPath;
Identifier = string.IsNullOrEmpty(options.Identifier) ? DiagnosticUtilities.GenerateIdentifier(EntityPath) : options.Identifier;
Identifier = identifier;
_connection = connection;
_retryPolicy = _connection.RetryOptions.ToRetryPolicy();
_innerSender = _connection.CreateTransportSender(
Expand Down Expand Up @@ -159,7 +158,7 @@ protected ServiceBusSender()
/// <param name="client">The client instance to use for the sender.</param>
/// <param name="queueOrTopicName">The name of the queue or topic to send to.</param>
protected ServiceBusSender(ServiceBusClient client, string queueOrTopicName) :
this(queueOrTopicName, client.Connection, new ServiceBusSenderOptions())
this(queueOrTopicName, client.Connection, null)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.Core.Amqp;
using Azure.Core.Serialization;
using Azure.Core.Shared;
using Azure.Messaging.ServiceBus.Amqp;
using Azure.Messaging.ServiceBus.Primitives;
using Microsoft.Azure.Amqp.Encoding;
using NUnit.Framework;

namespace Azure.Messaging.ServiceBus.Tests.Message
Expand Down Expand Up @@ -431,10 +429,10 @@ public async Task CanSendMultipleDataSections()
new double[] { 3.1415926 },
new decimal(3.1415926),
new decimal[] { new decimal(3.1415926) },
DateTimeOffset.Parse("3/24/21").UtcDateTime,
new DateTime[] {DateTimeOffset.Parse("3/24/21").UtcDateTime },
DateTimeOffset.Parse("3/24/21"),
new DateTimeOffset[] {DateTimeOffset.Parse("3/24/21") },
DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime,
new DateTime[] {DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime },
DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture),
new DateTimeOffset[] {DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture) },
TimeSpan.FromSeconds(5),
new TimeSpan[] {TimeSpan.FromSeconds(5)},
new Uri("http://localHost"),
Expand All @@ -449,7 +447,7 @@ public async Task CanSendMultipleDataSections()
new Dictionary<string, short> {{ "key", 1 } },
new Dictionary<string, double> {{ "key", 3.1415926 } },
new Dictionary<string, decimal> {{ "key", new decimal(3.1415926) } },
new Dictionary<string, DateTime> {{ "key", DateTimeOffset.Parse("3/24/21").UtcDateTime } },
new Dictionary<string, DateTime> {{ "key", DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime } },
// for some reason dictionaries with DateTimeOffset, Timespan, or Uri values are not supported in AMQP lib
// new Dictionary<string, DateTimeOffset> {{ "key", DateTimeOffset.Parse("3/24/21") } },
// new Dictionary<string, TimeSpan> {{ "key", TimeSpan.FromSeconds(5) } },
Expand Down Expand Up @@ -494,7 +492,7 @@ public async Task CanSendValueSection(object value)
Enumerable.Repeat(new object[] { long.MaxValue }, 2),
Enumerable.Repeat(new object[] { 1 }, 2),
Enumerable.Repeat(new object[] { 3.1415926, true }, 2),
Enumerable.Repeat(new object[] { DateTimeOffset.Parse("3/24/21").UtcDateTime, true }, 2),
Enumerable.Repeat(new object[] { DateTimeOffset.Parse("3/24/21", CultureInfo.InvariantCulture).UtcDateTime, true }, 2),
new List<IList<object>> { new List<object> { "first", 1}, new List<object> { "second", 2 } }
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public async Task SendBatchManagesLockingTheBatch()

Assert.That(batch.TryAddMessage(new ServiceBusMessage(Array.Empty<byte>())), Is.True, "The batch should not be locked before sending.");

var sender = new ServiceBusSender("dummy", mockConnection.Object, new ServiceBusSenderOptions());
var sender = new ServiceBusSender("dummy", mockConnection.Object);
var sendTask = sender.SendMessagesAsync(batch);

Assert.That(() => batch.TryAddMessage(new ServiceBusMessage(Array.Empty<byte>())), Throws.InstanceOf<InvalidOperationException>(), "The batch should be locked while sending.");
Expand Down Expand Up @@ -281,7 +281,7 @@ public async Task CloseRespectsCancellationToken()
It.IsAny<string>()))
.Returns(mockTransportSender.Object);

var sender = new ServiceBusSender("fake", mockConnection.Object, new ServiceBusSenderOptions());
var sender = new ServiceBusSender("fake", mockConnection.Object);
await sender.CloseAsync(cts.Token);
mockTransportSender.Verify(transportReceiver => transportReceiver.CloseAsync(It.Is<CancellationToken>(ct => ct == cts.Token)));
}
Expand Down
Loading