From 1daa25d0f954cb09f86ae22f7e8957cf5097237b Mon Sep 17 00:00:00 2001 From: Wei Zhao Date: Wed, 23 Oct 2024 14:17:04 +0800 Subject: [PATCH] Issues #2091 Improve the performance of RetainedMessages without subscribing to RetainedMessageChangedEvent. --- .../MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs b/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs index 8995b35f..6d2e288f 100644 --- a/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs +++ b/Source/MQTTnet.Server/Internal/MqttRetainedMessagesManager.cs @@ -62,6 +62,7 @@ public async Task UpdateMessage(string clientId, MqttApplicationMessage applicat { List messagesForSave = null; var saveIsRequired = false; + var hasHandlers = _eventContainer.RetainedMessageChangedEvent.HasHandlers; lock (_messages) { @@ -93,13 +94,13 @@ public async Task UpdateMessage(string clientId, MqttApplicationMessage applicat _logger.Verbose("Client '{0}' set retained message for topic '{1}'.", clientId, applicationMessage.Topic); } - if (saveIsRequired) + if (saveIsRequired && hasHandlers) { messagesForSave = new List(_messages.Values); } } - if (saveIsRequired) + if (saveIsRequired && hasHandlers) { using (await _storageAccessLock.EnterAsync().ConfigureAwait(false)) {