// Copyright 2018-2026 The NATS Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Adapted from server/accounts.go in the NATS server Go source. using ZB.MOM.NatsNet.Server.Auth; using ZB.MOM.NatsNet.Server.Internal; using ZB.MOM.NatsNet.Server.Internal.DataStructures; using System.Text; namespace ZB.MOM.NatsNet.Server; // ============================================================================ // Account — full implementation // Mirrors Go `Account` struct in server/accounts.go lines 52-119. // ============================================================================ /// /// Represents a NATS account, tracking clients, subscriptions, imports, exports, /// and subject mappings. Implements so that /// can interact with it without a hard dependency. /// Mirrors Go Account struct in server/accounts.go. /// public sealed class Account : INatsAccount { // ------------------------------------------------------------------------- // Constants // ------------------------------------------------------------------------- /// /// jwt.NoLimit equivalent: -1 means no limit applied. /// private const int NoLimit = -1; // ------------------------------------------------------------------------- // Identity fields // ------------------------------------------------------------------------- /// Account name. Mirrors Go Name string. public string Name { get; set; } = string.Empty; /// NKey public key. Mirrors Go Nkey string. public string Nkey { get; set; } = string.Empty; /// JWT issuer key. Mirrors Go Issuer string. public string Issuer { get; set; } = string.Empty; /// Raw JWT claim string. Mirrors Go claimJWT string. internal string ClaimJwt { get; set; } = string.Empty; /// Time of last update from resolver. Mirrors Go updated time.Time. internal DateTime Updated { get; set; } // ------------------------------------------------------------------------- // Locks // ------------------------------------------------------------------------- /// Primary account read/write lock. Mirrors Go mu sync.RWMutex. private readonly ReaderWriterLockSlim _mu = new(LockRecursionPolicy.NoRecursion); /// Send-queue mutex. Mirrors Go sqmu sync.Mutex. private readonly object _sqmu = new(); /// Leaf-node list lock. Mirrors Go lmu sync.RWMutex. private readonly ReaderWriterLockSlim _lmu = new(LockRecursionPolicy.NoRecursion); /// Event ID mutex. Mirrors Go eventIdsMu sync.Mutex. private readonly object _eventIdsMu = new(); /// JetStream migration/clear-observer mutex. Mirrors Go jscmMu sync.Mutex. private readonly object _jscmMu = new(); // ------------------------------------------------------------------------- // Subscription index // ------------------------------------------------------------------------- /// /// Subscription trie for this account. Mirrors Go sl *Sublist. /// Set by the server when the account is registered. /// internal SubscriptionIndex? Sublist { get; set; } // ------------------------------------------------------------------------- // Internal client and send queue (stubs) // ------------------------------------------------------------------------- /// /// Internal account client. Mirrors Go ic *client. /// TODO: session 12 — full internal client wiring. /// internal ClientConnection? InternalClient { get; set; } /// /// Per-account send queue. Mirrors Go sq *sendq. /// internal SendQueue? SendQueue { get; set; } internal SendQueue? GetSendQueue() { lock (_sqmu) { return SendQueue; } } internal void SetSendQueue(SendQueue? sendQueue) { lock (_sqmu) { SendQueue = sendQueue; } } // ------------------------------------------------------------------------- // Eventing timers // ------------------------------------------------------------------------- /// Expiration timer. Mirrors Go etmr *time.Timer. private Timer? _etmr; /// Connection-count timer. Mirrors Go ctmr *time.Timer. private Timer? _ctmr; // ------------------------------------------------------------------------- // Remote server tracking // ------------------------------------------------------------------------- /// /// Per-server connection and leaf-node counts. /// Key is server ID. Mirrors Go strack map[string]sconns. /// private Dictionary? _strack; /// /// Remote client count (sum of strack[*].Conns). /// Mirrors Go nrclients int32. /// Protected by . /// private int _nrclients; /// /// System client count. /// Mirrors Go sysclients int32. /// Protected by . /// private int _sysclients; /// /// Local leaf-node count. /// Mirrors Go nleafs int32. /// Protected by . /// private int _nleafs; /// /// Remote leaf-node count (sum of strack[*].Leafs). /// Mirrors Go nrleafs int32. /// Protected by . /// private int _nrleafs; // ------------------------------------------------------------------------- // Client set // ------------------------------------------------------------------------- /// /// Active local clients. Mirrors Go clients map[*client]struct{}. /// Protected by . /// private HashSet? _clients; // ------------------------------------------------------------------------- // Route and leaf-queue maps // ------------------------------------------------------------------------- /// /// Route map: subject → reference count. /// Mirrors Go rm map[string]int32. /// Protected by . /// private Dictionary? _rm; /// /// Leaf queue weights: subject → weight. /// Mirrors Go lqws map[string]int32. /// Protected by . /// private Dictionary? _lqws; // ------------------------------------------------------------------------- // User revocations // ------------------------------------------------------------------------- /// /// Revoked user nkeys: key → revocation timestamp (Unix seconds). /// Mirrors Go usersRevoked map[string]int64. /// Protected by . /// internal Dictionary? UsersRevoked { get; set; } // ------------------------------------------------------------------------- // Subject mappings // ------------------------------------------------------------------------- /// /// Ordered list of subject mappings. Mirrors Go mappings []*mapping. /// Protected by . /// private List _mappings = []; /// /// Atomic flag: 1 when is non-empty. /// Mirrors Go hasMapped atomic.Bool. /// private int _hasMapped; // 0 = false, 1 = true // ------------------------------------------------------------------------- // Leaf nodes // ------------------------------------------------------------------------- /// /// Ordered list of local leaf-node clients. /// Mirrors Go lleafs []*client. /// Protected by . /// private List _lleafs = []; /// /// Cluster name → count of leaf-node connections from that cluster. /// Mirrors Go leafClusters map[string]uint64. /// Protected by . /// private Dictionary? _leafClusters; // ------------------------------------------------------------------------- // Import / export maps // ------------------------------------------------------------------------- /// Import tracking. Mirrors Go imports importMap. internal ImportMap Imports { get; set; } = new(); /// Export tracking. Mirrors Go exports exportMap. internal ExportMap Exports { get; set; } = new(); // ------------------------------------------------------------------------- // JetStream (stubs) // ------------------------------------------------------------------------- /// /// JetStream account state. Mirrors Go js *jsAccount. /// TODO: session 19 — JetStream implementation. /// internal object? JetStream { get; set; } /// /// Per-domain JetStream limits. Mirrors Go jsLimits map[string]JetStreamAccountLimits. /// TODO: session 19 — JetStream implementation. /// internal Dictionary? JetStreamLimits { get; set; } // ------------------------------------------------------------------------- // Misc identity fields // ------------------------------------------------------------------------- /// Non-routed gateway account name. Mirrors Go nrgAccount string. internal string NrgAccount { get; set; } = string.Empty; // ------------------------------------------------------------------------- // Limits (embedded `limits` struct in Go) // ------------------------------------------------------------------------- /// /// Maximum payload size (-1 = unlimited). Mirrors Go embedded limits.mpay int32. /// internal int MaxPayload { get; set; } = NoLimit; /// /// Maximum subscriptions (-1 = unlimited). Mirrors Go embedded limits.msubs int32. /// internal int MaxSubscriptions { get; set; } = NoLimit; /// /// Maximum connections (-1 = unlimited). Mirrors Go embedded limits.mconns int32. /// internal int MaxConnections { get; set; } = NoLimit; /// /// Maximum leaf nodes (-1 = unlimited). Mirrors Go embedded limits.mleafs int32. /// internal int MaxLeafNodes { get; set; } = NoLimit; /// /// When true, bearer tokens are not allowed. /// Mirrors Go embedded limits.disallowBearer bool. /// internal bool DisallowBearer { get; set; } // ------------------------------------------------------------------------- // Expiration (atomic) // ------------------------------------------------------------------------- /// /// 1 when the account JWT has expired. Mirrors Go expired atomic.Bool. /// private int _expired; // 0 = not expired, 1 = expired // ------------------------------------------------------------------------- // Miscellaneous // ------------------------------------------------------------------------- /// /// When true, this account's config could not be fully resolved. /// Mirrors Go incomplete bool. /// internal bool Incomplete { get; set; } /// /// Signing keys for JWT validation. /// Mirrors Go signingKeys map[string]jwt.Scope. /// Value is object? because JWT Scope is not yet fully ported. /// internal Dictionary? SigningKeys { get; set; } /// /// External authorization configuration stub. /// Mirrors Go extAuth *jwt.ExternalAuthorization. /// TODO: session 11 — JWT full integration. /// internal object? ExternalAuth { get; set; } /// /// The server this account is registered with, or null if not yet registered. /// Stored as object? to avoid circular reference. /// Mirrors Go srv *Server. /// internal object? Server { get; set; } /// /// Loop detection subject for leaf nodes. /// Mirrors Go lds string. /// internal string LoopDetectionSubject { get; set; } = string.Empty; /// /// Service reply prefix (wildcard subscription root). /// Mirrors Go siReply []byte. /// internal byte[]? ServiceImportReply { get; set; } /// /// Subscription ID counter for internal use. /// Mirrors Go isid uint64. /// private ulong _isid; /// /// Default permissions for users with no explicit permissions. /// Mirrors Go defaultPerms *Permissions. /// internal Permissions? DefaultPerms { get; set; } /// /// Account tags from JWT. Mirrors Go tags jwt.TagList. /// Stored as string array pending full JWT integration. /// internal string[] Tags { get; set; } = []; /// /// Human-readable name tag (distinct from ). /// Mirrors Go nameTag string. /// internal string NameTag { get; set; } = string.Empty; /// /// Unix-nanosecond timestamp of last max-subscription-limit log. /// Mirrors Go lastLimErr int64. /// private long _lastLimErr; /// /// Route pool index (-1 = dedicated, -2 = transitioning, ≥ 0 = shared). /// Mirrors Go routePoolIdx int. /// internal int RoutePoolIdx { get; set; } /// /// Message-tracing destination subject. /// Mirrors Go traceDest string. /// Protected by . /// private string _traceDest = string.Empty; /// /// Tracing sampling percentage (0 = header-triggered, 1-100 = rate). /// Mirrors Go traceDestSampling int. /// Protected by . /// private int _traceDestSampling; /// /// Sets account-level message trace destination subject. /// Mirrors writes to Go acc.traceDest during config parsing. /// internal void SetMessageTraceDestination(string subject) { _mu.EnterWriteLock(); try { _traceDest = subject ?? string.Empty; } finally { _mu.ExitWriteLock(); } } /// /// Returns account-level message trace destination subject. /// Mirrors reads of Go acc.traceDest during config parsing. /// internal string GetMessageTraceDestination() { _mu.EnterReadLock(); try { return _traceDest; } finally { _mu.ExitReadLock(); } } /// /// Sets account-level message trace sampling percentage. /// Mirrors writes to Go acc.traceDestSampling during config parsing. /// internal void SetMessageTraceSampling(int sampling) { _mu.EnterWriteLock(); try { _traceDestSampling = sampling; } finally { _mu.ExitWriteLock(); } } /// /// Returns account-level message trace sampling percentage. /// Mirrors reads of Go acc.traceDestSampling during config parsing. /// internal int GetMessageTraceSampling() { _mu.EnterReadLock(); try { return _traceDestSampling; } finally { _mu.ExitReadLock(); } } /// /// Sets account-level message trace destination subject. /// Mirrors Go (a *Account) setTraceDest(dest string). /// internal void SetTraceDest(string dest) => SetMessageTraceDestination(dest); /// /// Returns trace destination and sampling. /// Mirrors Go (a *Account) getTraceDestAndSampling() (string, int). /// internal (string Destination, int Sampling) GetTraceDestAndSampling() { _mu.EnterReadLock(); try { return (_traceDest, _traceDestSampling); } finally { _mu.ExitReadLock(); } } // ------------------------------------------------------------------------- // Factory // ------------------------------------------------------------------------- /// /// Creates a new unlimited account with the given name. /// Mirrors Go NewAccount(name string) *Account. /// public static Account NewAccount(string name) => new() { Name = name, MaxPayload = NoLimit, MaxSubscriptions = NoLimit, MaxConnections = NoLimit, MaxLeafNodes = NoLimit, }; // ------------------------------------------------------------------------- // Object overrides // ------------------------------------------------------------------------- /// /// Returns the account name. Mirrors Go (a *Account) String() string. /// public override string ToString() => Name; /// /// Returns the account name. /// Mirrors Go (a *Account) String() string. /// public string String() => Name; // ------------------------------------------------------------------------- // Shallow copy for config reload // ------------------------------------------------------------------------- /// /// Copies identity and config fields from the options-struct account (a) /// into the live server account (na). The write lock on na must /// be held by the caller; this (the options account) requires no lock. /// Mirrors Go (a *Account) shallowCopy(na *Account). /// internal void ShallowCopy(Account na) { na.Nkey = Nkey; na.Issuer = Issuer; na._traceDest = _traceDest; na._traceDestSampling = _traceDestSampling; na.NrgAccount = NrgAccount; // Stream imports — shallow-clone each entry. if (Imports.Streams != null) { na.Imports.Streams = new List(Imports.Streams.Count); foreach (var si in Imports.Streams) { // Struct-style shallow copy via record-style clone. na.Imports.Streams.Add(new StreamImportEntry { Account = si.Account, From = si.From, To = si.To, Transform = si.Transform, ReverseTransform = si.ReverseTransform, Claim = si.Claim, UsePublishedSubject = si.UsePublishedSubject, Invalid = si.Invalid, AllowTrace = si.AllowTrace, }); } } // Service imports — shallow-clone each inner list. if (Imports.Services != null) { na.Imports.Services = new Dictionary>(Imports.Services.Count); foreach (var (k, list) in Imports.Services) { var cloned = new List(list.Count); foreach (var si in list) { cloned.Add(new ServiceImportEntry { Account = si.Account, Claim = si.Claim, ServiceExport = si.ServiceExport, SubscriptionId = si.SubscriptionId, From = si.From, To = si.To, Transform = si.Transform, Timestamp = si.Timestamp, ResponseType = si.ResponseType, Latency = si.Latency, M1 = si.M1, RequestingClient = si.RequestingClient, UsePublishedSubject = si.UsePublishedSubject, IsResponse = si.IsResponse, Invalid = si.Invalid, Share = si.Share, Tracking = si.Tracking, DidDeliver = si.DidDeliver, AllowTrace = si.AllowTrace, TrackingHeader = si.TrackingHeader, }); } na.Imports.Services[k] = cloned; } } // Stream exports — shallow-clone each entry. if (Exports.Streams != null) { na.Exports.Streams = new Dictionary(Exports.Streams.Count); foreach (var (k, se) in Exports.Streams) { na.Exports.Streams[k] = se == null ? null! : new StreamExport { TokenRequired = se.TokenRequired, AccountPosition = se.AccountPosition, Approved = se.Approved, ActivationsRevoked = se.ActivationsRevoked, }; } } // Service exports — shallow-clone each entry. if (Exports.Services != null) { na.Exports.Services = new Dictionary(Exports.Services.Count); foreach (var (k, se) in Exports.Services) { na.Exports.Services[k] = se == null ? null! : new ServiceExportEntry { Account = se.Account, ResponseType = se.ResponseType, Latency = se.Latency, ResponseTimer = se.ResponseTimer, ResponseThreshold = se.ResponseThreshold, AllowTrace = se.AllowTrace, TokenRequired = se.TokenRequired, AccountPosition = se.AccountPosition, Approved = se.Approved, ActivationsRevoked = se.ActivationsRevoked, }; } } // Mappings and limits — copy by reference / value. na._mappings = _mappings; Interlocked.Exchange(ref na._hasMapped, _mappings.Count > 0 ? 1 : 0); // JetStream limits — shared reference. // TODO: session 19 — deep copy JetStream limits when ported. na.JetStreamLimits = JetStreamLimits; // Server-config account limits. na.MaxPayload = MaxPayload; na.MaxSubscriptions = MaxSubscriptions; na.MaxConnections = MaxConnections; na.MaxLeafNodes = MaxLeafNodes; na.DisallowBearer = DisallowBearer; } // ------------------------------------------------------------------------- // Event ID generation // ------------------------------------------------------------------------- /// /// Generates a unique event identifier using its own dedicated lock. /// Mirrors Go (a *Account) nextEventID() string. /// internal string NextEventId() { lock (_eventIdsMu) { return Guid.NewGuid().ToString("N"); } } // ------------------------------------------------------------------------- // Client accessors // ------------------------------------------------------------------------- /// /// Returns a snapshot list of clients. Lock must be held by the caller. /// Mirrors Go (a *Account) getClientsLocked() []*client. /// internal List GetClientsLocked() { if (_clients == null || _clients.Count == 0) return []; return [.. _clients]; } /// /// Returns a thread-safe snapshot list of clients. /// Mirrors Go (a *Account) getClients() []*client. /// internal List GetClients() { _mu.EnterReadLock(); try { return GetClientsLocked(); } finally { _mu.ExitReadLock(); } } /// /// Returns a snapshot of non-internal clients. Lock must be held by the caller. /// Mirrors Go (a *Account) getExternalClientsLocked() []*client. /// internal List GetExternalClientsLocked() { if (_clients == null || _clients.Count == 0) return []; var result = new List(_clients.Count); foreach (var c in _clients) { if (!IsInternalClientKind(c.Kind)) result.Add(c); } return result; } // ------------------------------------------------------------------------- // Remote server tracking // ------------------------------------------------------------------------- /// /// Updates the remote-server tracking table for this account based on an /// incoming message, and returns the set of /// local clients that must be disconnected because a connection limit has /// been exceeded (after accounting for remote connections). /// Mirrors Go (a *Account) updateRemoteServer(m *AccountNumConns) []*client. /// internal List UpdateRemoteServer(AccountNumConns m) { _mu.EnterWriteLock(); try { _strack ??= new Dictionary(); _strack.TryGetValue(m.Server.Id, out var prev); _strack[m.Server.Id] = new SConns { Conns = m.Conns, Leafs = m.LeafNodes, }; _nrclients += m.Conns - (prev?.Conns ?? 0); _nrleafs += m.LeafNodes - (prev?.Leafs ?? 0); var localCount = _clients?.Count ?? 0; // Check if total connections exceed the limit. bool maxConnsExceeded = MaxConnections != NoLimit && (localCount - _sysclients + _nrclients) > MaxConnections; List toDisconnect = []; if (maxConnsExceeded) { var external = GetExternalClientsLocked(); // Sort: newest connections first (reverse chronological). // TODO: session 12 — sort by c.Start once ClientConnection has a Start field. // For now we cannot sort without the start time, so take from end. int over = (localCount - _sysclients + _nrclients) - MaxConnections; if (over < external.Count) toDisconnect.AddRange(external.GetRange(0, over)); else toDisconnect.AddRange(external); } // Check if total leaf nodes exceed the limit. bool maxLeafsExceeded = MaxLeafNodes != NoLimit && (_nleafs + _nrleafs) > MaxLeafNodes; if (maxLeafsExceeded) { _lmu.EnterReadLock(); try { int over = _nleafs + _nrleafs - MaxLeafNodes; if (over > 0) { int start = Math.Max(0, _lleafs.Count - over); toDisconnect.AddRange(_lleafs.GetRange(start, _lleafs.Count - start)); } } finally { _lmu.ExitReadLock(); } } return toDisconnect; } finally { _mu.ExitWriteLock(); } } /// /// Removes tracking for a remote server that has shut down. /// Mirrors Go (a *Account) removeRemoteServer(sid string). /// internal void RemoveRemoteServer(string sid) { _mu.EnterWriteLock(); try { if (_strack != null && _strack.TryGetValue(sid, out var prev)) { _strack.Remove(sid); _nrclients -= prev.Conns; _nrleafs -= prev.Leafs; } } finally { _mu.ExitWriteLock(); } } /// /// Returns the number of remote servers that have at least one connection or /// leaf-node for this account. /// Mirrors Go (a *Account) expectedRemoteResponses() int32. /// internal int ExpectedRemoteResponses() { int expected = 0; _mu.EnterReadLock(); try { if (_strack != null) { foreach (var sc in _strack.Values) { if (sc.Conns > 0 || sc.Leafs > 0) expected++; } } } finally { _mu.ExitReadLock(); } return expected; } // ------------------------------------------------------------------------- // Eventing // ------------------------------------------------------------------------- /// /// Clears eventing state including timers, clients, and remote tracking. /// Mirrors Go (a *Account) clearEventing(). /// internal void ClearEventing() { _mu.EnterWriteLock(); try { _nrclients = 0; ClearTimerLocked(ref _etmr); ClearTimerLocked(ref _ctmr); _clients = null; _strack = null; } finally { _mu.ExitWriteLock(); } } // ------------------------------------------------------------------------- // Name accessors // ------------------------------------------------------------------------- /// /// Returns the account name, thread-safely. /// Mirrors Go (a *Account) GetName() string. /// public string GetName() { _mu.EnterReadLock(); try { return Name; } finally { _mu.ExitReadLock(); } } /// /// Returns the if set, otherwise . /// Acquires a read lock. /// Mirrors Go (a *Account) getNameTag() string. /// internal string GetNameTag() { _mu.EnterReadLock(); try { return GetNameTagLocked(); } finally { _mu.ExitReadLock(); } } /// /// Returns the if set, otherwise . /// Lock must be held by the caller. /// Mirrors Go (a *Account) getNameTagLocked() string. /// internal string GetNameTagLocked() => string.IsNullOrEmpty(NameTag) ? Name : NameTag; // ------------------------------------------------------------------------- // Connection counts // ------------------------------------------------------------------------- /// /// Returns the total number of active clients across all servers (local minus /// system accounts plus remote). /// Mirrors Go (a *Account) NumConnections() int. /// public int NumConnections() { _mu.EnterReadLock(); try { return (_clients?.Count ?? 0) - _sysclients + _nrclients; } finally { _mu.ExitReadLock(); } } /// /// Returns the number of client and leaf-node connections that are on /// remote servers. /// Mirrors Go (a *Account) NumRemoteConnections() int. /// public int NumRemoteConnections() { _mu.EnterReadLock(); try { return _nrclients + _nrleafs; } finally { _mu.ExitReadLock(); } } /// /// Returns the number of non-system, non-leaf clients on this server. /// Mirrors Go (a *Account) NumLocalConnections() int. /// public int NumLocalConnections() { _mu.EnterReadLock(); try { return NumLocalConnectionsLocked(); } finally { _mu.ExitReadLock(); } } /// /// Returns local non-system, non-leaf client count. Lock must be held. /// Mirrors Go (a *Account) numLocalConnections() int. /// internal int NumLocalConnectionsLocked() => (_clients?.Count ?? 0) - _sysclients - _nleafs; /// /// Returns local non-system, non-leaf client count. Lock must be held. /// Mirrors Go (a *Account) numLocalConnections() int. /// internal int NumLocalConnectionsInternal() => NumLocalConnectionsLocked(); /// /// Returns all local connections including leaf nodes (minus system clients). /// Mirrors Go (a *Account) numLocalAndLeafConnections() int. /// internal int NumLocalAndLeafConnections() { _mu.EnterReadLock(); try { return (_clients?.Count ?? 0) - _sysclients; } finally { _mu.ExitReadLock(); } } /// /// Returns the local leaf-node count. /// Mirrors Go (a *Account) numLocalLeafNodes() int. /// internal int NumLocalLeafNodes() => _nleafs; // ------------------------------------------------------------------------- // Connection limit checks // ------------------------------------------------------------------------- /// /// Returns true if the total (local + remote) client count has reached or /// exceeded the configured limit. /// Mirrors Go (a *Account) MaxTotalConnectionsReached() bool. /// public bool MaxTotalConnectionsReached() { _mu.EnterReadLock(); try { if (MaxConnections == NoLimit) return false; return (_clients?.Count ?? 0) - _sysclients + _nrclients >= MaxConnections; } finally { _mu.ExitReadLock(); } } /// /// Returns the configured maximum connections limit. /// Mirrors Go (a *Account) MaxActiveConnections() int. /// public int MaxActiveConnections() { _mu.EnterReadLock(); try { return MaxConnections; } finally { _mu.ExitReadLock(); } } // ------------------------------------------------------------------------- // Leaf-node limit checks // ------------------------------------------------------------------------- /// /// Returns true if the total (local + remote) leaf-node count has reached or /// exceeded the configured limit. /// Mirrors Go (a *Account) MaxTotalLeafNodesReached() bool. /// public bool MaxTotalLeafNodesReached() { _mu.EnterReadLock(); try { return MaxTotalLeafNodesReachedLocked(); } finally { _mu.ExitReadLock(); } } /// /// Lock must be held by the caller. /// Mirrors Go (a *Account) maxTotalLeafNodesReached() bool. /// internal bool MaxTotalLeafNodesReachedLocked() { if (MaxLeafNodes == NoLimit) return false; return _nleafs + _nrleafs >= MaxLeafNodes; } /// /// Returns true if total leaf-node count reached the configured maximum. /// Lock must be held by the caller. /// Mirrors Go (a *Account) maxTotalLeafNodesReached() bool. /// internal bool MaxTotalLeafNodesReachedInternal() => MaxTotalLeafNodesReachedLocked(); /// /// Returns the total leaf-node count (local + remote). /// Mirrors Go (a *Account) NumLeafNodes() int. /// public int NumLeafNodes() { _mu.EnterReadLock(); try { return _nleafs + _nrleafs; } finally { _mu.ExitReadLock(); } } /// /// Returns the remote leaf-node count. /// Mirrors Go (a *Account) NumRemoteLeafNodes() int. /// public int NumRemoteLeafNodes() { _mu.EnterReadLock(); try { return _nrleafs; } finally { _mu.ExitReadLock(); } } /// /// Returns the configured maximum leaf-nodes limit. /// Mirrors Go (a *Account) MaxActiveLeafNodes() int. /// public int MaxActiveLeafNodes() { _mu.EnterReadLock(); try { return MaxLeafNodes; } finally { _mu.ExitReadLock(); } } // ------------------------------------------------------------------------- // Subscription counts // ------------------------------------------------------------------------- /// /// Returns the number of route-map entries (subjects sent across routes). /// Mirrors Go (a *Account) RoutedSubs() int. /// public int RoutedSubs() { _mu.EnterReadLock(); try { return _rm?.Count ?? 0; } finally { _mu.ExitReadLock(); } } /// /// Returns the total number of subscriptions in this account's subscription index. /// Mirrors Go (a *Account) TotalSubs() int. /// public int TotalSubs() { _mu.EnterReadLock(); try { if (Sublist == null) return 0; return (int)Sublist.Count(); } finally { _mu.ExitReadLock(); } } /// /// Returns true when there is at least one matching subscription for . /// Mirrors Go (a *Account) SubscriptionInterest(subject string) bool. /// public bool SubscriptionInterest(string subject) => Interest(subject) > 0; /// /// Returns total number of plain and queue subscriptions matching . /// Mirrors Go (a *Account) Interest(subject string) int. /// public int Interest(string subject) { _mu.EnterReadLock(); try { if (Sublist == null) return 0; var (np, nq) = Sublist.NumInterest(subject); return np + nq; } finally { _mu.ExitReadLock(); } } /// /// Increments the leaf-node count for a remote cluster. /// Mirrors Go (a *Account) registerLeafNodeCluster(cluster string). /// internal void RegisterLeafNodeCluster(string cluster) { _mu.EnterWriteLock(); try { _leafClusters ??= new Dictionary(StringComparer.Ordinal); _leafClusters.TryGetValue(cluster, out var current); _leafClusters[cluster] = current + 1; } finally { _mu.ExitWriteLock(); } } /// /// Returns true when this account already tracks one or more leaf nodes from . /// Mirrors Go (a *Account) hasLeafNodeCluster(cluster string) bool. /// internal bool HasLeafNodeCluster(string cluster) { _mu.EnterReadLock(); try { return _leafClusters != null && _leafClusters.TryGetValue(cluster, out var count) && count > 0; } finally { _mu.ExitReadLock(); } } /// /// Returns true when the account is leaf-cluster isolated to . /// Mirrors Go (a *Account) isLeafNodeClusterIsolated(cluster string) bool. /// internal bool IsLeafNodeClusterIsolated(string cluster) { _mu.EnterReadLock(); try { if (string.IsNullOrEmpty(cluster)) return false; if (_leafClusters == null || _leafClusters.Count > 1) return false; return _leafClusters.TryGetValue(cluster, out var count) && count == (ulong)_nleafs; } finally { _mu.ExitReadLock(); } } // ------------------------------------------------------------------------- // Subscription limit error throttle // ------------------------------------------------------------------------- /// /// Returns true when it is appropriate to log a max-subscription-limit error. /// Rate-limited to at most once per . /// Mirrors Go (a *Account) shouldLogMaxSubErr() bool. /// internal bool ShouldLogMaxSubErr() { _mu.EnterReadLock(); long last = Interlocked.Read(ref _lastLimErr); _mu.ExitReadLock(); long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() * 1_000_000L; // nanoseconds long threshold = (long)AccountEventConstants.DefaultMaxSubLimitReportThreshold.TotalMilliseconds * 1_000_000L; if (now - last < threshold) return false; _mu.EnterWriteLock(); try { Interlocked.Exchange(ref _lastLimErr, now); } finally { _mu.ExitWriteLock(); } return true; } // ------------------------------------------------------------------------- // Expiration // ------------------------------------------------------------------------- /// /// Returns true when the account JWT has expired. /// Mirrors Go (a *Account) IsExpired() bool. /// public bool IsExpired() => Interlocked.CompareExchange(ref _expired, 0, 0) == 1; /// /// Returns true when this account is backed by a JWT claim. /// Lock must be held by the caller. /// Mirrors Go (a *Account) isClaimAccount() bool. /// internal bool IsClaimAccount() => !string.IsNullOrEmpty(ClaimJwt); /// /// Invoked when the expiration timer fires: marks expired and collects clients. /// Mirrors Go (a *Account) expiredTimeout(). /// private void ExpiredTimeout() { Interlocked.Exchange(ref _expired, 1); var clients = GetClients(); foreach (var c in clients) { if (!IsInternalClientKind(c.Kind)) { // TODO: session 12 — call c.AccountAuthExpired() once fully ported. } } } /// /// Starts or resets the JWT expiration timer. /// Mirrors Go (a *Account) setExpirationTimer(d time.Duration). /// internal void SetExpirationTimer(TimeSpan d) { _etmr = new Timer(_ => ExpiredTimeout(), null, d, Timeout.InfiniteTimeSpan); } /// /// Stops the expiration timer. Returns true if it was active. /// Lock must be held by the caller. /// Mirrors Go (a *Account) clearExpirationTimer() bool. /// internal bool ClearExpirationTimer() { if (_etmr == null) return true; _etmr.Dispose(); _etmr = null; return true; } // ------------------------------------------------------------------------- // Subject mappings — public API // ------------------------------------------------------------------------- /// /// Adds a simple 1:1 subject mapping from to /// with weight 100. /// Mirrors Go (a *Account) AddMapping(src, dest string) error. /// public Exception? AddMapping(string src, string dest) => AddWeightedMappings(src, MapDest.New(dest, 100)); /// /// Adds weighted subject mappings for one or more destinations. /// Total weights must not exceed 100 per cluster group. If the total is /// less than 100 and the source was not listed as a destination, the /// remainder is automatically routed back to the source. /// Weights are converted to cumulative form and sorted ascending so that /// random selection can use a single linear scan. /// Mirrors Go (a *Account) AddWeightedMappings(src string, dests ...*MapDest) error. /// public Exception? AddWeightedMappings(string src, params MapDest[] dests) { _mu.EnterWriteLock(); try { if (!SubscriptionIndex.IsValidSubject(src)) return ServerErrors.ErrBadSubject; bool hasWildcard = SubscriptionIndex.SubjectHasWildcard(src); var m = new SubjectMapping { Source = src, HasWildcard = hasWildcard, Destinations = new List(dests.Length + 1), }; var seen = new HashSet(dests.Length); var totals = new Dictionary(); // cluster → cumulative weight foreach (var d in dests) { if (!seen.Add(d.Subject)) return new InvalidOperationException($"duplicate entry for \"{d.Subject}\""); if (d.Weight > 100) return new InvalidOperationException("individual weights need to be <= 100"); totals.TryGetValue(d.Cluster, out byte tw); int next = tw + d.Weight; if (next > 100) return new InvalidOperationException("total weight needs to be <= 100"); totals[d.Cluster] = (byte)next; // Validate the transform is valid. var validateErr = ValidateMapping(src, d.Subject); if (validateErr != null) return validateErr; var (tr, trErr) = SubjectTransform.New(src, d.Subject); if (trErr != null) return trErr; if (string.IsNullOrEmpty(d.Cluster)) { m.Destinations.Add(new Destination { Transform = tr, Weight = d.Weight }); } else { m.ClusterDestinations ??= new Dictionary>(); if (!m.ClusterDestinations.TryGetValue(d.Cluster, out var clusterList)) { clusterList = []; m.ClusterDestinations[d.Cluster] = clusterList; } clusterList.Add(new Destination { Transform = tr, Weight = d.Weight }); } } // Process each destination list: fill remainder and convert to cumulative weights. var destErr = ProcessDestinations(src, hasWildcard, seen, m.Destinations); if (destErr != null) return destErr; if (m.ClusterDestinations != null) { var clusterKeys = new List(m.ClusterDestinations.Keys); foreach (var cluster in clusterKeys) { destErr = ProcessDestinations(src, hasWildcard, seen, m.ClusterDestinations[cluster]); if (destErr != null) return destErr; } } // Replace existing entry for the same source, or append. for (int i = 0; i < _mappings.Count; i++) { if (_mappings[i].Source == src) { _mappings[i] = m; return null; } } _mappings.Add(m); Interlocked.Exchange(ref _hasMapped, _mappings.Count > 0 ? 1 : 0); // TODO: session 15 — notify connected leaf nodes via lc.ForceAddToSmap(src). return null; } finally { _mu.ExitWriteLock(); } } /// /// Removes a subject mapping entry by source subject. /// Returns true if an entry was removed. /// Mirrors Go (a *Account) RemoveMapping(src string) bool. /// public bool RemoveMapping(string src) { _mu.EnterWriteLock(); try { for (int i = 0; i < _mappings.Count; i++) { if (_mappings[i].Source == src) { // Swap with last element to avoid shifting (order may change). _mappings[i] = _mappings[^1]; _mappings.RemoveAt(_mappings.Count - 1); Interlocked.Exchange(ref _hasMapped, _mappings.Count > 0 ? 1 : 0); // TODO: session 15 — notify leaf nodes via lc.ForceRemoveFromSmap(src). return true; } } return false; } finally { _mu.ExitWriteLock(); } } /// /// Returns true when there is at least one subject mapping entry. /// Mirrors Go (a *Account) hasMappings() bool. /// internal bool HasMappings() => Interlocked.CompareExchange(ref _hasMapped, 0, 0) == 1; /// /// Selects a mapped destination subject using weighted random selection. /// Returns (, false) when no mapping matches. /// Mirrors Go (a *Account) selectMappedSubject(dest string) (string, bool). /// internal (string dest, bool mapped) SelectMappedSubject(string dest) { if (!HasMappings()) return (dest, false); _mu.EnterReadLock(); try { // Tokenise the destination for wildcard subset matching. string[]? tts = null; SubjectMapping? m = null; foreach (var rm in _mappings) { if (!rm.HasWildcard && rm.Source == dest) { m = rm; break; } // Lazy tokenise for subset matching. tts ??= TokenizeSubjectForMapping(dest); if (SubjectTransform.IsSubsetMatch(tts, rm.Source)) { m = rm; break; } } if (m == null) return (dest, false); // Select the destination list (cluster-scoped or global). List dests = m.Destinations; if (m.ClusterDestinations != null && m.ClusterDestinations.Count > 0) { string clusterName = GetCachedClusterName(); if (!string.IsNullOrEmpty(clusterName) && m.ClusterDestinations.TryGetValue(clusterName, out var cdests)) { dests = cdests; } } if (dests.Count == 0) return (dest, false); // Optimise single-entry case where the full weight is 100. Destination? selected = null; if (dests.Count == 1 && dests[0].Weight == 100) { selected = dests[0]; } else { byte w = (byte)(Random.Shared.Next() % 100); foreach (var d in dests) { if (w < d.Weight) { selected = d; break; } } } if (selected == null) return (dest, false); string ndest; if (selected.Transform == null) { ndest = dest; } else if (tts != null) { ndest = selected.Transform.TransformTokenizedSubject(tts); } else { ndest = selected.Transform.TransformSubject(dest); } return (ndest, true); } finally { _mu.ExitReadLock(); } } // ------------------------------------------------------------------------- // Service export configuration // ------------------------------------------------------------------------- /// /// Configures an exported service with singleton response semantics. /// Mirrors Go (a *Account) AddServiceExport(subject string, accounts []*Account) error. /// public Exception? AddServiceExport(string subject, IReadOnlyList? accounts = null) => AddServiceExportWithResponseAndAccountPos(subject, ServiceRespType.Singleton, accounts, 0); /// /// Configures an exported service with singleton response semantics and account-position auth. /// Mirrors Go (a *Account) addServiceExportWithAccountPos(...). /// public Exception? AddServiceExportWithAccountPos(string subject, IReadOnlyList? accounts, uint accountPos) => AddServiceExportWithResponseAndAccountPos(subject, ServiceRespType.Singleton, accounts, accountPos); /// /// Configures an exported service with explicit response type. /// Mirrors Go (a *Account) AddServiceExportWithResponse(...). /// public Exception? AddServiceExportWithResponse(string subject, ServiceRespType respType, IReadOnlyList? accounts = null) => AddServiceExportWithResponseAndAccountPos(subject, respType, accounts, 0); /// /// Configures an exported service with explicit response type and account-position auth. /// Mirrors Go (a *Account) addServiceExportWithResponseAndAccountPos(...). /// public Exception? AddServiceExportWithResponseAndAccountPos(string subject, ServiceRespType respType, IReadOnlyList? accounts, uint accountPos) { if (!SubscriptionIndex.IsValidSubject(subject)) return ServerErrors.ErrBadSubject; _mu.EnterWriteLock(); try { Exports.Services ??= new Dictionary(StringComparer.Ordinal); if (!Exports.Services.TryGetValue(subject, out var serviceExport) || serviceExport == null) serviceExport = new ServiceExportEntry(); if (respType != ServiceRespType.Singleton) serviceExport.ResponseType = respType; if (accounts != null || accountPos > 0) { var authErr = SetExportAuth(serviceExport, subject, accounts, accountPos); if (authErr != null) return authErr; } serviceExport.Account = this; serviceExport.ResponseThreshold = ServerConstants.DefaultServiceExportResponseThreshold; Exports.Services[subject] = serviceExport; return null; } finally { _mu.ExitWriteLock(); } } /// /// Enables latency tracking for with default sampling. /// Mirrors Go (a *Account) TrackServiceExport(service, results string) error. /// public Exception? TrackServiceExport(string service, string results) => TrackServiceExportWithSampling(service, results, ServerConstants.DefaultServiceLatencySampling); /// /// Enables latency tracking for with explicit sampling. /// Mirrors Go (a *Account) TrackServiceExportWithSampling(...). /// public Exception? TrackServiceExportWithSampling(string service, string results, int sampling) { if (sampling != 0 && (sampling < 1 || sampling > 100)) return ServerErrors.ErrBadSampling; if (!SubscriptionIndex.IsValidPublishSubject(results)) return ServerErrors.ErrBadPublishSubject; if (IsExportService(results)) return ServerErrors.ErrBadPublishSubject; _mu.EnterWriteLock(); try { if (Exports.Services == null) return ServerErrors.ErrMissingService; if (!Exports.Services.TryGetValue(service, out var serviceExport)) return ServerErrors.ErrMissingService; serviceExport ??= new ServiceExportEntry(); if (serviceExport.ResponseType != ServiceRespType.Singleton) return ServerErrors.ErrBadServiceType; serviceExport.Latency = new InternalServiceLatency { Sampling = sampling, Subject = results, }; Exports.Services[service] = serviceExport; if (Imports.Services != null) { foreach (var imports in Imports.Services.Values) { foreach (var import in imports) { if (import?.Account?.Name != Name) continue; if (SubjectTransform.IsSubsetMatch(SubjectTransform.TokenizeSubject(import.To), service)) import.Latency = serviceExport.Latency; } } } return null; } finally { _mu.ExitWriteLock(); } } /// /// Disables latency tracking for the exported service. /// Mirrors Go (a *Account) UnTrackServiceExport(service string). /// public void UnTrackServiceExport(string service) { _mu.EnterWriteLock(); try { if (Exports.Services == null || !Exports.Services.TryGetValue(service, out var serviceExport) || serviceExport?.Latency == null) return; serviceExport.Latency = null; if (Imports.Services == null) return; foreach (var imports in Imports.Services.Values) { foreach (var import in imports) { if (import?.Account?.Name != Name) continue; if (SubjectTransform.IsSubsetMatch(SubjectTransform.TokenizeSubject(import.To), service)) { import.Latency = null; import.M1 = null; } } } } finally { _mu.ExitWriteLock(); } } // ------------------------------------------------------------------------- // Export checks // ------------------------------------------------------------------------- /// /// Returns true if the given service subject is exported (exact or wildcard match). /// Mirrors Go (a *Account) IsExportService(service string) bool. /// public bool IsExportService(string service) { _mu.EnterReadLock(); try { if (Exports.Services == null) return false; if (Exports.Services.ContainsKey(service)) return true; var tokens = SubjectTransform.TokenizeSubject(service); foreach (var subj in Exports.Services.Keys) { if (SubjectTransform.IsSubsetMatch(tokens, subj)) return true; } return false; } finally { _mu.ExitReadLock(); } } /// /// Returns true if the given service export has latency tracking enabled. /// Mirrors Go (a *Account) IsExportServiceTracking(service string) bool. /// public bool IsExportServiceTracking(string service) { _mu.EnterReadLock(); try { if (Exports.Services == null) return false; if (Exports.Services.TryGetValue(service, out var ea)) { if (ea == null) return false; if (ea.Latency != null) return true; } var tokens = SubjectTransform.TokenizeSubject(service); foreach (var (subj, se) in Exports.Services) { if (SubjectTransform.IsSubsetMatch(tokens, subj) && se?.Latency != null) return true; } return false; } finally { _mu.ExitReadLock(); } } /// /// Checks whether another account is approved to import this stream export. /// Lock must be held on entry (read is sufficient). /// Mirrors Go (a *Account) checkStreamExportApproved(...) bool. /// internal bool CheckStreamExportApproved(Account account, string subject, object? imClaim) { if (Exports.Streams == null) return false; if (Exports.Streams.TryGetValue(subject, out var ea)) { if (ea == null) return true; return CheckAuth(ea, account, imClaim, null); } var tokens = SubjectTransform.TokenizeSubject(subject); foreach (var (subj, se) in Exports.Streams) { if (SubjectTransform.IsSubsetMatch(tokens, subj)) { if (se == null) return true; return CheckAuth(se, account, imClaim, tokens); } } return false; } /// /// Checks whether another account is approved to import this service export. /// Lock must be held on entry (read is sufficient). /// Mirrors Go (a *Account) checkServiceExportApproved(...) bool. /// internal bool CheckServiceExportApproved(Account account, string subject, object? imClaim) { if (Exports.Services == null) return false; if (Exports.Services.TryGetValue(subject, out var se)) { if (se == null) return true; return CheckAuth(se, account, imClaim, null); } var tokens = SubjectTransform.TokenizeSubject(subject); foreach (var (subj, entry) in Exports.Services) { if (SubjectTransform.IsSubsetMatch(tokens, subj)) { if (entry == null) return true; return CheckAuth(entry, account, imClaim, tokens); } } return false; } // ------------------------------------------------------------------------- // User revocation check // ------------------------------------------------------------------------- /// /// Returns true if the user identified by with the /// given timestamp has been revoked. /// Mirrors Go (a *Account) checkUserRevoked(nkey string, issuedAt int64) bool. /// public bool CheckUserRevoked(string nkey, long issuedAt) { _mu.EnterReadLock(); try { return IsRevoked(UsersRevoked, nkey, issuedAt); } finally { _mu.ExitReadLock(); } } // ------------------------------------------------------------------------- // Config-reload comparison helpers // ------------------------------------------------------------------------- /// /// Returns true if this account's stream imports equal 's. /// Acquires this account's read lock; must not be /// concurrently accessed. /// Mirrors Go (a *Account) checkStreamImportsEqual(b *Account) bool. /// internal bool CheckStreamImportsEqual(Account b) { _mu.EnterReadLock(); try { var aStreams = Imports.Streams; var bStreams = b.Imports.Streams; int aLen = aStreams?.Count ?? 0; int bLen = bStreams?.Count ?? 0; if (aLen != bLen) return false; if (aLen == 0) return true; // Build an index from (accName+from+to) → entry for b. var bIndex = new Dictionary(bLen); foreach (var bim in bStreams!) { string key = (bim.Account?.Name ?? string.Empty) + bim.From + bim.To; bIndex[key] = bim; } foreach (var aim in aStreams!) { string key = (aim.Account?.Name ?? string.Empty) + aim.From + aim.To; if (!bIndex.TryGetValue(key, out var bim)) return false; if (aim.AllowTrace != bim.AllowTrace) return false; } return true; } finally { _mu.ExitReadLock(); } } /// /// Returns true if this account's stream exports equal 's. /// Acquires this account's read lock; must not be /// concurrently accessed. /// Mirrors Go (a *Account) checkStreamExportsEqual(b *Account) bool. /// internal bool CheckStreamExportsEqual(Account b) { _mu.EnterReadLock(); try { var aStreams = Exports.Streams; var bStreams = b.Exports.Streams; int aLen = aStreams?.Count ?? 0; int bLen = bStreams?.Count ?? 0; if (aLen != bLen) return false; if (aLen == 0) return true; foreach (var (subj, aea) in aStreams!) { if (!bStreams!.TryGetValue(subj, out var bea)) return false; if (!IsStreamExportEqual(aea, bea)) return false; } return true; } finally { _mu.ExitReadLock(); } } /// /// Returns true if this account's service exports equal 's. /// Acquires this account's read lock; must not be /// concurrently accessed. /// Mirrors Go (a *Account) checkServiceExportsEqual(b *Account) bool. /// internal bool CheckServiceExportsEqual(Account b) { _mu.EnterReadLock(); try { var aServices = Exports.Services; var bServices = b.Exports.Services; int aLen = aServices?.Count ?? 0; int bLen = bServices?.Count ?? 0; if (aLen != bLen) return false; if (aLen == 0) return true; foreach (var (subj, aea) in aServices!) { if (!bServices!.TryGetValue(subj, out var bea)) return false; if (!IsServiceExportEqual(aea, bea)) return false; } return true; } finally { _mu.ExitReadLock(); } } // ------------------------------------------------------------------------- // Leaf-node helpers // ------------------------------------------------------------------------- /// /// Notifies leaf nodes of a subscription change. /// Stub — full implementation in session 15. /// Mirrors Go (a *Account) updateLeafNodes(sub, delta). /// internal void UpdateLeafNodes(object sub, int delta) { if (delta == 0 || sub is not Subscription s || s.Subject.Length == 0) return; var subject = Encoding.UTF8.GetString(s.Subject); var queue = s.Queue is { Length: > 0 } ? Encoding.UTF8.GetString(s.Queue) : string.Empty; _mu.EnterWriteLock(); try { _rm ??= new Dictionary(StringComparer.Ordinal); if (!_rm.TryGetValue(subject, out var rc)) rc = 0; rc += delta; if (rc <= 0) _rm.Remove(subject); else _rm[subject] = rc; if (!string.IsNullOrEmpty(queue)) { _lqws ??= new Dictionary(StringComparer.Ordinal); var key = $"{subject} {queue}"; var qw = s.Qw != 0 ? s.Qw : 1; if (!_lqws.TryGetValue(key, out var qv)) qv = 0; qv += delta * qw; if (qv <= 0) _lqws.Remove(key); else _lqws[key] = qv; } } finally { _mu.ExitWriteLock(); } List leafs; _lmu.EnterReadLock(); try { leafs = [.. _lleafs]; } finally { _lmu.ExitReadLock(); } foreach (var leaf in leafs) leaf.FlushSignal(); } // ------------------------------------------------------------------------- // addClient / removeClient // ------------------------------------------------------------------------- /// /// Registers a client with this account, updating system and leaf counters. /// Returns the previous total client count. /// Mirrors Go (a *Account) addClient(c *client) int. /// private int AddClientInternal(ClientConnection c) { _mu.EnterWriteLock(); int prev; try { _clients ??= new HashSet(); prev = _clients.Count; if (!_clients.Add(c)) { // Client was already present — do nothing. return prev; } if (IsInternalClientKind(c.Kind)) { _sysclients++; } else if (c.Kind == ClientKind.Leaf) { _nleafs++; } } finally { _mu.ExitWriteLock(); } // Add leaf to the leaf list (uses separate lock). if (c.Kind == ClientKind.Leaf) { _lmu.EnterWriteLock(); try { _lleafs.Add(c); } finally { _lmu.ExitWriteLock(); } } // TODO: session 12 — notify server via c.srv.accConnsUpdate(a). return prev; } /// /// Unregisters a client from this account, updating system and leaf counters. /// Returns the previous total client count. /// Mirrors Go (a *Account) removeClient(c *client) int. /// private int RemoveClientInternal(ClientConnection c) { _mu.EnterWriteLock(); int prev; bool wasLeaf = false; try { prev = _clients?.Count ?? 0; if (_clients == null || !_clients.Remove(c)) return prev; if (IsInternalClientKind(c.Kind)) { _sysclients--; } else if (c.Kind == ClientKind.Leaf) { _nleafs--; wasLeaf = true; // Cluster accounting for hub leaf nodes. if (c.IsHubLeafNode()) { // TODO: session 15 — c.RemoteCluster() for cluster accounting. } } } finally { _mu.ExitWriteLock(); } if (wasLeaf) { RemoveLeafNode(c); } // TODO: session 12 — notify server via c.srv.accConnsUpdate(a). return prev; } /// /// Removes a leaf-node client from the ordered leaf list. /// Uses internally. /// Mirrors Go (a *Account) removeLeafNode(c *client). /// private void RemoveLeafNode(ClientConnection c) { _lmu.EnterWriteLock(); try { int idx = _lleafs.IndexOf(c); if (idx < 0) return; int last = _lleafs.Count - 1; _lleafs[idx] = _lleafs[last]; _lleafs.RemoveAt(last); } finally { _lmu.ExitWriteLock(); } } // ------------------------------------------------------------------------- // INatsAccount implementation // ------------------------------------------------------------------------- /// /// Returns true when the account is valid (not expired). /// Mirrors Go INatsAccount.IsValid. /// bool INatsAccount.IsValid => !IsExpired(); /// /// Delegates to . /// Mirrors Go INatsAccount.MaxTotalConnectionsReached(). /// bool INatsAccount.MaxTotalConnectionsReached() => MaxTotalConnectionsReached(); /// /// Delegates to . /// Mirrors Go INatsAccount.MaxTotalLeafNodesReached(). /// bool INatsAccount.MaxTotalLeafNodesReached() => MaxTotalLeafNodesReached(); /// /// Registers a client connection. Returns the previous client count. /// Mirrors Go INatsAccount.AddClient(c). /// int INatsAccount.AddClient(ClientConnection c) => AddClientInternal(c); /// /// Unregisters a client connection. Returns the previous client count. /// Mirrors Go INatsAccount.RemoveClient(c). /// int INatsAccount.RemoveClient(ClientConnection c) => RemoveClientInternal(c); // ------------------------------------------------------------------------- // Static helpers // ------------------------------------------------------------------------- /// /// Returns true when the user identified by with /// the given timestamp has been revoked. /// Also checks the wildcard entry (jwt.All = "*"). /// Mirrors Go package-level isRevoked(...) bool. /// internal static bool IsRevoked( Dictionary? revocations, string subject, long issuedAt) { if (revocations == null || revocations.Count == 0) return false; // Check specific key. if (revocations.TryGetValue(subject, out long ts) && ts >= issuedAt) return true; // Check wildcard revocation ("*" = jwt.All). if (revocations.TryGetValue("*", out long tsAll) && tsAll >= issuedAt) return true; return false; } /// /// Returns true if the reply is a tracked reply (ends with "..T"). /// Mirrors Go package-level isTrackedReply(reply []byte) bool. /// internal static bool IsTrackedReply(ReadOnlySpan reply) { int lreply = reply.Length - 1; return lreply > 3 && reply[lreply - 1] == '.' && reply[lreply] == 'T'; } /// /// Validates a mapping destination subject without creating a full transform. /// Mirrors Go ValidateMapping(src, dest string) error in sublist.go. /// Returns null on success; an exception on failure. /// internal static Exception? ValidateMapping(string src, string dest) { if (string.IsNullOrEmpty(dest)) return null; bool sfwc = false; foreach (var token in dest.Split('.')) { int length = token.Length; if (length == 0 || sfwc) return new MappingDestinationException(token, ServerErrors.ErrInvalidMappingDestinationSubject); // If it looks like a mapping function, ensure it is a known one. if (length > 4 && token[0] == '{' && token[1] == '{' && token[length - 2] == '}' && token[length - 1] == '}') { var (tt, _, _, _, terr) = SubjectTransform.IndexPlaceHolders(token); if (terr != null) return terr; if (tt == TransformType.BadTransform) return new MappingDestinationException(token, ServerErrors.ErrUnknownMappingDestinationFunction); continue; } if (length == 1 && token[0] == '>') { sfwc = true; } else if (token.IndexOfAny(['\t', '\n', '\f', '\r', ' ']) >= 0) { return ServerErrors.ErrInvalidMappingDestinationSubject; } } // Verify the full transform can be constructed. var (_, err) = SubjectTransform.New(src, dest); return err; } /// /// Returns true if the given is an internal kind /// (System, JetStream, or Account — not a real user connection). /// Mirrors Go isInternalClient(kind int) bool. /// private static bool IsInternalClientKind(ClientKind kind) => kind is ClientKind.System or ClientKind.JetStream or ClientKind.Account; // ------------------------------------------------------------------------- // Private helpers // ------------------------------------------------------------------------- /// /// Builds the cumulative-weight destination list from a list of raw-weight /// entries. If the total weight is less than 100 /// and the source was not explicitly listed as a destination, a pass-through /// entry is auto-added for the remainder. /// Mirrors Go processDestinations(dests []*destination) ([]*destination, error). /// private static Exception? ProcessDestinations( string src, bool hasWildcard, HashSet seen, List dests) { byte totalWeight = 0; foreach (var d in dests) totalWeight += d.Weight; bool haveSrc = seen.Contains(src); // Auto-fill the remaining weight with a pass-through to the source. if (totalWeight != 100 && !haveSrc) { string passThroughDest = src; if (hasWildcard) passThroughDest = SubjectTransform.TransformTokenize(src); var (tr, err) = SubjectTransform.New(src, passThroughDest); if (err != null) return err; byte aw = dests.Count == 0 ? (byte)100 : (byte)(100 - totalWeight); dests.Add(new Destination { Transform = tr, Weight = aw }); } // Sort ascending by raw weight so the cumulative scan is correct. dests.Sort((a, b) => a.Weight.CompareTo(b.Weight)); // Convert raw weights to cumulative weights. byte cumulative = 0; foreach (var d in dests) { cumulative += d.Weight; d.Weight = cumulative; } return null; } /// /// Tokenises a subject string into an array, using the same split logic /// as btsep-based tokenisation in the Go source. /// private static string[] TokenizeSubjectForMapping(string subject) { var parts = new List(); int start = 0; for (int i = 0; i < subject.Length; i++) { if (subject[i] == '.') { parts.Add(subject[start..i]); start = i + 1; } } parts.Add(subject[start..]); return [.. parts]; } /// /// Returns the cached cluster name for cluster-scoped mapping selection. /// Delegates to the server when available; returns empty string as a stub. /// Mirrors Go a.srv.cachedClusterName(). /// TODO: session 09 — wire up Server.CachedClusterName(). /// private string GetCachedClusterName() { // TODO: session 09 — Server.CachedClusterName(). return string.Empty; } /// /// Stops and nulls out a timer. Lock must be held by the caller. /// Mirrors Go clearTimer(t **time.Timer). /// private static void ClearTimerLocked(ref Timer? t) { t?.Dispose(); t = null; } /// /// Checks whether is authorised to use /// (either via explicit approval or token requirement). /// Mirrors Go (a *Account) checkAuth(...) bool. /// TODO: session 11 — full JWT activation check. /// private static bool CheckAuth( ExportAuth ea, Account account, object? imClaim, string[]? tokens) { if (ea.Approved != null && ea.Approved.ContainsKey(account.Name)) return true; if (ea.TokenRequired) { // TODO: session 11 — validate activation token in imClaim. return imClaim != null; } // No approved list and no token required → public export. if (ea.Approved == null) return true; // AccountPosition embedding check. if (ea.AccountPosition > 0 && tokens != null) { int pos = (int)ea.AccountPosition - 1; if (pos < tokens.Length && tokens[pos] == account.Name) return true; } return false; } /// /// Applies account-based authorization rules to an export descriptor. /// Mirrors Go setExportAuth(&se.exportAuth, ...). /// private static Exception? SetExportAuth(ExportAuth auth, string subject, IReadOnlyList? accounts, uint accountPos) { if (!SubscriptionIndex.IsValidSubject(subject)) return ServerErrors.ErrBadSubject; auth.AccountPosition = accountPos; if (accounts == null || accounts.Count == 0) { auth.Approved = null; return null; } var approved = new Dictionary(accounts.Count, StringComparer.Ordinal); foreach (var account in accounts) { if (account == null) continue; approved[account.Name] = account; } auth.Approved = approved; return null; } // ------------------------------------------------------------------------- // Export equality helpers // ------------------------------------------------------------------------- private static bool IsStreamExportEqual(StreamExport? a, StreamExport? b) { if (a == null && b == null) return true; if ((a == null) != (b == null)) return false; return IsExportAuthEqual(a!, b!); } private static bool IsServiceExportEqual(ServiceExportEntry? a, ServiceExportEntry? b) { if (a == null && b == null) return true; if ((a == null) != (b == null)) return false; if (!IsExportAuthEqual(a!, b!)) return false; if ((a!.Account?.Name ?? string.Empty) != (b!.Account?.Name ?? string.Empty)) return false; if (a.ResponseType != b.ResponseType) return false; if (a.AllowTrace != b.AllowTrace) return false; // Latency comparison. if ((a.Latency == null) != (b.Latency == null)) return false; if (a.Latency != null) { if (a.Latency.Sampling != b.Latency!.Sampling) return false; if (a.Latency.Subject != b.Latency.Subject) return false; } return true; } private static bool IsExportAuthEqual(ExportAuth a, ExportAuth b) { if (a.TokenRequired != b.TokenRequired) return false; if (a.AccountPosition != b.AccountPosition) return false; int aApproved = a.Approved?.Count ?? 0; int bApproved = b.Approved?.Count ?? 0; if (aApproved != bApproved) return false; if (a.Approved != null) { foreach (var (ak, av) in a.Approved) { if (!b.Approved!.TryGetValue(ak, out var bv) || av.Name != bv.Name) return false; } } int aRevoked = a.ActivationsRevoked?.Count ?? 0; int bRevoked = b.ActivationsRevoked?.Count ?? 0; if (aRevoked != bRevoked) return false; if (a.ActivationsRevoked != null) { foreach (var (ak, av) in a.ActivationsRevoked) { if (!b.ActivationsRevoked!.TryGetValue(ak, out var bv) || av != bv) return false; } } return true; } }