Attempt to defer initial security publish until loading is complete so initial load is not bogged down by ticking security feed
This commit is contained in:
1 parent
aea38c1689
commit
1abec0ab39
2 files changed
+23
-13
No files matched your search
@@ -14,19 +14,21 @@ using SecurityKey = System.UInt32;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using bts.api.user;
|
||||
using bts.md;
|
||||
|
||||
namespace bts {
|
||||
[Export(typeof(IInitializable))]
|
||||
class MDDaemon : bts.md.MDSource, IObserver<List<SecurityAndHandle>>, IInitializable, IDisposable {
|
||||
class MDDaemon : md.MDSource, IObserver<List<SecurityAndHandle>>, IInitializable, IDisposable {
|
||||
readonly Lazy<IBus> _bus;
|
||||
readonly Lazy<AuthTokenImmutable> _token;
|
||||
readonly Lazy<SyntheticSecurityService> _ss;
|
||||
readonly Dictionary<SecurityKey, SecurityMD> _deleted_synthetics = new Dictionary<SecurityKey, SecurityMD>();
|
||||
|
||||
static uint get_naggle() => mbts.config.Configuration.get<bts.md.RelayOptions>().naggle_ms;
|
||||
static uint get_naggle() => mbts.config.Configuration.get<md.RelayOptions>().naggle_ms;
|
||||
|
||||
[ImportingConstructor]
|
||||
public MDDaemon(Lazy<IBus> bus, Lazy<AuthTokenImmutable> token, Lazy<SyntheticSecurityService> ss) : base(get_naggle(), true) {
|
||||
public MDDaemon(Lazy<IBus> bus, Lazy<AuthTokenImmutable> token, Lazy<SyntheticSecurityService> ss) :
|
||||
base(get_naggle(), MDSourceOptions.DeferInit | MDSourceOptions.DeferPublish) {
|
||||
_bus = bus;
|
||||
_token = token;
|
||||
_ss = ss;
|
||||
@@ -44,14 +46,14 @@ namespace bts {
|
||||
if (!deleted_synthetic_keys.Contains(kvp.Key)) {
|
||||
_exchange_sec_map.TryAdd(kvp.Key, kvp.Value);
|
||||
_deleted_synthetics.Remove(kvp.Key);
|
||||
_version += 1;
|
||||
_version = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
// Remove any newly deleted synthetics and store them for potential undelete
|
||||
foreach (var key in deleted_synthetic_keys) {
|
||||
if (_exchange_sec_map.TryRemove(key, out SecurityMD deleted)) {
|
||||
_deleted_synthetics[key] = deleted;
|
||||
_version += 1;
|
||||
_version = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
// Post full dictionary of syntheics here
|
||||
|
||||
@@ -15,20 +15,27 @@ using System.Reactive.Subjects;
|
||||
|
||||
namespace bts.md
|
||||
{
|
||||
[Flags]
|
||||
enum MDSourceOptions {
|
||||
DeferInit = 1,
|
||||
DeferPublish = 2
|
||||
}
|
||||
class MDSource : IObserver<List<SecurityAndHandle>>, IDisposable
|
||||
{
|
||||
protected readonly MDConsumer _consumer = new MDConsumer();
|
||||
protected readonly Co2RelayControl _relay;
|
||||
protected long _version = 0;
|
||||
protected DateTime _version = DateTime.MinValue;
|
||||
protected readonly ConcurrentDictionary<SecurityKey, SecurityMD> _exchange_sec_map = new ConcurrentDictionary<SecurityKey, SecurityMD>();
|
||||
protected List<IDisposable> _dispose = new List<IDisposable>();
|
||||
protected readonly MDSourceOptions _options;
|
||||
|
||||
public MDSource(uint naggle_ms, bool defer_init = false)
|
||||
{
|
||||
public MDSource(uint naggle_ms, MDSourceOptions options = 0)
|
||||
{
|
||||
_options = options;
|
||||
_relay = new Co2RelayControl(_consumer, naggle_ms);
|
||||
_relay.preload_securities((co2.MDRequest.Types.Flags)0xff).ignore();
|
||||
_dispose.Add(_consumer.on_security.Subscribe(this));
|
||||
if(!defer_init)
|
||||
if(options.HasFlag(MDSourceOptions.DeferInit))
|
||||
initialize();
|
||||
}
|
||||
|
||||
@@ -57,19 +64,20 @@ namespace bts.md
|
||||
var smd = new SecurityMD(new SecurityImmutable(v.security), v.handle);
|
||||
_exchange_sec_map.AddOrUpdate(smd.security.Id, smd, (_, __) => smd);
|
||||
}
|
||||
_version += 1;
|
||||
_version = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() => Disposable.dispose(_dispose);
|
||||
async protected Task run()
|
||||
{
|
||||
long run_version = 0;
|
||||
DateTime run_version = DateTime.MinValue;
|
||||
var minimum_change_delta = _options.HasFlag(MDSourceOptions.DeferPublish) ? TimeSpan.FromMilliseconds(200) : TimeSpan.Zero;
|
||||
while (true)
|
||||
{
|
||||
var exch = _exchange_sec_map;
|
||||
long current_version = _version;
|
||||
if (current_version > run_version)
|
||||
var current_version = _version;
|
||||
if (current_version - run_version > minimum_change_delta)
|
||||
{
|
||||
run_version = current_version;
|
||||
try
|
||||
|
||||
Reference in new issue
Block a user