refactor: Change Configuration.initialize to be async

Tweak SparkCoreServices to synchronize Configuration.init globally
This commit is contained in:
John Colvin committed 2025-12-30 11:42:20 -05:00
1 parent be2368f95a
commit 3a6141f3ae
37 files changed
+762 -323

No files matched your search

@@ -30,7 +30,10 @@ async Task<int> RunAsync(DataStreamPublisherConfig cfg)
cfg.MergeWithConfigFile();
cfg.MergeWith(DataStreamPublisherConfig.Default());
PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub));
Configuration.initialize(cfg.Sparkd);
if (!await Configuration.InitializeAsync(cfg.Sparkd)) {
Console.Error.WriteLine("Failed to initialize Spark config");
return 1;
}
Log.Gen.info("Starting DataStreamPublisher");
var service_ctx = ServiceContext.create(cfg.Sparkd);
await service_ctx.login(cfg.User ?? "bts", cfg.Password ?? string.Empty);
@@ -26,7 +26,7 @@ static class Program {
cfg.MergeWithConfigFile();
cfg.MergeWith(DataStreamQueryAnalyzerConfig.Default());
PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub));
Configuration.initialize(cfg.Sparkd);
await Configuration.InitializeAsync(cfg.Sparkd);
PrimitiveManager.SetMarketDataClient(new MarketDataClient(new(TimeSpan.FromSeconds(0.5))));
Log.Gen.info("Starting DataStreamQueryAnalyzer");
var mainForm = new MainForm();
+6 -3
View File
@@ -17,14 +17,17 @@ using System.Threading;
using System.Threading.Tasks;
Parser.Default.ParseArguments<PubSubServerConfig>(args)
.MapResult(Run, errs => 1);
.MapResult(Run, errs => Task.FromResult(1));
int Run(PubSubServerConfig cfg) {
async Task<int> Run(PubSubServerConfig cfg) {
try {
cfg.MergeWithConfigFile();
cfg.MergeWith(PubSubServerConfig.Default());
PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub));
Configuration.initialize(cfg.Sparkd);
if (!await Configuration.InitializeAsync(cfg.Sparkd)) {
Console.Error.WriteLine("Failed to initialize Spark config");
return 1;
}
bool run_echo_server = false;
bool echo_msg_back = false;
cfg.SimpleEchoServer = run_echo_server;
@@ -8,7 +8,7 @@ namespace Spark.Console {
class ConfigCommand {
public static int run(ConfigCommandOptions opts) {
Configuration.initialize(opts.sparkd);
Configuration.InitializeAsync(opts.sparkd).AsTask().Wait();
var keys = opts.keys?.ToArray() ?? Array.Empty<string>();
var excluded_keys = opts.excluded_keys?.ToArray() ?? Array.Empty<string>();
return Configuration.run_config_query(
@@ -8,6 +8,7 @@ namespace Spark.Console {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
static class TestCommand {
static readonly Lazy<TestEnumerator> _enumerator_backing = new(
@@ -15,12 +16,15 @@ namespace Spark.Console {
static TestEnumerator _enumerator => _enumerator_backing.Value;
public static int run(TestCommandOptions o) {
public static int run(TestCommandOptions o) =>
RunAsync(o).GetAwaiter().GetResult();
public static async Task<int> RunAsync(TestCommandOptions o) {
if (o.type == TestType.Unit) {
// Skip loading any local config
Configuration.initialize_for_test();
await Configuration.ResetForTestAsync();
} else {
Configuration.initialize(o.sparkd);
await Configuration.InitializeAsync(o.sparkd);
}
ConsoleEx.WriteLine(JsonConvert.SerializeObject(o), ConsoleColor.DarkGray);
+2 -1
View File
@@ -13,6 +13,7 @@ namespace bts {
using mbts.spark;
using mbts.ui;
using Spark.Api.Services;
using Spark.Core.Data;
using Spark.Core.Disposables;
using Spark.Core.Reactive;
@@ -70,7 +71,7 @@ namespace bts {
_relay_throttle = relay_throttle;
_output_throttle = output_throttle;
_security_metadata = security_metadata;
_md_source_backing = new(() => new(_user.Value, _relay_throttle, _output_throttle));
_md_source_backing = new(() => new(UserDataPermissions.Create(_user.Value), _relay_throttle, _output_throttle));
}
public int priority => 55;
@@ -28,7 +28,7 @@ namespace mbts.ui.tests {
// This is required to satisfy the StaticResources in various parts of the application
if (Application.Current == null) {
AutoResetEvent gonow = new AutoResetEvent(false);
Configuration.initialize();
Configuration.InitializeAsync().AsTask().Wait();
Thread t = new Thread(() => {
try {
Assert.AreEqual(Thread.CurrentThread.GetApartmentState(), ApartmentState.STA);
@@ -119,7 +119,7 @@ namespace mbts.ui.shell {
public async Task<bool> init() {
// Make sure config is initialized and working
try {
Configuration.initialize(_config_source);
Configuration.InitializeAsync(_config_source).AsTask().Wait();
} catch (ApplicationException e) {
ErrorMessage = (e.Message ?? string.Empty) + " " + (App.stunnel_job_error ?? string.Empty);
HasFatalError = true;
@@ -161,7 +161,7 @@ namespace mbts.ui.shell {
}
return result;
}
return new LoginResult("Invalid username");
return new() { reason = "Invalid username" };
}
private void on_user_authenticated(string username, AuthTokenImmutable? token) {
+9 -5
View File
@@ -4,6 +4,7 @@ namespace bts.md {
using mbts;
using mbts.co2;
using Spark.Api.Services;
using Spark.Core.Disposables;
using System;
@@ -18,18 +19,21 @@ namespace bts.md {
public class MDSource : IDisposable {
readonly IConnectableObservable<ConcurrentDictionary<SecurityKey, SecurityMD>> _securities_src;
readonly Co2RelayControl _relay;
readonly UserTuple _user;
readonly List<IDisposable> _resources = new();
readonly IUserDataPermissions userDataPermissions;
State _state = State.Uninitialized;
object _lock = new();
public MDSource(TimeSpan relay_throttle)
: this(UserTuple.admin, relay_throttle, TimeSpan.FromSeconds(1)) { }
: this(UserDataPermissions.Create(UserTuple.admin), relay_throttle) { }
public MDSource(IUserDataPermissions userDataPermissions, TimeSpan? relayThrottle = null, TimeSpan? outputThrottle = null)
: this(userDataPermissions, relayThrottle ?? TimeSpan.FromSeconds(0.1), outputThrottle ?? TimeSpan.FromSeconds(1)) { }
public MDSource(
UserTuple user, TimeSpan relay_throttle, TimeSpan output_throttle) {
_user = user;
IUserDataPermissions userDataPermissions, TimeSpan relay_throttle, TimeSpan output_throttle) {
this.userDataPermissions = userDataPermissions;
_relay = new Co2RelayControl(consumer, relay_throttle);
_relay.preload_securities((co2.MDRequest.Types.Flags)0xff).ignore();
@@ -58,7 +62,7 @@ namespace bts.md {
bool sec_filter(SecurityAndHandle sec) =>
sec.security.ContractCase != Security.ContractOneofCase.Synthetic ||
_user.can_access_user_data(sec.security.Synthetic.UserId);
userDataPermissions.Allow(sec.security.Synthetic.UserId);
}
enum State {
@@ -0,0 +1,9 @@
namespace Spark.Api.Services;
using bts.api.core;
using System.Threading.Tasks;
public interface ISparkdLoginService {
ValueTask<LoginResult> LoginAsync();
}
@@ -0,0 +1,5 @@
namespace Spark.Api.Services;
public interface IUserDataPermissions {
bool Allow(uint userId);
}
@@ -5,6 +5,8 @@ using bts.api.admin;
using bts.api.core;
using bts.api.orderman;
using bts.api.position;
using bts.co2;
using bts.md;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@@ -31,8 +33,8 @@ public static class SparkApiServices {
Console.WriteLine("SparkApi services have been disabled");
return services;
}
if (!services.Any(s => s.ServiceType == typeof(SparkService))) {
/// <see cref="SparkService"/> initializes configuration and logging (this should be done
if (!services.Any(s => s.ServiceType == typeof(ISparkConfigLoader))) {
/// <see cref="SparkConfigLoader"/> initializes configuration and logging (this should be done
/// before any api-related services run).
throw new InvalidOperationException("Must add SparkService first");
}
@@ -41,7 +43,9 @@ public static class SparkApiServices {
.AddSingleton<ServiceContext>()
.AddSingleton<Api.IServiceProvider, ServiceContext>(sp => sp.GetRequiredService<ServiceContext>())
.AddSingleton<SparkdLoginService>()
.AddHostedService(sp => sp.GetRequiredService<SparkdLoginService>());
.AddSingleton<ISparkdLoginService, SparkdLoginService>(sp => sp.GetRequiredService<SparkdLoginService>())
.AddHostedService(sp => sp.GetRequiredService<SparkdLoginService>())
.AddSingleton<SecurityResolver>();
if (configurationSection.GetValue("EnableOrders", true)) {
services.AddSparkdService<OrderService>();
services.TryAddSingleton<SecurityResolver>();
@@ -50,6 +54,12 @@ public static class SparkApiServices {
if (configurationSection.GetValue("EnablePosition", true)) {
services.AddSparkdService<PositionService>();
}
if (configurationSection.GetValue("EnableMarketData", true)) {
services.AddSparkdService<MarketDataRelay>()
.AddSingleton<IUserDataPermissions, UserDataPermissions>()
.AddSingleton<MDSource>()
.AddSingleton<MarketDataClient>();
}
if (configurationSection.GetValue("EnableAdmin", true)) {
services.AddSparkdService<AdminService>();
services.AddSparkdService<AdminClient>();
@@ -7,30 +7,55 @@ using bts.api.core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
///
/// An <see cref="IHostedService"/> that logs into Sparkd. Login is only attemped once. After that,
/// subsequent calls to <see cref="LoginAsync"/> simply return the result of the initial attempt.
/// </summary>
public class SparkdLoginService : IHostedService {
public class SparkdLoginService : IHostedService, ISparkdLoginService {
readonly ServiceContext serviceContext;
readonly SparkApiCredentials config;
readonly object lockObj = new();
LoginResult? loginResult;
Task<LoginResult>? loginTask;
public SparkdLoginService(ServiceContext serviceContext, IOptions<SparkApiCredentials> config) {
public SparkdLoginService(ServiceContext serviceContext, IOptions<SparkApiCredentials> config)
: this(serviceContext, config.Value) { }
public SparkdLoginService(ServiceContext serviceContext, SparkApiCredentials config) {
this.serviceContext = serviceContext;
this.config = config.Value;
this.config = config;
}
public ValueTask<LoginResult> LoginAsync() {
if (loginResult is not null) {
return ValueTask.FromResult(loginResult);
}
if (string.IsNullOrEmpty(config.User)) {
return ValueTask.FromException<LoginResult>(new InvalidOperationException("User is null/empty"));
}
lock (lockObj) {
if (loginResult is not null) {
return ValueTask.FromResult(loginResult);
}
loginTask ??= LoginImplAsync();
return new(loginTask);
}
async Task<LoginResult> LoginImplAsync() {
loginResult = await serviceContext.login(config.User ?? string.Empty, config.Password ?? string.Empty);
if (!loginResult.success) {
Log.Gen.error($"Failed to login to sparkd. reason: {loginResult.reason}");
}
return loginResult;
}
}
public Task StartAsync(CancellationToken cancellationToken) =>
string.IsNullOrEmpty(config.User)
? Task.CompletedTask
: Task.Run(() => {
var res = serviceContext.login(config.User, config.Password ?? string.Empty).GetAwaiter().GetResult();
if (!res.success) {
Log.Gen.error("Failed to login to sparkd.");
}
}, cancellationToken);
LoginAsync().AsTask();
public Task StopAsync(CancellationToken cancellationToken) =>
Task.CompletedTask;
@@ -0,0 +1,48 @@
#nullable enable
namespace Spark.Api.Services;
using bts;
using bts.api.core;
using System.Diagnostics.CodeAnalysis;
public class UserDataPermissions : IUserDataPermissions {
readonly object lockObj = new();
readonly ISparkdLoginService? sparkdLoginService;
UserTuple? user;
public UserDataPermissions(ISparkdLoginService sparkdLoginService) {
this.sparkdLoginService = sparkdLoginService;
}
UserDataPermissions(UserTuple user) {
this.user = user;
}
public static UserDataPermissions Create(UserTuple user) =>
new(user);
public bool Allow(uint userId) {
if (!user.HasValue) {
blockForUser();
}
return user.Value.id == userId || user.Value.permissions == UserTuple.Permissions.Admin;
}
[MemberNotNull(nameof(user))]
void blockForUser() {
Log.Gen.warn("UserDataPermissions waiting for login (blocked).");
var loginRes = Login();
Log.Gen.warn($"UserDataPermissions received login result (unblocked). {loginRes}");
Log.assert(loginRes.success);
Log.notnull(loginRes.user);
user = loginRes.user;
LoginResult Login() {
Log.notnull(sparkdLoginService);
lock (lockObj) {
return sparkdLoginService.LoginAsync().AsTask().GetAwaiter().GetResult();
}
}
}
}
@@ -0,0 +1,35 @@
#nullable enable
namespace Spark.Core;
using bts.api.core;
using System;
sealed class ManagedQuickbusWrapper : IDisposable {
readonly object lockObj = new();
IntPtr handle;
public ManagedQuickbusWrapper()
: this(ApiNativeMethods.api_create_quickbus(null, 0)) {}
internal ManagedQuickbusWrapper(IntPtr handle) =>
this.handle = handle;
public static ManagedQuickbusWrapper None => new(IntPtr.Zero);
public IntPtr Handle => handle;
public bool IsDisposed => handle == IntPtr.Zero;
public void Dispose() {
if (IsDisposed) {
return;
}
lock (lockObj) {
if (handle != IntPtr.Zero) {
ApiNativeMethods.api_destroy_quickbus(handle);
handle = IntPtr.Zero;
}
}
}
}
+61 -24
View File
@@ -2,36 +2,59 @@
namespace bts.api.core {
using bts.api.user;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using Spark.Core.Services;
using Spark.Core;
using System.Threading;
public sealed class ServiceContext : Spark.Api.IServiceProvider, IDisposable {
IntPtr _qbus;
readonly object lockObj = new();
LazyOnce<ManagedQuickbusWrapper>? quickbusBacking;
bool isDisposed;
AuthToken? _token;
public ServiceContext() {
_qbus = ApiNativeMethods.api_create_quickbus(null, 0);
public ServiceContext()
: this(new SparkConfigLoader(SparkConfigLoaderOpts.Default())) { }
public ServiceContext(ISparkConfigLoader configLoader) {
quickbusBacking = new(QuickbusFactory, lockObj);
ManagedQuickbusWrapper QuickbusFactory() {
if (isDisposed) {
return ManagedQuickbusWrapper.None;
}
configLoader.InitializeAsync(CancellationToken.None).Wait();
return new();
}
}
internal ServiceContext(IntPtr qbus) =>
_qbus = qbus;
internal ServiceContext(IntPtr qbus) {
quickbusBacking = new(QuickbusFactory, lockObj);
ManagedQuickbusWrapper QuickbusFactory() {
// This code is executed in a lock (since we passed 'lockObj' in to the constructor), so
// no need to worry about concurrency when reading 'isDisposed'.
Log.assert(!isDisposed);
if (isDisposed) {
}
return new(qbus);
}
}
public AuthTokenImmutable? token => _token?.immutable();
public UserTuple? user { get; private set; }
public bool is_authenticated => _token is not null;
internal IntPtr native_context => _qbus;
internal IntPtr native_context => quickbusBacking?.Value?.Handle ?? throw new ObjectDisposedException(nameof(ServiceContext));
public static ServiceContext create(string? config_server = null) {
BTS.init(config_server);
var qbus = ApiNativeMethods.api_create_quickbus(null, 0);
return new ServiceContext(qbus);
}
public static ServiceContext create(string? configSource) =>
new(new SparkConfigLoader(new SparkConfigLoaderOpts() { ConfigServer = configSource }));
public T locate<T>(string qualifier = "") where T : class, IApiClient {
var ctr = typeof(T).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
@@ -71,12 +94,12 @@ namespace bts.api.core {
if (result.value.IsValid) {
_token = result.value.Token;
user = new(result.value.UserId, username);
return new LoginResult(result.value.Reason, true);
return new() { reason = result.value.Reason, success = true, user = user };
} else {
return new LoginResult(result.value.Reason);
return new() { reason = result.value.Reason };
}
} else {
return new LoginResult(result.error_message);
return new() { reason = result.error_message };
}
}
}
@@ -87,9 +110,25 @@ namespace bts.api.core {
}
public void Dispose() {
if (_qbus != IntPtr.Zero) {
ApiNativeMethods.api_destroy_quickbus(_qbus);
_qbus = IntPtr.Zero;
if (isDisposed) {
return;
}
ManagedQuickbusWrapper? oldQuickbus;
lock (lockObj) {
if (isDisposed) {
return;
}
isDisposed = true;
if (quickbusBacking?.IsValueCreated ?? false) {
// IMPORTANT: Don't access quickbusBacking.Value unless you are SURE that the value
// has already been created (otherwise the factory function will deadlock, since it
// also locks 'lockObj').
oldQuickbus = quickbusBacking.Value;
quickbusBacking.Value.Dispose();
}
// Set quickbusBacking to null to prevent the quickbus from being created after dispose
// has been called (if it hasn't been created already).
quickbusBacking = null;
}
}
@@ -101,10 +140,8 @@ namespace bts.api.core {
}
public class LoginResult {
public LoginResult() { success = true; }
public LoginResult(string reason) { success = false; this.reason = reason; }
public LoginResult(string reason, bool success) { this.success = success; this.reason = reason; }
public bool success { get; }
public string? reason { get; }
public bool success { get; init; }
public string? reason { get; init; }
public UserTuple? user { get; init; }
}
}
+7 -3
View File
@@ -10,6 +10,8 @@
using CommandLine;
using Spark.Api.Server;
using Spark.Api.Services;
using Spark.Core.Services;
using System;
using System.Reactive.Linq;
@@ -39,11 +41,13 @@
Log.notnull(opts.user);
Log.notnull(opts.password);
PubSubConfig.SetGlobalConfig(Log.notnull(opts.PubSub));
var service_ctx = ServiceContext.create(opts.sparkd);
await service_ctx.login(opts.user, opts.password);
var serviceCtx = new ServiceContext(new SparkConfigLoader(new SparkConfigLoaderOpts() { ConfigServer = opts.sparkd } ));
var loginService = new SparkdLoginService(serviceCtx, new SparkApiCredentials() { User = opts.user, Password = opts.password });
await loginService.LoginAsync();
PrimitiveManager.SetMarketDataClient(new MarketDataClient(new(TimeSpan.FromSeconds(0.5))));
var sync = new bts.websocket_core.WebsocketDataSync(service_ctx);
var userDataPermissions = new UserDataPermissions(loginService);
var sync = new bts.websocket_core.WebsocketDataSync(serviceCtx, userDataPermissions);
var gwy = new bts.websocket_core.WebsocketConnectionGateway();
var prms = new WebsocketServerConfig {
DefaultEndPoint = "/",
+9 -2
View File
@@ -8,10 +8,17 @@ namespace Spark.Core {
/// </summary>
public class LazyOnce<T> {
readonly Func<T> _create;
readonly object syncRoot;
Optional<T> _value;
public LazyOnce(Func<T> create) =>
public LazyOnce(Func<T> create)
: this(create, new()) { }
public LazyOnce(Func<T> create, object syncRoot) {
_create = create;
ArgumentNullException.ThrowIfNull(syncRoot);
this.syncRoot = syncRoot;
}
public bool IsValueCreated => _value.has_value;
@@ -19,7 +26,7 @@ namespace Spark.Core {
get {
// check has_value outside of lock since, once set to true, it doesn't change
if (!_value.has_value) {
lock (_create) {
lock (syncRoot) {
// check has_value again (may have become true since first check)
if (!_value.has_value) {
_value = _create();
@@ -0,0 +1,12 @@
#nullable enable
namespace Spark.Core.Services;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Initializes Spark protobuf config.
/// </summary>
public interface ISparkConfigLoader {
Task InitializeAsync(CancellationToken cancellationToken);
}
@@ -1,6 +0,0 @@
#nullable enable
namespace Spark.Core.Services;
public class SparkConfig {
public string? ConfigServer { get; set; }
}
@@ -0,0 +1,27 @@
#nullable enable
namespace Spark.Core.Services;
using bts;
using mbts.config;
using Microsoft.Extensions.Options;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Initializes Spark protobuf config.
/// </summary>
public class SparkConfigLoader : ISparkConfigLoader {
readonly SparkConfigLoaderOpts config;
public SparkConfigLoader(IOptions<SparkConfigLoaderOpts> config)
: this(config.Value) { }
public SparkConfigLoader(SparkConfigLoaderOpts config) =>
this.config = config;
public async Task InitializeAsync(CancellationToken cancellationToken) =>
Log.assert(await Configuration.InitializeAsync(config.ConfigServer, cancellationToken));
}
@@ -0,0 +1,8 @@
#nullable enable
namespace Spark.Core.Services;
public class SparkConfigLoaderOpts {
public string? ConfigServer { get; set; }
public static SparkConfigLoaderOpts Default() => new();
}
@@ -0,0 +1,25 @@
#nullable enable
namespace Spark.Core.Services;
using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// An <see cref="IHostedService"/> facade that initializes Spark protobuf config, logging, and
/// quickbus for both c# and cpp.
/// </summary>
public class SparkConfigLoaderService : IHostedService {
readonly ISparkConfigLoader loader;
public SparkConfigLoaderService(ISparkConfigLoader loader) {
this.loader = loader;
}
public Task StartAsync(CancellationToken cancellationToken) =>
loader.InitializeAsync(cancellationToken);
public Task StopAsync(CancellationToken cancellationToken) =>
Task.CompletedTask;
}
@@ -8,11 +8,11 @@ using System.Linq;
public static class SparkCoreServices {
public static IServiceCollection AddSpark(this IServiceCollection services, IConfiguration configurationSection) {
if (services.Any(s => s.ServiceType == typeof(SparkService))) {
if (services.Any(s => s.ServiceType == typeof(ISparkConfigLoader))) {
throw new InvalidOperationException("SparkService has already been added");
}
return services.Configure<SparkConfig>(configurationSection)
.AddSingleton<SparkService>()
.AddHostedService(sp => sp.GetRequiredService<SparkService>());
return services.Configure<SparkConfigLoaderOpts>(configurationSection)
.AddSingleton<ISparkConfigLoader, SparkConfigLoader>()
.AddHostedService<SparkConfigLoaderService>();
}
}
@@ -1,32 +0,0 @@
#nullable enable
namespace Spark.Core.Services;
using mbts.config;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// An <see cref="IHostedService"/> facade that initializes Spark protobuf config, logging, and
/// quickbus for both c# and cpp.
/// </summary>
public class SparkService : IHostedService {
readonly SparkConfig config;
public SparkService(IOptions<SparkConfig> config)
: this(config.Value) { }
public SparkService(SparkConfig config) =>
this.config = config;
public Task StartAsync(CancellationToken cancellationToken) {
Configuration.initialize(config.ConfigServer);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) =>
Task.CompletedTask;
}
@@ -0,0 +1,27 @@
#nullable enable
namespace Spark.Core.Services;
using bts;
using mbts.config;
using Microsoft.Extensions.Options;
using System.Threading;
using System.Threading.Tasks;
/// <summary>
/// Initializes Spark protobuf config.
/// </summary>
class SparkTestConfigLoader : ISparkConfigLoader {
readonly SparkTestConfigLoaderOpts config;
public SparkTestConfigLoader(IOptions<SparkTestConfigLoaderOpts> config)
: this(config.Value) { }
public SparkTestConfigLoader(SparkTestConfigLoaderOpts config) =>
this.config = config;
public async Task InitializeAsync(CancellationToken cancellationToken) =>
Log.assert(await Configuration.ResetForTestAsync(string.Empty, cancellationToken));
}
@@ -0,0 +1,8 @@
#nullable enable
namespace Spark.Core.Services;
public class SparkTestConfigLoaderOpts {
public bool IsUnitTest { get; set; }
public static SparkTestConfigLoaderOpts Default() => new();
}
+99 -15
View File
@@ -9,9 +9,12 @@ namespace mbts.config {
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
public static class Configuration {
static object _config_lock = new();
static TaskCompletionSource<bool>? initTcs;
static State _state = State.Uninitialized;
@@ -50,19 +53,90 @@ namespace mbts.config {
}
}
public static void initialize(string? config_source = null) {
if (_state == State.OK) {
return;
public static ValueTask<bool> InitializeAsync(string? configSource = null) =>
InitializeAsync(configSource, CancellationToken.None);
public static ValueTask<bool> InitializeAsync(CancellationToken cancellationToken) =>
InitializeAsync(null, cancellationToken);
public static ValueTask<bool> InitializeAsync(string? config_source, CancellationToken cancellationToken) {
return _state switch {
State.OK => ValueTask.FromResult(true),
State.Error => ValueTask.FromResult(false),
State.Uninitialized => Initialize(),
_ => throw new InvalidOperationException(),
};
ValueTask<bool> Initialize() {
lock (_config_lock) {
switch (_state) {
case State.OK:
return ValueTask.FromResult(true);
case State.Error:
return ValueTask.FromResult(false);
case State.Uninitialized:
if (initTcs is not null) {
return new(initTcs.Task);
}
initTcs = new(cancellationToken);
break;
default:
throw new InvalidOperationException();
}
}
// Set 'initTcs' to null at the end so subsequent calls to InitializeAsync re-run BTS.init,
// rather than simply return the same completed task.
// Note: if BTS.init is successful, it won't rerun (see the switch case above).
var task = initTcs.Task;
try {
BTS.init(config_source);
// Guarentee BTS.init runs BEFORE we signal initialization is complete (this code won't
// run concurrently, so a full lock isn't necessary).
Thread.MemoryBarrier();
_state = State.OK;
initTcs.SetResult(true);
} catch (Exception ex) {
if (ex is ApplicationException) {
lock (_config_lock) {
_state = State.Error;
initTcs.SetException(ex);
initTcs = null;
}
}
return new(false);
}
initTcs = null;
return new(task);
}
// Considered doing this async on a background thread but it wouldn't do any good. The next
// guy down the line to ask for config will just block until this completes.
try {
BTS.init(config_source);
} catch (ApplicationException) {
_state = State.Error;
throw;
}
public static Task<bool> ResetForTestAsync() =>
ResetForTestAsync(string.Empty, CancellationToken.None);
public static Task<bool> ResetForTestAsync(string config) =>
ResetForTestAsync(config, CancellationToken.None);
public static Task<bool> ResetForTestAsync(CancellationToken cancellationToken) =>
ResetForTestAsync(string.Empty, cancellationToken);
public static Task<bool> ResetForTestAsync(string config, CancellationToken cancellationToken) {
return Task.Run(ResetForTest, cancellationToken);
bool ResetForTest() {
lock (_config_lock) {
_raw_initialize_for_test(config);
// still need init_spark_lib
try {
BTS.init();
} catch (Exception ex) {
Console.Error.WriteLine($"Exception occurred resetting config for test: {ex}");
_state = State.Error;
throw;
}
_state = State.OK;
}
return true;
}
_state = State.OK;
}
public static void initialize_for_test(string config = "") {
@@ -72,7 +146,7 @@ namespace mbts.config {
_state = State.OK;
}
public static unsafe bool try_initialize(out string error) {
public static bool try_initialize(out string error) {
_reset();
try {
BTS.init();
@@ -103,12 +177,22 @@ namespace mbts.config {
$"Failed to deserialize config '{key}' as {type}: {error}";
static byte[]? _get_buffer(string key_name) {
switch (_state) {
case State.Uninitialized:
if (!InitializeAsync().GetAwaiter().GetResult()) {
return null;
}
break;
case State.Error:
return null;
case State.OK:
break;
default:
throw new InvalidOperationException($"Unexpected state: {_state}");
}
/// The native get_buffer (<see cref="_raw_get_buffer(string, ref int)"/>) is not thread
/// safe, so this call must be protected from re-entrancy.
lock (_config_lock) {
if (_state == State.Uninitialized) { initialize(); }
if (_state == State.Error) { return null; }
int length = 0;
byte[] buffer;
IntPtr pbuffer = _raw_get_buffer(key_name, ref length);
+1 -1
View File
@@ -54,7 +54,7 @@ namespace bts {
}
[return: NotNull]
public static T notnull<T>([NotNull] T? obj, string msg = "") where T : class {
public static T notnull<T>([NotNull] T? obj, string msg = "") {
if (obj is null) {
assert(false, msg);
throw new InvalidOperationException("Assertion failed: expected obj to be non-null");
@@ -0,0 +1,11 @@
{
"Spark": {
"ConfigServer": ""
},
"SparkApi": {
"Credentials": {
"User": "bts",
"Password": ""
}
}
}
+40
View File
@@ -0,0 +1,40 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Spark.Core.Services;
using Spark.Api.Services;
using System;
using bts.sdk;
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(ConfigureApp)
.ConfigureServices(ConfigureServices)
.Build();
try {
Console.Write($"Starting Application");
host.Run();
Console.Write($"Application exited gracefully.");
} catch (Exception ex) {
Console.Error.Write($"Application crashed: {ex}");
return 1;
}
return 0;
static void ConfigureApp(HostBuilderContext context, IConfigurationBuilder configBuilder) {
configBuilder.AddCommandLine(Environment.GetCommandLineArgs());
var cfgRoot = configBuilder.Build();
var cfg = cfgRoot["config"];
if (cfg is not null) {
configBuilder.AddJsonFile(cfg, false);
}
}
static void ConfigureServices(HostBuilderContext context, IServiceCollection services) {
/// Bind configuration json fields to strong types. This allows us to add services with
/// constructors that accept these types as arguments.
services.AddSpark(context.Configuration.GetSection("Spark"))
.AddSparkApi(context.Configuration.GetSection("SparkApi"))
.AddHostedService<SdkSampleService>();
}
+10 -9
View File
@@ -25,19 +25,20 @@ namespace bts.sdk
readonly List<IDisposable> _disps = new List<IDisposable>();
IReadOnlyDictionary<uint, SecurityImmutable> _securities =
new Dictionary<uint, SecurityImmutable>(0);
readonly Lazy<OrderService> _orders;
readonly Lazy<AdminService> _admin;
readonly OrderService _orders;
readonly AdminService _admin;
public OrderSample(
ServiceContext serviceContext,
OrdersClient orders_client,
OrderService orders,
AdminService admin,
MarketDataClient md_client,
IObservable<IReadOnlyDictionary<uint, SecurityImmutable>> securities_src
) {
_orders_client = orders_client;
_md = md_client;
_orders = new(() => serviceContext.locate<OrderService>(), true);
_admin = new(() => serviceContext.locate<AdminService>(), true);
_orders = orders;
_admin = admin;
_disps.Add(securities_src.Subscribe(secs => _securities = secs));
}
@@ -76,16 +77,16 @@ namespace bts.sdk
return null;
}
cr.NewOrder.SecurityKey = sec.Value.Id;
Response resp = _orders.Value.create_order(cr);
Response resp = _orders.create_order(cr);
print_update(resp);
//subscribei_ subscribes to a stream of Immutable (in this case FillImmutable) which is nice if you know you don't want to change the fills, which you dont'
_disps.Add(_orders.Value.subscribei_fills().Subscribe(x =>
_disps.Add(_orders.subscribei_fills().Subscribe(x =>
{
foreach(var xx in x.added)
print_update(xx);
}));
_disps.Add(_orders.Value.subscribei_orders().Subscribe(x =>
_disps.Add(_orders.subscribei_orders().Subscribe(x =>
{
foreach (var xx in x.removed)
print_update(xx);
@@ -96,7 +97,7 @@ namespace bts.sdk
}
void order_setup(Create cr, string username, string accountname) =>
order_setup(cr, username, accountname, _admin.Value.get_admin_data(new VoidMessage()));
order_setup(cr, username, accountname, _admin.get_admin_data(new VoidMessage()));
static void order_setup(Create cr, string username, string accountname, AdminData admin)
{
-168
View File
@@ -1,168 +0,0 @@
namespace bts.sdk {
using bts.api.admin;
using bts.api.core;
using bts.api.orderman;
using bts.api.position;
using bts.co2;
using bts.md;
using Spark.Api.Admin;
using Spark.Api.Orders;
using Spark.Api.Reactive;
using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
public class SDKMain
{
private static void user_test(ServiceContext ctx)
{
var s = ctx.locate<api.user.AuthService>();
var r = new api.user.GetUsersRequest();
var resp = s.get_users(r);
Console.WriteLine("{0}", resp);
}
public static void Main(string[] args)
{
BTS.init();
Log.Sdk.info("Welcome to the BTS Client Side SDK example");
var ctx = new ServiceContext();
string u = args.FirstOrDefault(x => x.Contains("--user="));
string p = args.FirstOrDefault(x => x.Contains("--pass="));
u = u?.Replace("--user=", string.Empty) ?? string.Empty;
p = p?.Replace("--pass=", string.Empty) ?? string.Empty;
var lr = ctx.login(u,p).Result;
if (!lr.success)
{
Console.WriteLine("Login failure: " + lr.reason);
help();
}
if (args.FirstOrDefault() == "users")
{
user_test(ctx);
}
else
{
uint ms = 100;
string pu = args.FirstOrDefault(x => x.Contains("--pump="));
if (pu != null)
ms = uint.Parse(p.Replace("--pump=", string.Empty));
var order_service = ctx.locate<OrderService>();
var position_service = ctx.locate<PositionService>();
var admin_service = ctx.locate<AdminService>();
var md_relay = ctx.locate<MarketDataRelay>();
var md_source = new MDSource(TimeSpan.FromSeconds(0.1));
var md_client = new MarketDataClient(md_source);
var admin_client = new AdminClient(admin_service, UserTuple.admin);
var sec_resolver = new SecurityResolver(md_relay, md_source.subscribe_securities());
var orders_client = new OrdersClient(
order_service, position_service, admin_client, sec_resolver);
using(var disp = new CompositeDisposable(md_source)) {
if (args.Length > 0 && args.FirstOrDefault().Contains("md"))
{
var s = new MarketDataSample(md_client);
if (args.FirstOrDefault() == "md")
{
if (args.Length >= 5)
{
s.print_security(args[1], args[2], args[3], args[4]);
}
else if (args.Length >= 4)
{
s.print_product(args[1], args[2], args[3]);
}
else
md_help();
}
else
md_help();
}
else if (args.FirstOrDefault() == "order")
{
var os = new OrderSample(
ctx,
orders_client,
md_client,
md_source.subscribe_securities().Select(sec_map =>
sec_map.ToDictionary(kv => kv.Key, kv => kv.Value.security)));
disp.Add(os);
order_sample(os, u, args);
}
else
help();
wait_for_stop();
}
}
}
static void order_sample(OrderSample os, string user, string[] args)
{
if (args.Length >= 10) //--user and --pass are in args[]
{
os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6], args[7]);
}
else if (args.Length >= 9)
{
os.send_order(user, args[1], args[2], args[3], args[4], args[5], args[6]);
}
else
order_help();
}
static void wait_for_stop()
{
Console.WriteLine("Hit q to stop");
ConsoleKeyInfo c;
do
{
c = Console.ReadKey();
Console.WriteLine();
} while (c.KeyChar != 'q');
}
static void help()
{
Console.WriteLine("BTS Spark SDK");
Console.WriteLine("Usages: spark_sdk [command] [arguments]");
Console.WriteLine();
Console.WriteLine("Commands:");
Console.WriteLine(" md <exchange> <security_type> <symbol> [<security_desc>]");
Console.WriteLine(" order <account> <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>]");
Console.WriteLine("Arguments:");
Console.WriteLine(" --user=<bts user name>");
Console.WriteLine(" --pass=<password>");
Console.WriteLine(" --pump=<pump_interval_in_ms=100 default>");
Environment.Exit(1);
}
static void order_help()
{
Console.WriteLine("order <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>] [<user>] [<account>]");
Console.WriteLine("This command sends a limit or market DAY order for the specified security_desc. Leave off the price for a market order");
Console.WriteLine(" example: order \"ACC A1\" GLBX Future ES ESM8 10 2650.0");
Console.WriteLine(" order \"ACC A1\" SIM Future ES SIM:ESM8 -10 2650.0");
Console.WriteLine(" order \"ACC A1\" GLBX Future ES ESM8 10 (this is a market order)");
Environment.Exit(1);
}
static void md_help()
{
Console.WriteLine("md <exchange> <security_type> <symbol>");
Console.WriteLine("This command subscribes to market data for an exchange-type-symbol channel and writes to console.");
Console.WriteLine("Marketdata is read off the wire on the same thread as is used to read and print");
Console.WriteLine(" example: md GLBX Future ES --user=bts --pass=password");
Console.WriteLine(" md SIM Future ES SIM:ESZ8 --user=bts --pass=password");
Console.WriteLine(" md GLBX Spread ES --user=bts --pass=password");
Environment.Exit(1);
}
}
}
@@ -0,0 +1,29 @@
#nullable enable
namespace Spark.Sdk;
using bts.api;
using Spark.Api.Services;
public class SdkSampleConfig {
public SparkApiCredentials? Credentials { get; set; }
public SdkMarketDataSampleConfig? MarketData { get; set; }
public SdkOrderSampleConfig? Order { get; set; }
}
public class SdkMarketDataSampleConfig {
public string? Exchange { get; set; }
public string? SecurityType { get; set; }
public string? Symbol { get; set; }
public string? SecurityDesc { get; set; }
}
public class SdkOrderSampleConfig {
public string? Exchange { get; set; }
public string? SecurityType { get; set; }
public string? Symbol { get; set; }
public string? SecurityDesc { get; set; }
public string? Account { get; set; }
public string? Quantity { get; set; }
public string? Price { get; set; }
}
@@ -0,0 +1,176 @@
namespace bts.sdk {
using bts.api.admin;
using bts.api.core;
using bts.api.orderman;
using bts.api.position;
using bts.co2;
using bts.md;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Spark.Api.Admin;
using Spark.Api.Orders;
using Spark.Api.Reactive;
using Spark.Api.Services;
using Spark.Core.Services;
using Spark.Sdk;
using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
public class SdkSampleService : IHostedService {
readonly ISparkConfigLoader sparkConfigLoader;
readonly SdkSampleConfig sdkConfig;
readonly Spark.Api.IServiceProvider sparkServiceProvider;
readonly IUserDataPermissions userDataPermissions;
readonly ISparkdLoginService loginService;
OrderService orderService;
OrdersClient ordersClient;
PositionService positionService;
AdminService adminService;
AdminClient adminClient;
MarketDataRelay mdRelay;
public SdkSampleService(
ISparkConfigLoader sparkConfigLoader,
ISparkdLoginService loginService,
IUserDataPermissions userDataPermissions,
Spark.Api.IServiceProvider sparkServiceProvider,
OrderService orderService,
OrdersClient ordersClient,
PositionService positionService,
AdminService adminService,
AdminClient adminClient,
MarketDataRelay mdRelay,
IOptions<SdkSampleConfig> sdkConfig)
: this(sparkConfigLoader, loginService, userDataPermissions, sparkServiceProvider, orderService, ordersClient, positionService, adminService, adminClient, mdRelay, sdkConfig.Value) { }
public SdkSampleService(
ISparkConfigLoader sparkConfigLoader,
ISparkdLoginService loginService,
IUserDataPermissions userDataPermissions,
Spark.Api.IServiceProvider sparkServiceProvider,
OrderService orderService,
OrdersClient ordersClient,
PositionService positionService,
AdminService adminService,
AdminClient adminClient,
MarketDataRelay mdRelay,
SdkSampleConfig sdkConfig) {
this.sparkConfigLoader = sparkConfigLoader;
this.loginService = loginService;
this.sparkServiceProvider = sparkServiceProvider;
this.userDataPermissions = userDataPermissions;
this.orderService = orderService;
this.ordersClient = ordersClient;
this.positionService = positionService;
this.adminService = adminService;
this.adminClient = adminClient;
this.mdRelay = mdRelay;
this.sdkConfig = sdkConfig;
}
UserTuple user {
get {
var loginTask = loginService.LoginAsync();
if (!loginTask.IsCompletedSuccessfully) {
return loginTask.AsTask().GetAwaiter().GetResult().user.Value;
}
return loginTask.Result.user.Value;
}
}
private static void user_test(ServiceContext ctx) {
var s = ctx.locate<api.user.AuthService>();
var r = new api.user.GetUsersRequest();
var resp = s.get_users(r);
Console.WriteLine("{0}", resp);
}
public async Task StartAsync(CancellationToken cancellationToken) {
BTS.init();
Log.Sdk.info("Welcome to the BTS Client Side SDK example");
await loginService.LoginAsync();
var md_source = new MDSource(userDataPermissions);
var md_client = new MarketDataClient(md_source);
using (var disp = new CompositeDisposable(md_source)) {
if (sdkConfig.MarketData is not null) {
var s = new MarketDataSample(md_client);
var mdCfg = sdkConfig.MarketData;
s.print_security(mdCfg.Exchange, mdCfg.SecurityDesc, mdCfg.Symbol, mdCfg.SecurityDesc);
} else if (sdkConfig.Order is not null) {
var os = new OrderSample(
ordersClient,
orderService,
adminService,
md_client,
md_source.subscribe_securities().Select(sec_map =>
sec_map.ToDictionary(kv => kv.Key, kv => kv.Value.security)));
disp.Add(os);
order_sample(os, user.name, sdkConfig.Order);
} else
help();
wait_for_stop();
}
}
public Task StopAsync(CancellationToken cancellationToken) =>
Task.CompletedTask;
static void order_sample(OrderSample os, string user, SdkOrderSampleConfig cfg) {
os.send_order(user, cfg.Account, cfg.Exchange, cfg.SecurityType, cfg.Symbol, cfg.SecurityDesc, cfg.Quantity, cfg.Price);
}
static void wait_for_stop() {
Console.WriteLine("Hit q to stop");
ConsoleKeyInfo c;
do {
c = Console.ReadKey();
Console.WriteLine();
} while (c.KeyChar != 'q');
}
static void help() {
Console.WriteLine("BTS Spark SDK");
Console.WriteLine("Usages: spark_sdk [command] [arguments]");
Console.WriteLine();
Console.WriteLine("Commands:");
Console.WriteLine(" md <exchange> <security_type> <symbol> [<security_desc>]");
Console.WriteLine(" order <account> <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>]");
Console.WriteLine("Arguments:");
Console.WriteLine(" --user=<bts user name>");
Console.WriteLine(" --pass=<password>");
Console.WriteLine(" --pump=<pump_interval_in_ms=100 default>");
Environment.Exit(1);
}
static void order_help() {
Console.WriteLine("order <exchange> <security_type> <symbol> <security_desc> <signed_quantity> [<price>] [<user>] [<account>]");
Console.WriteLine("This command sends a limit or market DAY order for the specified security_desc. Leave off the price for a market order");
Console.WriteLine(" example: order \"ACC A1\" GLBX Future ES ESM8 10 2650.0");
Console.WriteLine(" order \"ACC A1\" SIM Future ES SIM:ESM8 -10 2650.0");
Console.WriteLine(" order \"ACC A1\" GLBX Future ES ESM8 10 (this is a market order)");
Environment.Exit(1);
}
static void md_help() {
Console.WriteLine("md <exchange> <security_type> <symbol>");
Console.WriteLine("This command subscribes to market data for an exchange-type-symbol channel and writes to console.");
Console.WriteLine("Marketdata is read off the wire on the same thread as is used to read and print");
Console.WriteLine(" example: md GLBX Future ES --user=bts --pass=password");
Console.WriteLine(" md SIM Future ES SIM:ESZ8 --user=bts --pass=password");
Console.WriteLine(" md GLBX Spread ES --user=bts --pass=password");
Environment.Exit(1);
}
}
}
+12 -13
View File
@@ -16,27 +16,30 @@ using bts.co2;
using bts.tests.utils;
using Spark.Core.Disposables;
using Spark.Api.Services;
namespace bts.tests
{
[TestGroup(TestType = TestType.System)]
public class LimitsTests : IDisposable {
ServiceContext _context;
OrderService _order_service;
AdminService _admin_service;
ISparkdLoginService loginService;
MarketDataRelay mdRelay;
IDisposable _order_subscription;
Update<OrderRecordImmutable, uint> _orders;
public LimitsTests (){}
public LimitsTests(OrderService orderService, AdminService adminService, ISparkdLoginService loginService, MarketDataRelay mdRelay) {
_order_service = orderService;
_admin_service = adminService;
this.loginService = loginService;
this.mdRelay = mdRelay;
}
private async Task setup_limits_tests() {
await TestUtils.wait_for_system_ready();
_context = new ServiceContext();
_order_service = _context.locate<OrderService>();
_admin_service = _context.locate<AdminService>();
var auth = _context.login("bts","");
auth.Wait(3000);
loginService.LoginAsync().AsTask().Wait(3000);
_order_subscription = _order_service.subscribei_orders().Subscribe(ords =>
{
@@ -46,9 +49,6 @@ namespace bts.tests
}
public void Dispose() {
Disposable.dispose(ref _order_service);
Disposable.dispose(ref _admin_service);
Disposable.dispose(ref _context);
Disposable.dispose(ref _order_subscription);
}
@@ -68,9 +68,8 @@ namespace bts.tests
}
};
var relay = _context.locate<MarketDataRelay>();
await Task.Delay(100);
var result = await relay.requestmd_async(request);
var result = await mdRelay.requestmd_async(request);
List<SecurityImmutable> securities = new List<SecurityImmutable>();
if (result.success) {
@@ -1,21 +1,23 @@
namespace bts.websocket_core {
using bts.api.admin;
using bts.api.core;
using bts.api.orderman;
using bts.api.position;
using bts.api.synthetic;
using bts.api;
using bts.co2;
using bts.md;
using mbts;
using Newtonsoft.Json;
using System.Collections.Concurrent;
using bts.api.core;
using bts.api.orderman;
using bts.api.admin;
using bts.co2;
using Spark.Api.Reactive;
using Spark.Api.Orders;
using Spark.Api.Admin;
using Spark.Api.MarketData;
using bts.api.position;
using bts.api.synthetic;
using Spark.Api.Orders;
using Spark.Api.Reactive;
using Spark.Api.Services;
using System.Collections.Concurrent;
/// <summary>
/// This does all the bookkeeping for connections and data, and passes
@@ -24,9 +26,11 @@ namespace bts.websocket_core {
public class WebsocketDataSync : IWebsocketDataSync {
readonly AdminService adminService;
readonly ServiceContext _service_ctx;
readonly IUserDataPermissions userDataPermissions;
public WebsocketDataSync(ServiceContext service_ctx) {
public WebsocketDataSync(ServiceContext service_ctx, IUserDataPermissions userDataPermissions) {
_service_ctx = service_ctx;
this.userDataPermissions = userDataPermissions;
adminService = service_ctx.locate<AdminService>();
}
@@ -193,8 +197,7 @@ namespace bts.websocket_core {
public async Task initAsync(WebsocketConnectionGateway gtwy) {
Gateway = gtwy;
clients = new ClientsStore();
TimeSpan md_throttle = TimeSpan.FromSeconds(0.1);
MD_Source = new MDSource(md_throttle);
MD_Source = new MDSource(userDataPermissions);
Md_Client = new(MD_Source);
_securities_sub = MD_Source.subscribe_securities().Subscribe(sec_map => _securities = sec_map);
var order_service = _service_ctx.locate<OrderService>();