Add overflow strategy for pending messages queue.

This commit is contained in:
Christian
2018-05-11 20:49:31 +02:00
parent 57287a5303
commit e14ea9a240
10 changed files with 103 additions and 71 deletions
@@ -57,14 +57,14 @@ namespace MQTTnet.Client
_adapter = _adapterFactory.CreateClientAdapter(options, _logger);
_logger.Verbose<MqttClient>("Trying to connect with server.");
_logger.Verbose(this, "Trying to connect with server.");
await _adapter.ConnectAsync(_options.CommunicationTimeout, _cancellationTokenSource.Token).ConfigureAwait(false);
_logger.Verbose<MqttClient>("Connection with server established.");
_logger.Verbose(this, "Connection with server established.");
StartReceivingPackets(_cancellationTokenSource.Token);
var connectResponse = await AuthenticateAsync(options.WillMessage, _cancellationTokenSource.Token).ConfigureAwait(false);
_logger.Verbose<MqttClient>("MQTT connection with server established.");
_logger.Verbose(this, "MQTT connection with server established.");
_sendTracker.Restart();
@@ -76,12 +76,12 @@ namespace MQTTnet.Client
IsConnected = true;
Connected?.Invoke(this, new MqttClientConnectedEventArgs(connectResponse.IsSessionPresent));
_logger.Info<MqttClient>("Connected.");
_logger.Info(this, "Connected.");
return new MqttClientConnectResult(connectResponse.IsSessionPresent);
}
catch (Exception exception)
{
_logger.Error<MqttClient>(exception, "Error while connecting with server.");
_logger.Error(this, exception, "Error while connecting with server.");
await DisconnectInternalAsync(null, exception).ConfigureAwait(false);
throw;
@@ -233,7 +233,7 @@ namespace MQTTnet.Client
private async Task DisconnectInternalAsync(Task sender, Exception exception)
{
await _disconnectLock.WaitAsync();
await _disconnectLock.WaitAsync().ConfigureAwait(false);
try
{
if (_cancellationTokenSource == null || _cancellationTokenSource.IsCancellationRequested)
@@ -245,7 +245,7 @@ namespace MQTTnet.Client
}
catch (Exception adapterException)
{
_logger.Warning<MqttClient>(adapterException, "Error while disconnecting from adapter.");
_logger.Warning(this, adapterException, "Error while disconnecting from adapter.");
}
finally
{
@@ -260,21 +260,16 @@ namespace MQTTnet.Client
await WaitForTaskAsync(_packetReceiverTask, sender).ConfigureAwait(false);
await WaitForTaskAsync(_keepAliveMessageSenderTask, sender).ConfigureAwait(false);
if (_keepAliveMessageSenderTask != null && _keepAliveMessageSenderTask != sender)
{
await _keepAliveMessageSenderTask.ConfigureAwait(false);
}
if (_adapter != null)
{
await _adapter.DisconnectAsync(_options.CommunicationTimeout, CancellationToken.None).ConfigureAwait(false);
}
_logger.Verbose<MqttClient>("Disconnected from adapter.");
_logger.Verbose(this, "Disconnected from adapter.");
}
catch (Exception adapterException)
{
_logger.Warning<MqttClient>(adapterException, "Error while disconnecting from adapter.");
_logger.Warning(this, adapterException, "Error while disconnecting from adapter.");
}
finally
{
@@ -283,7 +278,7 @@ namespace MQTTnet.Client
_cancellationTokenSource?.Dispose();
_cancellationTokenSource = null;
_logger.Info<MqttClient>("Disconnected.");
_logger.Info(this, "Disconnected.");
Disconnected?.Invoke(this, new MqttClientDisconnectedEventArgs(clientWasConnected, exception));
}
}
@@ -329,7 +324,7 @@ namespace MQTTnet.Client
}
catch (MqttCommunicationTimedOutException)
{
_logger.Warning<MqttPacketDispatcher>($"Timeout while waiting for packet of type '{typeof(TResponsePacket).Namespace}'.");
_logger.Warning(this, null, "Timeout while waiting for packet of type '{0}'.", typeof(TResponsePacket).Namespace);
throw;
}
finally
@@ -340,7 +335,7 @@ namespace MQTTnet.Client
private async Task SendKeepAliveMessagesAsync(CancellationToken cancellationToken)
{
_logger.Verbose<MqttClient>("Start sending keep alive packets.");
_logger.Verbose(this, "Start sending keep alive packets.");
try
{
@@ -367,24 +362,24 @@ namespace MQTTnet.Client
}
else if (exception is MqttCommunicationException)
{
_logger.Warning<MqttClient>(exception, "MQTT communication exception while sending/receiving keep alive packets.");
_logger.Warning(this, exception, "MQTT communication exception while sending/receiving keep alive packets.");
}
else
{
_logger.Error<MqttClient>(exception, "Unhandled exception while sending/receiving keep alive packets.");
_logger.Error(this, exception, "Unhandled exception while sending/receiving keep alive packets.");
}
await DisconnectInternalAsync(_keepAliveMessageSenderTask, exception).ConfigureAwait(false);
}
finally
{
_logger.Verbose<MqttClient>("Stopped sending keep alive packets.");
_logger.Verbose(this, "Stopped sending keep alive packets.");
}
}
private async Task ReceivePacketsAsync(CancellationToken cancellationToken)
{
_logger.Verbose<MqttClient>("Start receiving packets.");
_logger.Verbose(this, "Start receiving packets.");
try
{
@@ -419,11 +414,11 @@ namespace MQTTnet.Client
}
else if (exception is MqttCommunicationException)
{
_logger.Warning<MqttClient>(exception, "MQTT communication exception while receiving packets.");
_logger.Warning(this, exception, "MQTT communication exception while receiving packets.");
}
else
{
_logger.Error<MqttClient>(exception, "Unhandled exception while receiving packets.");
_logger.Error(this, exception, "Unhandled exception while receiving packets.");
}
await DisconnectInternalAsync(_packetReceiverTask, exception).ConfigureAwait(false);
@@ -431,7 +426,7 @@ namespace MQTTnet.Client
}
finally
{
_logger.Verbose<MqttClient>("Stopped receiving packets.");
_logger.Verbose(this, "Stopped receiving packets.");
}
}
@@ -467,7 +462,7 @@ namespace MQTTnet.Client
}
catch (Exception exception)
{
_logger.Error<MqttClient>(exception, "Unhandled exception while processing received packet.");
_logger.Error(this, exception, "Unhandled exception while processing received packet.");
}
}
@@ -508,7 +503,7 @@ namespace MQTTnet.Client
private void StartReceivingPackets(CancellationToken cancellationToken)
{
_packetReceiverTask = Task.Factory.StartNew(
() => ReceivePacketsAsync(cancellationToken),
() => ReceivePacketsAsync(cancellationToken),
cancellationToken,
TaskCreationOptions.LongRunning,
TaskScheduler.Current);
@@ -518,7 +513,7 @@ namespace MQTTnet.Client
{
_keepAliveMessageSenderTask = Task.Factory.StartNew(
() => SendKeepAliveMessagesAsync(cancellationToken),
cancellationToken,
cancellationToken,
TaskCreationOptions.LongRunning,
TaskScheduler.Current);
}
@@ -539,7 +534,7 @@ namespace MQTTnet.Client
}
catch (Exception exception)
{
_logger.Error<MqttClient>(exception, "Unhandled exception while handling application message.");
_logger.Error(this, exception, "Unhandled exception while handling application message.");
}
}