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

Publish stored messages using Enumerator #2018

Draft
wants to merge 1 commit into
base: release/4.x.x
Choose a base branch
from
Draft
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,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace MQTTnet.Extensions.ManagedClient
Expand All @@ -11,6 +12,6 @@ public interface IManagedMqttClientStorage
{
Task SaveQueuedMessagesAsync(IList<ManagedMqttApplicationMessage> messages);

Task<IList<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync();
IAsyncEnumerable<ManagedMqttApplicationMessage> LoadQueuedMessagesAsync(CancellationToken cancellationToken = default);
}
}
}
60 changes: 46 additions & 14 deletions Source/MQTTnet.Extensions.ManagedClient/ManagedMqttClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,6 @@ public async Task StartAsync(ManagedMqttClientOptions options)

Options = options;

if (options.Storage != null)
{
_storageManager = new ManagedMqttClientStorageManager(options.Storage);
var messages = await _storageManager.LoadQueuedMessagesAsync().ConfigureAwait(false);

foreach (var message in messages)
{
_messageQueue.Enqueue(message);
}
}

var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
_connectionCancellationToken = cancellationTokenSource;
Expand Down Expand Up @@ -444,6 +433,48 @@ async Task MaintainConnectionAsync(CancellationToken cancellationToken)
}
}

async Task PublishStoredMessagesAsync(CancellationToken cancellationToken)
{


try
{
_storageManager = new ManagedMqttClientStorageManager(Options.Storage);

while (!cancellationToken.IsCancellationRequested && InternalClient.IsConnected)
{

cancellationToken.ThrowIfCancellationRequested();

if (Options.Storage != null)
{

await foreach (var msg in _storageManager.LoadQueuedMessagesAsync().ConfigureAwait(false))
{

await EnqueueAsync(msg);

cancellationToken.ThrowIfCancellationRequested();

await TryPublishQueuedMessageAsync(msg, cancellationToken).ConfigureAwait(false);
}
}

}
}
catch (OperationCanceledException)
{
}
catch (Exception exception)
{
_logger.Error(exception, "Error while publishing stored application messages.");
}
finally
{
_logger.Verbose("Stopped publishing stored messages.");
}
}

async Task PublishQueuedMessagesAsync(CancellationToken cancellationToken)
{
try
Expand Down Expand Up @@ -665,14 +696,15 @@ async Task<SendSubscribeUnsubscribeResult> SendSubscribeUnsubscribe(List<MqttTop
return new SendSubscribeUnsubscribeResult(subscribeResults, unsubscribeResults);
}

void StartPublishing()
async Task StartPublishing()
{
StopPublishing();

var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
_publishingCancellationToken = cancellationTokenSource;

await PublishStoredMessagesAsync(cancellationToken);
Task.Run(() => PublishQueuedMessagesAsync(cancellationToken), cancellationToken).RunInBackground(_logger);
}

Expand Down Expand Up @@ -717,11 +749,11 @@ async Task TryMaintainConnectionAsync(CancellationToken cancellationToken)
else if (connectionState == ReconnectionResult.Reconnected)
{
await PublishReconnectSubscriptionsAsync(cancellationToken).ConfigureAwait(false);
StartPublishing();
await StartPublishing();
}
else if (connectionState == ReconnectionResult.Recovered)
{
StartPublishing();
await StartPublishing();
}
else if (connectionState == ReconnectionResult.StillConnected)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet.Internal;
Expand All @@ -22,12 +23,13 @@ public ManagedMqttClientStorageManager(IManagedMqttClientStorage storage)
_storage = storage ?? throw new ArgumentNullException(nameof(storage));
}

public async Task<List<ManagedMqttApplicationMessage>> LoadQueuedMessagesAsync()
public async IAsyncEnumerable<ManagedMqttApplicationMessage> LoadQueuedMessagesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var loadedMessages = await _storage.LoadQueuedMessagesAsync().ConfigureAwait(false);
_messages.AddRange(loadedMessages);

return _messages;
await foreach (var message in _storage.LoadQueuedMessagesAsync(cancellationToken).ConfigureAwait(false))
{
_messages.Add(message);
yield return message;
}
}

public async Task AddAsync(ManagedMqttApplicationMessage applicationMessage)
Expand Down Expand Up @@ -63,4 +65,4 @@ private Task SaveAsync()
return _storage.SaveQueuedMessagesAsync(_messages);
}
}
}
}