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:
14
src/NATS.Server/Gateways/GatewayInfo.cs
Normal file
14
src/NATS.Server/Gateways/GatewayInfo.cs
Normal 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; }
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user