Skip to content

Commit

Permalink
Improve sonar metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
  • Loading branch information
zarusz committed Nov 24, 2023
1 parent ca8d6b7 commit 4f89c97
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 114 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ SlimMessageBus is a client façade for message brokers for .NET. It comes with i
[![Gitter](https://badges.gitter.im/SlimMessageBus/community.svg)](https://gitter.im/SlimMessageBus/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![GitHub license](https://img.shields.io/github/license/zarusz/SlimMessageBus)](https://github.com/zarusz/SlimMessageBus/blob/master/LICENSE)
[![Build](https://github.com/zarusz/SlimMessageBus/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/zarusz/SlimMessageBus/actions/workflows/build.yml)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=zarusz_SlimMessageBus)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=coverage)](https://sonarcloud.io/summary/new_code?id=zarusz_SlimMessageBus)
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=duplicated_lines_density)](https://sonarcloud.io/summary/new_code?id=zarusz_SlimMessageBus)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=zarusz_SlimMessageBus)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=zarusz_SlimMessageBus)
[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=sqale_rating)](https://sonarcloud.io/summary/overall?id=zarusz_SlimMessageBus)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=coverage)](https://sonarcloud.io/summary/overall?id=zarusz_SlimMessageBus)
[![Duplicated Lines (%)](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=duplicated_lines_density)](https://sonarcloud.io/summary/overall?id=zarusz_SlimMessageBus)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=vulnerabilities)](https://sonarcloud.io/summary/overall?id=zarusz_SlimMessageBus)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=zarusz_SlimMessageBus&metric=alert_status)](https://sonarcloud.io/summary/overall?id=zarusz_SlimMessageBus)

> The 2.x.x major release is available.
> Please review the [migration guide](https://github.com/zarusz/SlimMessageBus/releases/tag/Host.Transport-2.0.0).
Expand Down
2 changes: 1 addition & 1 deletion src/Host.Plugin.Properties.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.1;net6.0</TargetFrameworks>
<Version>2.2.0-rc1</Version>
<Version>2.2.0-rc2</Version>
</PropertyGroup>

</Project>
18 changes: 9 additions & 9 deletions src/SlimMessageBus.Host.AsyncApi/MessageBusDocumentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private static void GenerateChannelsFromConsumers(IDictionary<string, ChannelIte
{
foreach (var consumer in messageBusSettings.Consumers)
{
var subscribeOperation = GenerateOperationConsumer(consumer, schemaResolver, jsonSchemaGenerator, serviceProvider, namedServer);
var subscribeOperation = GenerateOperationConsumer(consumer, schemaResolver, jsonSchemaGenerator, serviceProvider);
if (subscribeOperation == null)
{
continue;
Expand Down Expand Up @@ -128,7 +128,7 @@ private static void GenerateChannelsFromProducers(IDictionary<string, ChannelIte
{
foreach (var producer in messageBusSettings.Producers)
{
var publishOperation = GenerateOperationProducer(producer, schemaResolver, jsonSchemaGenerator, serviceProvider, namedServer);
var publishOperation = GenerateOperationProducer(producer, schemaResolver, jsonSchemaGenerator);
if (publishOperation == null)
{
continue;
Expand All @@ -152,7 +152,7 @@ private static void GenerateChannelsFromProducers(IDictionary<string, ChannelIte
}
}

private static Operation? GenerateOperationConsumer(ConsumerSettings consumer, AsyncApiSchemaResolver schemaResolver, JsonSchemaGenerator jsonSchemaGenerator, IServiceProvider serviceProvider, NamedServer namedServer)
private static Operation? GenerateOperationConsumer(ConsumerSettings consumer, AsyncApiSchemaResolver schemaResolver, JsonSchemaGenerator jsonSchemaGenerator, IServiceProvider serviceProvider)
{
var consumerInstance = serviceProvider.GetRequiredService(consumer.ConsumerType);
var consumerType = consumerInstance.GetType();
Expand All @@ -174,7 +174,7 @@ private static void GenerateChannelsFromProducers(IDictionary<string, ChannelIte
// Add all message types pushed via this channel (topic/queue)
foreach (var invoker in consumer.Invokers)
{
var message = GenerateMessageFromAttribute(invoker.MessageType, schemaResolver, jsonSchemaGenerator, namedServer);
var message = GenerateMessageFromAttribute(invoker.MessageType, schemaResolver, jsonSchemaGenerator);
if (message != null)
{
messages.OneOf.Add(message);
Expand All @@ -183,7 +183,7 @@ private static void GenerateChannelsFromProducers(IDictionary<string, ChannelIte

if (messages.OneOf.Count == 1)
{
operation.Message = messages.OneOf.First();
operation.Message = messages.OneOf[0];
}

return operation;
Expand All @@ -199,7 +199,7 @@ static internal string GetMessageId(Type messageType)
return messageType.Name;
}

private static Operation? GenerateOperationProducer(ProducerSettings producer, AsyncApiSchemaResolver schemaResolver, JsonSchemaGenerator jsonSchemaGenerator, IServiceProvider serviceProvider, NamedServer namedServer)
private static Operation? GenerateOperationProducer(ProducerSettings producer, AsyncApiSchemaResolver schemaResolver, JsonSchemaGenerator jsonSchemaGenerator)
{
var messages = new Messages();
var operation = new Operation
Expand All @@ -209,21 +209,21 @@ static internal string GetMessageId(Type messageType)
};

// Add all message types pushed via this channel (topic/queue)
var message = GenerateMessageFromAttribute(producer.MessageType, schemaResolver, jsonSchemaGenerator, namedServer);
var message = GenerateMessageFromAttribute(producer.MessageType, schemaResolver, jsonSchemaGenerator);
if (message != null)
{
messages.OneOf.Add(message);
}

if (messages.OneOf.Count == 1)
{
operation.Message = messages.OneOf.First();
operation.Message = messages.OneOf[0];
}

return operation;
}

private static IMessage GenerateMessageFromAttribute(Type messageType, AsyncApiSchemaResolver schemaResolver, JsonSchemaGenerator jsonSchemaGenerator, NamedServer namedServer)
private static IMessage GenerateMessageFromAttribute(Type messageType, AsyncApiSchemaResolver schemaResolver, JsonSchemaGenerator jsonSchemaGenerator)
{
var message = new Message
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ public static void SetKeyProvider(this ProducerSettings producerSettings, Func<o
=> producerSettings.Properties[KeyProviderKey] = keyProvider;

public static Func<object, string> GetKeyProvider(this ProducerSettings producerSettings)
=> producerSettings.Properties.ContainsKey(KeyProviderKey) ? producerSettings.Properties[KeyProviderKey] as Func<object, string> : null;
=> producerSettings.Properties.TryGetValue(KeyProviderKey, out var value) ? value as Func<object, string> : null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
public class ServiceBusMessageBus : MessageBusBase<ServiceBusMessageBusSettings>
{
private readonly ILogger _logger;

private ServiceBusClient _client;
private SafeDictionaryWrapper<string, ServiceBusSender> _producerByPath;

private Task _provisionTopologyTask = null;

public ServiceBusMessageBus(MessageBusSettings settings, ServiceBusMessageBusSettings providerSettings)
: base(settings, providerSettings)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private enum TopologyCreationStatus
Created = 4
}

private async Task<TopologyCreationStatus> SwallowExceptionIfEntityExists(Func<Task<TopologyCreationStatus>> task)
private static async Task<TopologyCreationStatus> SwallowExceptionIfEntityExists(Func<Task<TopologyCreationStatus>> task)
{
try
{
Expand All @@ -39,7 +39,7 @@ private async Task<TopologyCreationStatus> SwallowExceptionIfEntityExists(Func<T
}
}

private async Task<Response> SwallowExceptionIfMessagingEntityNotFound(Func<Task<Response>> task)
private static async Task<Response> SwallowExceptionIfMessagingEntityNotFound(Func<Task<Response>> task)
{
try
{
Expand Down Expand Up @@ -207,7 +207,7 @@ await TryCreateQueue(path, topologyProvisioning.CanConsumerCreateQueue, options
removeRuleTasks
.AddRange(rulesPage.Values
.Where(rule => !filters.Any(filter => filter.Name == rule.Name))
.Select(rule => SwallowExceptionIfMessagingEntityNotFound(() =>
.Select(rule => ServiceBusTopologyService.SwallowExceptionIfMessagingEntityNotFound(() =>
adminClient.DeleteRuleAsync(path, subscriptionName, rule.Name)))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ public ConsumerBuilder<T> Path(string path)
return this;
}

public ConsumerBuilder<T> Topic(string topic) => Path(topic);

public ConsumerBuilder<T> Path(string path, Action<ConsumerBuilder<T>> pathConfig)
{
if (pathConfig is null) throw new ArgumentNullException(nameof(pathConfig));
Expand All @@ -25,6 +23,8 @@ public ConsumerBuilder<T> Path(string path, Action<ConsumerBuilder<T>> pathConfi
return b;
}

public ConsumerBuilder<T> Topic(string topic) => Path(topic);

public ConsumerBuilder<T> Topic(string topic, Action<ConsumerBuilder<T>> topicConfig) => Path(topic, topicConfig);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Description>Core configuration interfaces of SlimMessageBus</Description>
<PackageTags>SlimMessageBus</PackageTags>
<RootNamespace>SlimMessageBus.Host</RootNamespace>
<Version>2.0.5-rc1</Version>
<Version>2.0.5-rc2</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/SlimMessageBus.Host.Mqtt/MqttMessageBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override async Task CreateConsumers()
_mqttClient = ProviderSettings.MqttFactory.CreateManagedMqttClient();
_mqttClient.ApplicationMessageReceivedAsync += OnMessageReceivedAsync;

object MessageProvider(Type messageType, MqttApplicationMessage transportMessage) => Serializer.Deserialize(messageType, transportMessage.Payload);
object MessageProvider(Type messageType, MqttApplicationMessage transportMessage) => Serializer.Deserialize(messageType, transportMessage.PayloadSegment.Array);

void AddTopicConsumer(string topic, IMessageProcessor<MqttApplicationMessage> messageProcessor)
{
Expand All @@ -63,7 +63,7 @@ void AddTopicConsumer(string topic, IMessageProcessor<MqttApplicationMessage> me

if (Settings.RequestResponse != null)
{
var processor = new ResponseMessageProcessor<MqttApplicationMessage>(LoggerFactory, Settings.RequestResponse, responseConsumer: this, messagePayloadProvider: m => m.Payload);
var processor = new ResponseMessageProcessor<MqttApplicationMessage>(LoggerFactory, Settings.RequestResponse, responseConsumer: this, messagePayloadProvider: m => m.PayloadSegment.Array);
AddTopicConsumer(Settings.RequestResponse.Path, processor);
}

Expand Down Expand Up @@ -104,7 +104,7 @@ protected override async Task ProduceToTransport(object message, string path, by
{
var m = new MqttApplicationMessage
{
Payload = messagePayload,
PayloadSegment = messagePayload,
Topic = path
};

Expand Down
16 changes: 8 additions & 8 deletions src/SlimMessageBus.Host.Outbox.Sql/SqlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

public static class SqlHelper
{
private static readonly ISet<int> TransientErrorNumbers = new HashSet<int>
private static readonly HashSet<int> TransientErrorNumbers = new()
{
4060, 40197, 40501, 40613, 49918, 49919, 49920, 11001
};

public static async Task<TResult> RetryIfError<TResult>(ILogger logger, CancellationToken token, SqlRetrySettings retrySettings, Func<SqlException, bool> shouldRetry, Func<Task<TResult>> operation)
public static async Task<TResult> RetryIfError<TResult>(ILogger logger, SqlRetrySettings retrySettings, Func<SqlException, bool> shouldRetry, Func<Task<TResult>> operation, CancellationToken token)
{
Exception lastTransientException = null;
var nextRetryInterval = retrySettings.RetryInterval;
Expand All @@ -20,8 +20,8 @@ public static async Task<TResult> RetryIfError<TResult>(ILogger logger, Cancella
{
if (tries > 1)
{
logger.LogInformation("SQL error encountered. Will begin attempt number {0} of {1} max...", tries, retrySettings.RetryCount);
await Task.Delay(nextRetryInterval);
logger.LogInformation("SQL error encountered. Will begin attempt number {SqlRetryNumber} of {SqlRetryCount} max...", tries, retrySettings.RetryCount);
await Task.Delay(nextRetryInterval, token);
nextRetryInterval = nextRetryInterval.Multiply(retrySettings.RetryIntervalFactor);
}
var result = await operation();
Expand All @@ -42,9 +42,9 @@ public static async Task<TResult> RetryIfError<TResult>(ILogger logger, Cancella
throw lastTransientException;
}

public static Task<TResult> RetryIfTransientError<TResult>(ILogger logger, CancellationToken token, SqlRetrySettings retrySettings, Func<Task<TResult>> operation) =>
RetryIfError(logger, token, retrySettings, sqlEx => TransientErrorNumbers.Contains(sqlEx.Number), operation);
public static Task<TResult> RetryIfTransientError<TResult>(ILogger logger, SqlRetrySettings retrySettings, Func<Task<TResult>> operation, CancellationToken token) =>
RetryIfError(logger, retrySettings, sqlEx => TransientErrorNumbers.Contains(sqlEx.Number), operation, token);

public static Task RetryIfTransientError(ILogger logger, CancellationToken token, SqlRetrySettings retrySettings, Func<Task> operation) =>
RetryIfTransientError<object>(logger, token, retrySettings, async () => { await operation(); return null; });
public static Task RetryIfTransientError(ILogger logger, SqlRetrySettings retrySettings, Func<Task> operation, CancellationToken token) =>
RetryIfTransientError<object>(logger, retrySettings, async () => { await operation(); return null; }, token);
}
Loading

0 comments on commit 4f89c97

Please sign in to comment.