feat(batch24): complete leaf nodes implementation and verification

This commit is contained in:
Joseph Doherty
2026-03-01 01:31:57 -05:00
parent ff7e674ec4
commit 3e9ad16033
36 changed files with 3427 additions and 21 deletions

View File

@@ -1445,7 +1445,7 @@ public sealed partial class Account : INatsAccount
_mappings.Add(m);
Interlocked.Exchange(ref _hasMapped, _mappings.Count > 0 ? 1 : 0);
// TODO: session 15 — notify connected leaf nodes via lc.ForceAddToSmap(src).
UpdateLeafNodesEx(src, 1, force: true);
return null;
}
@@ -1474,7 +1474,7 @@ public sealed partial class Account : INatsAccount
_mappings.RemoveAt(_mappings.Count - 1);
Interlocked.Exchange(ref _hasMapped, _mappings.Count > 0 ? 1 : 0);
// TODO: session 15 — notify leaf nodes via lc.ForceRemoveFromSmap(src).
UpdateLeafNodesEx(src, -1, force: true);
return true;
}
}
@@ -3810,6 +3810,52 @@ public sealed partial class Account : INatsAccount
leaf.FlushSignal();
}
internal void UpdateLeafNodesEx(string subject, int delta, bool force = false)
{
if (string.IsNullOrWhiteSpace(subject) || delta == 0)
return;
var heldWriteLock = _mu.IsWriteLockHeld;
if (!heldWriteLock)
_mu.EnterWriteLock();
try
{
_rm ??= new Dictionary<string, int>(StringComparer.Ordinal);
_rm.TryGetValue(subject, out var interest);
interest += delta;
if (interest <= 0)
_rm.Remove(subject);
else
_rm[subject] = interest;
}
finally
{
if (!heldWriteLock)
_mu.ExitWriteLock();
}
List<ClientConnection> leafs;
_lmu.EnterReadLock();
try { leafs = [.. _lleafs]; }
finally { _lmu.ExitReadLock(); }
foreach (var leaf in leafs)
{
if (force)
{
if (delta > 0)
leaf.ForceAddToSmap(subject);
else
leaf.ForceRemoveFromSmap(subject);
}
else
{
leaf.FlushSignal();
}
}
}
// -------------------------------------------------------------------------
// addClient / removeClient
// -------------------------------------------------------------------------
@@ -3889,7 +3935,14 @@ public sealed partial class Account : INatsAccount
// Cluster accounting for hub leaf nodes.
if (c.IsHubLeafNode())
{
// TODO: session 15 — c.RemoteCluster() for cluster accounting.
var cluster = c.RemoteCluster();
if (!string.IsNullOrWhiteSpace(cluster) && _leafClusters != null && _leafClusters.TryGetValue(cluster, out var current))
{
if (current <= 1)
_leafClusters.Remove(cluster);
else
_leafClusters[cluster] = current - 1;
}
}
}
}