From 0ebe9a876ce0c7101b2425dd9e9bac6878d35959 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 5 Jan 2026 13:26:29 -0500 Subject: [PATCH] fix: Apply MarketDataClientConfig to MDSource For throttle intervals, etc. --- src/client/spark/Daemons/MDDaemon.cs | 2 +- .../spark_sdk/samples/SdkSampleService.cs | 2 +- src/cs/Bts.Spark.Api/MarketData/MDSource.cs | 15 ++++++---- .../Services/SparkApiServices.cs | 29 +++++++++++++------ src/cs/Bts.Spark.Sdk/SparkSession.cs | 9 ++++++ src/cs/Bts.Spark.Sdk/SparkSessionConfig.cs | 1 + 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/client/spark/Daemons/MDDaemon.cs b/src/client/spark/Daemons/MDDaemon.cs index 169f70f51..db735d80b 100644 --- a/src/client/spark/Daemons/MDDaemon.cs +++ b/src/client/spark/Daemons/MDDaemon.cs @@ -71,7 +71,7 @@ namespace bts { _relay_throttle = relay_throttle; _output_throttle = output_throttle; _security_metadata = security_metadata; - _md_source_backing = new(() => new(UserDataPermissions.Create(_user.Value), _relay_throttle, _output_throttle)); + _md_source_backing = new(() => MDSource.Create(UserDataPermissions.Create(_user.Value), _relay_throttle, _output_throttle)); } public int priority => 55; diff --git a/src/client/spark_sdk/samples/SdkSampleService.cs b/src/client/spark_sdk/samples/SdkSampleService.cs index e1d6a3aca..4c92c67e1 100644 --- a/src/client/spark_sdk/samples/SdkSampleService.cs +++ b/src/client/spark_sdk/samples/SdkSampleService.cs @@ -96,7 +96,7 @@ namespace bts.sdk { BTS.init(); Log.Sdk.info("Welcome to the BTS Client Side SDK example"); - var md_source = new MDSource(userDataPermissions); + var md_source = MDSource.Create(userDataPermissions); var md_client = new MarketDataClient( sparkConfigLoader, loginService, userDataPermissions, diff --git a/src/cs/Bts.Spark.Api/MarketData/MDSource.cs b/src/cs/Bts.Spark.Api/MarketData/MDSource.cs index 13f5c97e2..204c30942 100644 --- a/src/cs/Bts.Spark.Api/MarketData/MDSource.cs +++ b/src/cs/Bts.Spark.Api/MarketData/MDSource.cs @@ -4,6 +4,8 @@ namespace bts.md { using mbts; using mbts.co2; + using Microsoft.Extensions.Options; + using Spark.Api.Services; using Spark.Core.Disposables; @@ -25,13 +27,13 @@ namespace bts.md { State _state = State.Uninitialized; object _lock = new(); - public MDSource(TimeSpan relay_throttle) - : this(UserDataPermissions.Create(UserTuple.admin), relay_throttle) { } + public MDSource(IUserDataPermissions userDataPermissions, IOptions config) + : this(userDataPermissions, config.Value) { } - public MDSource(IUserDataPermissions userDataPermissions, TimeSpan? relayThrottle = null, TimeSpan? outputThrottle = null) - : this(userDataPermissions, relayThrottle ?? TimeSpan.FromSeconds(0.1), outputThrottle ?? TimeSpan.FromSeconds(1)) { } + public MDSource(IUserDataPermissions userDataPermissions, MarketDataClientConfig config) + : this(userDataPermissions, config.RelayThrottle, new TimeSpan(Math.Min(config.HotPollInterval.Ticks, config.TradePollInterval.Ticks))) { } - public MDSource( + MDSource( IUserDataPermissions userDataPermissions, TimeSpan relay_throttle, TimeSpan output_throttle) { this.userDataPermissions = userDataPermissions; _relay = new Co2RelayControl(consumer, relay_throttle); @@ -73,6 +75,9 @@ namespace bts.md { public MDConsumer consumer { get; } = new(); + public static MDSource Create(IUserDataPermissions userDataPermissions, TimeSpan? relayThrottle = null, TimeSpan? securityDiscoveryThrottle = null) => + new(userDataPermissions, relayThrottle ?? TimeSpan.FromSeconds(0.1), securityDiscoveryThrottle ?? TimeSpan.FromSeconds(1)); + public IObservable> subscribe_securities() => _securities_src; diff --git a/src/cs/Bts.Spark.Api/Services/SparkApiServices.cs b/src/cs/Bts.Spark.Api/Services/SparkApiServices.cs index 922cedf53..47a267605 100644 --- a/src/cs/Bts.Spark.Api/Services/SparkApiServices.cs +++ b/src/cs/Bts.Spark.Api/Services/SparkApiServices.cs @@ -61,17 +61,17 @@ public static class SparkApiServices { bool isMarketDataEnabled = configurationSection.GetValue("EnableMarketData", true); bool isOrdersEnabled = configurationSection.GetValue("EnableOrders", true); if (isMarketDataEnabled) { - services.AddSparkdService() - .AddSingleton() - .AddSingleton(sp => sp.GetRequiredService()) - .AddSingleton() - .AddSingleton(_ => SecurityMetadataStore.instance); var mdClientConfig = configurationSection.GetSection("MarketData"); if (mdClientConfig.Exists()) { services.Configure(mdClientConfig); } else { services.Configure(c => c.CopyFrom(MarketDataClientConfig.Default)); } + services.AddSparkdService() + .AddSingleton() + .AddSingleton(sp => sp.GetRequiredService()) + .AddSingleton() + .AddSingleton(_ => SecurityMetadataStore.instance); services.AddSingleton(sp => { var configLoader = sp.GetRequiredService(); var loginSrv = sp.GetRequiredService(); @@ -122,8 +122,11 @@ public static class SparkApiServices { /// /// The to add the service to. /// A reference to this instance after the operation has completed. - public static IServiceCollection AddSparkApi(this IServiceCollection services, Action configureApiCredentials) => - AddSparkApi(services, true, true, true, true, configureApiCredentials); + public static IServiceCollection AddSparkApi( + this IServiceCollection services, + Action configureApiCredentials, + Action configureMarketDataClient) => + AddSparkApi(services, true, true, true, true, configureApiCredentials, configureMarketDataClient); /// /// Adds services to connect to Sparkd. @@ -136,7 +139,14 @@ public static class SparkApiServices { /// Whether or not to enable admin service. /// A reference to this instance after the operation has completed. public static IServiceCollection AddSparkApi( - this IServiceCollection services, bool isMarketDataEnabled, bool isOrdersEnabled, bool isPositionsEnabled, bool isAdminEnabled, Action configureApiCredentials) { + this IServiceCollection services, + bool isMarketDataEnabled, + bool isOrdersEnabled, + bool isPositionsEnabled, + bool isAdminEnabled, + Action configureApiCredentials, + Action configureMarketDataClient + ) { if (!services.Any(s => s.ServiceType == typeof(ISparkConfigLoader))) { /// initializes configuration and logging (this should be done /// before any api-related services run). @@ -150,7 +160,8 @@ public static class SparkApiServices { .AddSingleton(sp => sp.GetRequiredService()) .AddHostedService(sp => sp.GetRequiredService()); if (isMarketDataEnabled) { - services.AddSparkdService() + services.Configure(configureMarketDataClient) + .AddSparkdService() .AddSingleton() .AddSingleton(sp => sp.GetRequiredService()) .AddSingleton() diff --git a/src/cs/Bts.Spark.Sdk/SparkSession.cs b/src/cs/Bts.Spark.Sdk/SparkSession.cs index f14a4e7bd..c99c3fa3c 100644 --- a/src/cs/Bts.Spark.Sdk/SparkSession.cs +++ b/src/cs/Bts.Spark.Sdk/SparkSession.cs @@ -59,6 +59,15 @@ public class SparkSession : IDisposable, IAsyncDisposable { creds => { creds.User = config.User; creds.Password = config.Password; + }, + mdCfg => { + var pollInterval = config.MarketDataPollInterval; + var slowPollInterval = new TimeSpan(Math.Max(pollInterval.Ticks, TimeSpan.FromSeconds(1).Ticks)); + mdCfg.HotPollInterval = pollInterval; + mdCfg.ColdPollInterval = slowPollInterval; + mdCfg.TradePollInterval = pollInterval; + mdCfg.RelayThrottle = pollInterval; + mdCfg.SecurityPollInterval = slowPollInterval; }) .AddSimpleSparkClient(config.IsOrderServiceEnabled ?? true); } diff --git a/src/cs/Bts.Spark.Sdk/SparkSessionConfig.cs b/src/cs/Bts.Spark.Sdk/SparkSessionConfig.cs index 850b34c8b..7852abc5b 100644 --- a/src/cs/Bts.Spark.Sdk/SparkSessionConfig.cs +++ b/src/cs/Bts.Spark.Sdk/SparkSessionConfig.cs @@ -8,4 +8,5 @@ public class SparkSessionConfig { public bool? IsOrderServiceEnabled { get; set; } public bool? IsPositionServiceEnabled { get; set; } public bool? IsAdminServiceEnabled { get; set; } + public TimeSpan MarketDataPollInterval { get; set; } }