Implement deferred core utility parity APIs/tests and refresh tracking artifacts

This commit is contained in:
Joseph Doherty
2026-02-27 10:27:05 -05:00
parent c0aaae9236
commit b94a67be6e
17 changed files with 842 additions and 9 deletions

View File

@@ -14,6 +14,8 @@
// Adapted from server/util.go in the NATS server Go source.
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
namespace ZB.MOM.NatsNet.Server.Internal;
@@ -268,6 +270,25 @@ public static class ServerUtilities
return client;
}
/// <summary>
/// Parity wrapper for Go <c>natsDialTimeout</c>.
/// Accepts a network label (tcp/tcp4/tcp6) and host:port address.
/// </summary>
public static Task<System.Net.Sockets.TcpClient> NatsDialTimeout(
string network, string address, TimeSpan timeout)
{
if (!string.Equals(network, "tcp", StringComparison.OrdinalIgnoreCase) &&
!string.Equals(network, "tcp4", StringComparison.OrdinalIgnoreCase) &&
!string.Equals(network, "tcp6", StringComparison.OrdinalIgnoreCase))
throw new NotSupportedException($"unsupported network: {network}");
var (host, port, err) = ParseHostPort(address, defaultPort: 0);
if (err != null || port <= 0)
throw new InvalidOperationException($"invalid dial address: {address}", err);
return NatsDialTimeoutAsync(host, port, timeout);
}
// -------------------------------------------------------------------------
// URL redaction
// -------------------------------------------------------------------------
@@ -337,6 +358,54 @@ public static class ServerUtilities
return result;
}
// -------------------------------------------------------------------------
// RefCountedUrlSet wrappers (Go parity mapping)
// -------------------------------------------------------------------------
/// <summary>
/// Parity wrapper for <see cref="RefCountedUrlSet.AddUrl"/>.
/// Mirrors <c>refCountedUrlSet.addUrl</c>.
/// </summary>
public static bool AddUrl(RefCountedUrlSet urlSet, string urlStr)
{
ArgumentNullException.ThrowIfNull(urlSet);
return urlSet.AddUrl(urlStr);
}
/// <summary>
/// Parity wrapper for <see cref="RefCountedUrlSet.RemoveUrl"/>.
/// Mirrors <c>refCountedUrlSet.removeUrl</c>.
/// </summary>
public static bool RemoveUrl(RefCountedUrlSet urlSet, string urlStr)
{
ArgumentNullException.ThrowIfNull(urlSet);
return urlSet.RemoveUrl(urlStr);
}
/// <summary>
/// Parity wrapper for <see cref="RefCountedUrlSet.GetAsStringSlice"/>.
/// Mirrors <c>refCountedUrlSet.getAsStringSlice</c>.
/// </summary>
public static string[] GetAsStringSlice(RefCountedUrlSet urlSet)
{
ArgumentNullException.ThrowIfNull(urlSet);
return urlSet.GetAsStringSlice();
}
// -------------------------------------------------------------------------
// INFO helpers
// -------------------------------------------------------------------------
/// <summary>
/// Serialises <paramref name="info"/> into an INFO line (<c>INFO {...}\r\n</c>).
/// Mirrors <c>generateInfoJSON</c>.
/// </summary>
public static byte[] GenerateInfoJSON(global::ZB.MOM.NatsNet.Server.ServerInfo info)
{
var json = JsonSerializer.Serialize(info);
return Encoding.UTF8.GetBytes($"INFO {json}\r\n");
}
// -------------------------------------------------------------------------
// Copy helpers
// -------------------------------------------------------------------------
@@ -391,6 +460,13 @@ public static class ServerUtilities
return channel.Writer;
}
/// <summary>
/// Parity wrapper for <see cref="CreateParallelTaskQueue"/>.
/// Mirrors <c>parallelTaskQueue</c>.
/// </summary>
public static System.Threading.Channels.ChannelWriter<Action> ParallelTaskQueue(int maxParallelism = 0) =>
CreateParallelTaskQueue(maxParallelism);
}
// -------------------------------------------------------------------------