feat: add INatsClient interface and implement on NatsClient
Extract INatsClient interface from NatsClient to enable internal clients (SYSTEM, ACCOUNT) to participate in the subscription system without requiring a socket connection. Change Subscription.Client from concrete NatsClient to INatsClient, keeping IMessageRouter and RemoveClient using the concrete type since only socket clients need those paths.
This commit is contained in:
19
src/NATS.Server/INatsClient.cs
Normal file
19
src/NATS.Server/INatsClient.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using NATS.Server.Auth;
|
||||||
|
using NATS.Server.Protocol;
|
||||||
|
|
||||||
|
namespace NATS.Server;
|
||||||
|
|
||||||
|
public interface INatsClient
|
||||||
|
{
|
||||||
|
ulong Id { get; }
|
||||||
|
ClientKind Kind { get; }
|
||||||
|
bool IsInternal => Kind.IsInternal();
|
||||||
|
Account? Account { get; }
|
||||||
|
ClientOptions? ClientOpts { get; }
|
||||||
|
ClientPermissions? Permissions { get; }
|
||||||
|
|
||||||
|
void SendMessage(string subject, string sid, string? replyTo,
|
||||||
|
ReadOnlyMemory<byte> headers, ReadOnlyMemory<byte> payload);
|
||||||
|
bool QueueOutbound(ReadOnlyMemory<byte> data);
|
||||||
|
void RemoveSubscription(string sid);
|
||||||
|
}
|
||||||
@@ -26,7 +26,7 @@ public interface ISubListAccess
|
|||||||
SubList SubList { get; }
|
SubList SubList { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public sealed class NatsClient : IDisposable
|
public sealed class NatsClient : INatsClient, IDisposable
|
||||||
{
|
{
|
||||||
private readonly Socket _socket;
|
private readonly Socket _socket;
|
||||||
private readonly Stream _stream;
|
private readonly Stream _stream;
|
||||||
@@ -45,6 +45,7 @@ public sealed class NatsClient : IDisposable
|
|||||||
private readonly ServerStats _serverStats;
|
private readonly ServerStats _serverStats;
|
||||||
|
|
||||||
public ulong Id { get; }
|
public ulong Id { get; }
|
||||||
|
public ClientKind Kind => ClientKind.Client;
|
||||||
public ClientOptions? ClientOpts { get; private set; }
|
public ClientOptions? ClientOpts { get; private set; }
|
||||||
public IMessageRouter? Router { get; set; }
|
public IMessageRouter? Router { get; set; }
|
||||||
public Account? Account { get; private set; }
|
public Account? Account { get; private set; }
|
||||||
|
|||||||
@@ -9,5 +9,5 @@ public sealed class Subscription
|
|||||||
public required string Sid { get; init; }
|
public required string Sid { get; init; }
|
||||||
public long MessageCount; // Interlocked
|
public long MessageCount; // Interlocked
|
||||||
public long MaxMessages; // 0 = unlimited
|
public long MaxMessages; // 0 = unlimited
|
||||||
public NatsClient? Client { get; set; }
|
public INatsClient? Client { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,4 +14,16 @@ public class InternalClientTests
|
|||||||
{
|
{
|
||||||
kind.IsInternal().ShouldBe(expected);
|
kind.IsInternal().ShouldBe(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NatsClient_implements_INatsClient()
|
||||||
|
{
|
||||||
|
typeof(NatsClient).GetInterfaces().ShouldContain(typeof(INatsClient));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NatsClient_kind_is_Client()
|
||||||
|
{
|
||||||
|
typeof(NatsClient).GetProperty("Kind")!.PropertyType.ShouldBe(typeof(ClientKind));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user