feat(batch18): implement group-a server core helpers

This commit is contained in:
Joseph Doherty
2026-02-28 19:17:53 -05:00
parent e16d7ffab3
commit f8d384711d
7 changed files with 308 additions and 10 deletions
@@ -178,7 +178,13 @@ public sealed partial class NatsServer
/// Returns true if the goroutine was started, false if the server is already stopped.
/// Mirrors Go <c>Server.startGoRoutine(f)</c>.
/// </summary>
internal bool StartGoRoutine(Action f)
internal bool StartGoRoutine(Action f) => StartGoRoutine(f, []);
/// <summary>
/// Starts a background Task with goroutine labels.
/// Mirrors Go <c>Server.startGoRoutine(f, tags...)</c>.
/// </summary>
internal bool StartGoRoutine(Action f, params IReadOnlyDictionary<string, string>[] tags)
{
lock (_grMu)
{
@@ -186,13 +192,41 @@ public sealed partial class NatsServer
_grWg.Add(1);
Task.Run(() =>
{
try { f(); }
try
{
SetGoRoutineLabels(tags);
f();
}
finally { _grWg.Done(); }
});
return true;
}
}
/// <summary>
/// Optional test-only sink used to observe goroutine labels during unit tests.
/// </summary>
internal static Action<IReadOnlyList<KeyValuePair<string, string>>>? SetGoRoutineLabelsHookForTest { get; set; }
/// <summary>
/// Sets goroutine labels for diagnostics when tags are present.
/// Mirrors Go <c>setGoRoutineLabels</c>.
/// </summary>
internal static void SetGoRoutineLabels(params IReadOnlyDictionary<string, string>[] tags)
{
var labels = new List<KeyValuePair<string, string>>();
foreach (var tag in tags)
{
foreach (var pair in tag)
{
labels.Add(new KeyValuePair<string, string>(pair.Key, pair.Value));
}
}
if (labels.Count > 0)
SetGoRoutineLabelsHookForTest?.Invoke(labels);
}
// =========================================================================
// Client / connection management (features 30813084)
// =========================================================================