Skip to content

Commit

Permalink
Optimize sending stored messages
Browse files Browse the repository at this point in the history
Optimize recursive function so it consumes less
resources

Signed-off-by: nedimtokic <nedim.tokic@secomind.com>
  • Loading branch information
nedimtokic committed Jun 28, 2024
1 parent 2db731a commit 6ca464a
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttV1Transport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ private async Task ResendFailedMessages(CancellationToken cancellationToken = de
Trace.WriteLine("Resending stored messages in progress.");
_resendingInProgress = true;

if (_client == null || _failedMessageStorage == null)
{
Console.WriteLine("Client or failed message storage is null.");
_resendingInProgress = false;
return;
}

try
{
if (_client == null || _failedMessageStorage == null)
{
Trace.WriteLine("Client or failed message storage is null.");
_resendingInProgress = false;
return;
}

await WaitForPendingMessagesToClear(_client);

var storedMessages = await _failedMessageStorage.LoadQueuedMessagesAsync();
Expand All @@ -263,7 +263,6 @@ private async Task ResendFailedMessages(CancellationToken cancellationToken = de
}

await WaitForPendingMessagesToClear(_client);
await ResendFailedMessages(cancellationToken);
}
catch (OperationCanceledException)
{
Expand All @@ -274,6 +273,8 @@ private async Task ResendFailedMessages(CancellationToken cancellationToken = de
_resendingInProgress = false;
Trace.WriteLine("Resending stored messages finished.");
}

await ResendFailedMessages(cancellationToken);
}

private static async Task WaitForPendingMessagesToClear(IManagedMqttClient _client)
Expand Down

0 comments on commit 6ca464a

Please sign in to comment.