feat(cluster): add implicit route and gateway discovery via INFO gossip

Implements ProcessImplicitRoute and ForwardNewRouteInfoToKnownServers on RouteManager,
and ProcessImplicitGateway on GatewayManager, mirroring Go server/route.go and
server/gateway.go INFO gossip-based peer discovery. Adds ConnectUrls to ServerInfo
and introduces GatewayInfo model. 12 new unit tests in ImplicitDiscoveryTests.
This commit is contained in:
Joseph Doherty
2026-02-25 03:05:35 -05:00
parent e09835ca70
commit bfe7a71fcd
5 changed files with 337 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
namespace NATS.Server.Gateways;
/// <summary>
/// Information about a remote gateway cluster received during implicit discovery.
/// Go reference: server/gateway.go — implicit gateway discovery via INFO gossip.
/// </summary>
public sealed record GatewayInfo
{
/// <summary>Name of the remote gateway cluster.</summary>
public required string Name { get; init; }
/// <summary>URLs for connecting to the remote gateway cluster.</summary>
public required string[] Urls { get; init; }
}

View File

@@ -16,6 +16,7 @@ public sealed class GatewayManager : IAsyncDisposable
private readonly Action<GatewayMessage> _messageSink;
private readonly ILogger<GatewayManager> _logger;
private readonly ConcurrentDictionary<string, GatewayConnection> _connections = new(StringComparer.Ordinal);
private readonly HashSet<string> _discoveredGateways = new(StringComparer.OrdinalIgnoreCase);
private long _forwardedJetStreamClusterMessages;
private CancellationTokenSource? _cts;
@@ -44,6 +45,29 @@ public sealed class GatewayManager : IAsyncDisposable
_logger = logger;
}
/// <summary>
/// Gateway clusters auto-discovered via INFO gossip.
/// Go reference: server/gateway.go processImplicitGateway.
/// </summary>
public IReadOnlyCollection<string> DiscoveredGateways
{
get { lock (_discoveredGateways) return _discoveredGateways.ToList(); }
}
/// <summary>
/// Processes a gateway info message from a peer, discovering new gateway clusters.
/// Go reference: server/gateway.go:800-850 (processImplicitGateway).
/// </summary>
public void ProcessImplicitGateway(GatewayInfo gwInfo)
{
ArgumentNullException.ThrowIfNull(gwInfo);
lock (_discoveredGateways)
{
_discoveredGateways.Add(gwInfo.Name);
}
}
public Task StartAsync(CancellationToken ct)
{
_cts = CancellationTokenSource.CreateLinkedTokenSource(ct);