mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-19 02:53:06 +08:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user