Namely, in DataStreamPublisher and PubSubServer. This is part of a broader paradigm shift towards Dependency Injection and Factory Pattern. https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines * Use interfaces to decouple services: - Decouple ManagedSubscriber from ISubscriberCore impl - Decouple PubTableBroker from IPublisherCore impl - Decouple RemoteQueryManager from IManagedSubscriber, IPubTableBroker impl * Rename Publishers -> PublisherNodes in DataStreamPublisherConfig * Model config profiles after dotnet appsettings - Rename Configurations/<name>.json -> appsettings.<name>.json - Make json fields match corresponding csharp props verbatim * The Dependency Injection framework doesn't support JsonPropertyName attributes. Config class props must match json fields verbatim. * Move reusable startup code into new Bts.Extensions.Hosting proj * Add pubsub and md-publisher services to compose.yaml
158 lines
5.3 KiB
C#
158 lines
5.3 KiB
C#
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<PubSubRoute>? PublisherRoutes => Routing?.PublisherRoutes;
|
|
public List<PubSubRoute>? 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<PubSubRoute> SubscriberRoutesFor(this PubSubConfig pubSubConfig, string site) {
|
|
return Log.notnull(pubSubConfig.SubscriberRoutes);
|
|
}
|
|
}
|
|
|