feat(batch24): complete leaf nodes implementation and verification
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user