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

[Discussion] Cache instances of MqttClientPublishResult #1325

Closed
wants to merge 3 commits into from
Closed
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
23 changes: 21 additions & 2 deletions Source/MQTTnet/Client/Publishing/MqttClientPublishResult.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@


using System;
using System.Collections.Generic;
using MQTTnet.Packets;

namespace MQTTnet.Client.Publishing
{
public class MqttClientPublishResult
public sealed class MqttClientPublishResult : IDisposable
{
[ThreadStatic]
private static MqttClientPublishResult t_cache;

public ushort? PacketIdentifier { get; set; }

/// <summary>
Expand All @@ -28,5 +32,20 @@ public class MqttClientPublishResult
/// Hint: MQTT 5 feature only.
/// </summary>
public List<MqttUserProperty> UserProperties { get; set; }

void IDisposable.Dispose()
{
PacketIdentifier = null;
ReasonCode = default;
ReasonString = null;
UserProperties = null;

t_cache = this;
}

internal static MqttClientPublishResult GetInstance()
{
return t_cache ?? new MqttClientPublishResult();
}
}
}
30 changes: 15 additions & 15 deletions Source/MQTTnet/Formatter/V3/MqttV310DataConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using MQTTnet.Client.Publishing;
Expand Down Expand Up @@ -251,28 +251,28 @@ public MqttDisconnectPacket CreateDisconnectPacket(MqttClientDisconnectOptions o

public MqttClientPublishResult CreateClientPublishResult(MqttPubAckPacket pubAckPacket)
{
return new MqttClientPublishResult
{
PacketIdentifier = pubAckPacket?.PacketIdentifier,
ReasonCode = MqttClientPublishReasonCode.Success
};
var mqttClientPublishResult = MqttClientPublishResult.GetInstance();
mqttClientPublishResult.PacketIdentifier = pubAckPacket?.PacketIdentifier;
mqttClientPublishResult.ReasonCode = MqttClientPublishReasonCode.Success;

return mqttClientPublishResult;
}

public MqttClientPublishResult CreateClientPublishResult(MqttPubRecPacket pubRecPacket, MqttPubCompPacket pubCompPacket)
{
var mqttClientPublishResult = MqttClientPublishResult.GetInstance();

if (pubRecPacket == null || pubCompPacket == null)
{
return new MqttClientPublishResult
{
ReasonCode = MqttClientPublishReasonCode.UnspecifiedError
};
mqttClientPublishResult.ReasonCode = MqttClientPublishReasonCode.UnspecifiedError;
}

return new MqttClientPublishResult
else
{
PacketIdentifier = pubCompPacket.PacketIdentifier,
ReasonCode = MqttClientPublishReasonCode.Success
};
mqttClientPublishResult.PacketIdentifier = pubCompPacket.PacketIdentifier;
mqttClientPublishResult.ReasonCode = MqttClientPublishReasonCode.Success;
}

return mqttClientPublishResult;
}
}
}
44 changes: 19 additions & 25 deletions Source/MQTTnet/Formatter/V5/MqttV500DataConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using MQTTnet.Client.Publishing;
Expand Down Expand Up @@ -308,12 +308,10 @@ public MqttDisconnectPacket CreateDisconnectPacket(MqttClientDisconnectOptions o

public MqttClientPublishResult CreateClientPublishResult(MqttPubAckPacket pubAckPacket)
{
var result = new MqttClientPublishResult
{
ReasonCode = MqttClientPublishReasonCode.Success,
ReasonString = pubAckPacket?.Properties?.ReasonString,
UserProperties = pubAckPacket?.Properties?.UserProperties
};
var result = MqttClientPublishResult.GetInstance();
result.ReasonCode = MqttClientPublishReasonCode.Success;
result.ReasonString = pubAckPacket?.Properties?.ReasonString;
result.UserProperties = pubAckPacket?.Properties?.UserProperties;

if (pubAckPacket != null)
{
Expand All @@ -329,33 +327,29 @@ public MqttClientPublishResult CreateClientPublishResult(MqttPubAckPacket pubAck

public MqttClientPublishResult CreateClientPublishResult(MqttPubRecPacket pubRecPacket, MqttPubCompPacket pubCompPacket)
{
var result = MqttClientPublishResult.GetInstance();

if (pubRecPacket == null || pubCompPacket == null)
{
return new MqttClientPublishResult
{
ReasonCode = MqttClientPublishReasonCode.UnspecifiedError
};
result.ReasonCode = MqttClientPublishReasonCode.UnspecifiedError;
return result;
}

// The PUBCOMP is the last packet in QoS 2. So we use the results from that instead of PUBREC.
if (pubCompPacket.ReasonCode == MqttPubCompReasonCode.PacketIdentifierNotFound)
{
return new MqttClientPublishResult
{
PacketIdentifier = pubCompPacket.PacketIdentifier,
ReasonCode = MqttClientPublishReasonCode.UnspecifiedError,
ReasonString = pubCompPacket.Properties?.ReasonString,
UserProperties = pubCompPacket.Properties?.UserProperties
};
result.PacketIdentifier = pubCompPacket.PacketIdentifier;
result.ReasonCode = MqttClientPublishReasonCode.UnspecifiedError;
result.ReasonString = pubCompPacket.Properties?.ReasonString;
result.UserProperties = pubCompPacket.Properties?.UserProperties;

return result;
}

var result = new MqttClientPublishResult
{
PacketIdentifier = pubCompPacket.PacketIdentifier,
ReasonCode = MqttClientPublishReasonCode.Success,
ReasonString = pubCompPacket.Properties?.ReasonString,
UserProperties = pubCompPacket.Properties?.UserProperties
};
result.PacketIdentifier = pubCompPacket.PacketIdentifier;
result.ReasonCode = MqttClientPublishReasonCode.Success;
result.ReasonString = pubCompPacket.Properties?.ReasonString;
result.UserProperties = pubCompPacket.Properties?.UserProperties;

if (pubRecPacket.ReasonCode.HasValue)
{
Expand Down
6 changes: 4 additions & 2 deletions Source/MQTTnet/Server/MqttServer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using MQTTnet.Adapter;
using MQTTnet.Adapter;
using MQTTnet.Client.Publishing;
using MQTTnet.Client.Receiving;
using MQTTnet.Diagnostics;
Expand All @@ -19,6 +19,8 @@ namespace MQTTnet.Server
{
public class MqttServer : Disposable, IMqttServer
{
static readonly Task<MqttClientPublishResult> s_completedClientPublishResult = Task.FromResult(new MqttClientPublishResult());

readonly MqttServerEventDispatcher _eventDispatcher;
readonly ICollection<IMqttServerAdapter> _adapters;
readonly IMqttNetLogger _rootLogger;
Expand Down Expand Up @@ -150,7 +152,7 @@ public Task<MqttClientPublishResult> PublishAsync(MqttApplicationMessage applica

_clientSessionsManager.DispatchApplicationMessage(applicationMessage, null);

return Task.FromResult(new MqttClientPublishResult());
return s_completedClientPublishResult;
}

public async Task StartAsync(IMqttServerOptions options)
Expand Down