fix: make subscriberIds private & check for add failure

This commit is contained in:
Andrew Welker
2025-11-03 15:19:17 -06:00
parent 9be5823956
commit edc10a9c2a

View File

@@ -31,7 +31,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
/// <remarks> /// <remarks>
/// Unsoliciited feedback from a device in a messenger will ONLY be sent to devices in this subscription list. When a client disconnects, it's ID will be removed from the collection. /// Unsoliciited feedback from a device in a messenger will ONLY be sent to devices in this subscription list. When a client disconnects, it's ID will be removed from the collection.
/// </remarks> /// </remarks>
protected HashSet<string> SubscriberIds = new HashSet<string>(); private readonly HashSet<string> subscriberIds = new HashSet<string>();
/// <summary> /// <summary>
/// Lock object for thread-safe access to SubscriberIds /// Lock object for thread-safe access to SubscriberIds
@@ -200,13 +200,11 @@ namespace PepperDash.Essentials.AppServer.Messengers
lock (_subscriberLock) lock (_subscriberLock)
{ {
if (SubscriberIds.Contains(clientId)) if (!subscriberIds.Add(clientId))
{ {
this.LogVerbose("Client {clientId} already subscribed", clientId); this.LogVerbose("Client {clientId} already subscribed", clientId);
return; return;
} }
SubscriberIds.Add(clientId);
} }
this.LogDebug("Client {clientId} subscribed", clientId); this.LogDebug("Client {clientId} subscribed", clientId);
@@ -227,10 +225,10 @@ namespace PepperDash.Essentials.AppServer.Messengers
bool wasSubscribed; bool wasSubscribed;
lock (_subscriberLock) lock (_subscriberLock)
{ {
wasSubscribed = SubscriberIds.Contains(clientId); wasSubscribed = subscriberIds.Contains(clientId);
if (wasSubscribed) if (wasSubscribed)
{ {
SubscriberIds.Remove(clientId); subscriberIds.Remove(clientId);
} }
} }
@@ -332,7 +330,7 @@ namespace PepperDash.Essentials.AppServer.Messengers
List<string> subscriberSnapshot; List<string> subscriberSnapshot;
lock (_subscriberLock) lock (_subscriberLock)
{ {
subscriberSnapshot = new List<string>(SubscriberIds); subscriberSnapshot = new List<string>(subscriberIds);
} }
foreach (var client in subscriberSnapshot) foreach (var client in subscriberSnapshot)