unfifed timeout handling and fixed memory usage due to Task.Delay tasks for each send package are present for the duration of the timeout resulting in memory usage. new approach uses cancellationtoken that will be cleaned up directly if operation completes before timeout

This commit is contained in:
Eggers Jan
2017-09-13 09:12:34 +02:00
parent 5712bc11d2
commit 090e59e99a
5 changed files with 106 additions and 41 deletions
+12 -8
View File
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Threading.Tasks;
using MQTTnet.Core.Diagnostics;
using MQTTnet.Core.Exceptions;
using MQTTnet.Core.Internal;
using MQTTnet.Core.Packets;
using System.Collections.Concurrent;
@@ -22,16 +23,19 @@ namespace MQTTnet.Core.Client
var packetAwaiter = AddPacketAwaiter(request, responseType);
DispatchPendingPackets();
var hasTimeout = await Task.WhenAny(Task.Delay(timeout), packetAwaiter.Task).ConfigureAwait(false) != packetAwaiter.Task;
RemovePacketAwaiter(request, responseType);
if (hasTimeout)
try
{
MqttTrace.Warning(nameof(MqttPacketDispatcher), "Timeout while waiting for packet.");
throw new MqttCommunicationTimedOutException();
return await packetAwaiter.Task.TimeoutAfter( timeout );
}
catch ( MqttCommunicationTimedOutException )
{
MqttTrace.Warning( nameof( MqttPacketDispatcher ), "Timeout while waiting for packet." );
throw;
}
finally
{
RemovePacketAwaiter(request, responseType);
}
return packetAwaiter.Task.Result;
}
public void Dispatch(MqttBasePacket packet)