namespace Bts.PubSub; using BCG; using bts; using Bts.PubSub.Routing; using System; using System.Collections.Generic; public class PubSubConfig { static PubSubConfig? instance; public static PubSubConfig Instance => instance ?? throw new InvalidOperationException( "SetGlobalConfig must be called to initialize config static singleton"); public static void SetGlobalConfig(PubSubConfig cfg) { // This should only be called once Log.isnull(instance); instance = Log.notnull(cfg); } public short? ServerPort { get; set; } public bool? SimpleEchoServer { get; set; } // unused public int? MillisecondsBetweenSubInfoUpdates { get; set; } = 1000; // unused public int? MillisecondsBetweenTimeSliceUpdates { get; set; } = 5000; // unused public int? MillisecondsBetweenNoBufferRetries { get; set; } = 100; // unused public int? RetriesOnNoBufferSpace { get; set; } = 1; // unused public int? DoItAllSecondsBetweenLog { get; set; } = 0; // Used by the PubSubMessageReactor to pass to the ManagedTCPMessageHb (but unsed within it) public int? HeartbeatInterval { get; set; } = 15000; public int? MissingHeartbeatsBeforeGc { get; set; } = 50; // unused // Milliseconds public int? StateUpdatePeriod { get; set; } = 5000; // unused // Milliseconds public int? StatisticsUpdatePeriod { get; set; } = 5000; // Milliseconds public int? DisposedConnectionCheckPeriod { get; set; } = 1000; public TagTypeToAssembly? TagTypeToAssemblyMap { get; set; } public PubSubTagHandlerFactoryConfig? TagHandlerFactory { get; set; } // unused public int? LatencyReportThresholdMs { get; set; } // unused public bool? InternallyPublishSubInfo { get; set; } public string? CompressedTags { get; set; } public int? GcActivationThreshold { get; set; } // Milliseconds public int? WaitPeriodAfterGc { get; set; } public PubSubRouterConfig? Routing { get; set; } public List? PublisherRoutes => Routing?.PublisherRoutes; public List? SubscriberRoutes => Routing?.SubscriberRoutes; public static PubSubConfig Default() { const string NOTAGS = "^NOTAGS$"; var rv = new PubSubConfig() { MillisecondsBetweenSubInfoUpdates = 1000, MillisecondsBetweenTimeSliceUpdates = 5000, MillisecondsBetweenNoBufferRetries = 100, RetriesOnNoBufferSpace = 1, DoItAllSecondsBetweenLog = 0, HeartbeatInterval = 15000, MissingHeartbeatsBeforeGc = 50, StateUpdatePeriod = 5000, StatisticsUpdatePeriod = 5000, DisposedConnectionCheckPeriod = 1000, ServerPort = 12345, TagTypeToAssemblyMap = new(), TagHandlerFactory = PubSubTagHandlerFactoryConfig.Default(), LatencyReportThresholdMs = 1500, InternallyPublishSubInfo = true, CompressedTags = NOTAGS, GcActivationThreshold = 0, WaitPeriodAfterGc = 1000, SimpleEchoServer = false, Routing = new() { PublisherRoutes = new() { new("bts-cloud://127.0.0.1:12345", ".*") }, SubscriberRoutes = new() { new("bts-cloud://127.0.0.1:12345", ".*") } } }; return rv; } public void MergeWith(PubSubConfig other) { MillisecondsBetweenSubInfoUpdates ??= other.MillisecondsBetweenSubInfoUpdates; MillisecondsBetweenTimeSliceUpdates ??= other.MillisecondsBetweenTimeSliceUpdates; MillisecondsBetweenNoBufferRetries ??= other.MillisecondsBetweenNoBufferRetries; RetriesOnNoBufferSpace ??= other.RetriesOnNoBufferSpace; DoItAllSecondsBetweenLog ??= other.DoItAllSecondsBetweenLog; HeartbeatInterval ??= other.HeartbeatInterval; MissingHeartbeatsBeforeGc ??= other.MissingHeartbeatsBeforeGc; StateUpdatePeriod ??= other.StateUpdatePeriod; StatisticsUpdatePeriod ??= other.StatisticsUpdatePeriod; DisposedConnectionCheckPeriod ??= other.DisposedConnectionCheckPeriod; ServerPort ??= other.ServerPort; TagTypeToAssemblyMap ??= other.TagTypeToAssemblyMap; TagHandlerFactory ??= other.TagHandlerFactory; LatencyReportThresholdMs ??= other.LatencyReportThresholdMs; InternallyPublishSubInfo ??= other.InternallyPublishSubInfo; CompressedTags ??= other.CompressedTags; GcActivationThreshold ??= other.GcActivationThreshold; WaitPeriodAfterGc ??= other.WaitPeriodAfterGc; SimpleEchoServer ??= other.SimpleEchoServer; if (other.Routing is not null) { if (Routing is null) { Routing = other.Routing?.Clone(); } else { Routing.MergeWith(other.Routing); } } } } public static class PubSubConfigExtensions { public static TimeSpan ConnectionDropThreshold(this PubSubConfig pubSubConfig) { ArgumentNullException.ThrowIfNull(pubSubConfig.MissingHeartbeatsBeforeGc); ArgumentNullException.ThrowIfNull(pubSubConfig.HeartbeatInterval); return TimeSpan.FromMilliseconds(pubSubConfig.HeartbeatInterval!.Value * pubSubConfig.MissingHeartbeatsBeforeGc!.Value); } public static List SubscriberRoutesFor(this PubSubConfig pubSubConfig, string site) { return Log.notnull(pubSubConfig.SubscriberRoutes); } }