namespace Bts.PubSub; using BCG; using bts; using System; using System.Collections.Generic; using System.Text.Json.Serialization; 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); } [JsonPropertyName("server_port")] public short? ServerPort { get; set; } [JsonPropertyName("simple_echo_server")] public bool? SimpleEchoServer { get; set; } // unused [JsonPropertyName("milliseconds_between_sub_info_updates")] public int? MillisecondsBetweenSubInfoUpdates { get; set; } = 1000; // unused [JsonPropertyName("milliseconds_between_time_slice_updates")] public int? MillisecondsBetweenTimeSliceUpdates { get; set; } = 5000; // unused [JsonPropertyName("milliseconds_between_no_buffer_space_retries")] public int? MillisecondsBetweenNoBufferRetries { get; set; } = 100; // unused [JsonPropertyName("retries_on_no_buffer_space")] public int? RetriesOnNoBufferSpace { get; set; } = 1; // unused [JsonPropertyName("do_it_all_seconds_between_log")] public int? DoItAllSecondsBetweenLog { get; set; } = 0; // Used by the PubSubMessageReactor to pass to the ManagedTCPMessageHb (but unsed within it) [JsonPropertyName("heartbeat_interval")] public int? HeartbeatInterval { get; set; } = 15000; [JsonPropertyName("missing_heartbeats_before_gc")] public int? MissingHeartbeatsBeforeGc { get; set; } = 50; // unused // Milliseconds [JsonPropertyName("state_update_period")] public int? StateUpdatePeriod { get; set; } = 5000; // unused // Milliseconds [JsonPropertyName("statistics_update_period")] public int? StatisticsUpdatePeriod { get; set; } = 5000; // Milliseconds [JsonPropertyName("disposed_connection_check_period")] public int? DisposedConnectionCheckPeriod { get; set; } = 1000; [JsonPropertyName("tag_type_to_assembly_map")] public TagTypeToAssembly? TagTypeToAssemblyMap { get; set; } // unused [JsonPropertyName("latency_report_threshold_ms")] public int? LatencyReportThresholdMs { get; set; } // unused [JsonPropertyName("internally_publish_sub_info")] public bool? InternallyPublishSubInfo { get; set; } [JsonPropertyName("compressed_tags")] public string? CompressedTags { get; set; } [JsonPropertyName("gc_activation_threshold")] public int? GcActivationThreshold { get; set; } // Milliseconds [JsonPropertyName("wait_period_after_gc")] public int? WaitPeriodAfterGc { get; set; } [JsonPropertyName("publisher_routes")] public List? PublisherRoutes { get; set; } [JsonPropertyName("subscriber_routes")] public List? SubscriberRoutes { get; set; } 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(), LatencyReportThresholdMs = 1500, InternallyPublishSubInfo = true, CompressedTags = NOTAGS, GcActivationThreshold = 0, WaitPeriodAfterGc = 1000, SimpleEchoServer = false, PublisherRoutes = new() { new("bluto://127.0.0.1:12345", ".*") }, SubscriberRoutes = new() { new("bluto://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; LatencyReportThresholdMs ??= other.LatencyReportThresholdMs; InternallyPublishSubInfo ??= other.InternallyPublishSubInfo; CompressedTags ??= other.CompressedTags; GcActivationThreshold ??= other.GcActivationThreshold; WaitPeriodAfterGc ??= other.WaitPeriodAfterGc; SimpleEchoServer ??= other.SimpleEchoServer; } } 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); } }