From 1abec0ab39b706ac7ea3fa6fab334c21a5331211 Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Tue, 8 Jan 2019 16:32:57 -0500 Subject: [PATCH] Attempt to defer initial security publish until loading is complete so initial load is not bogged down by ticking security feed --- src/client/spark/Daemons/MDDaemon.cs | 12 +++++++----- src/client/spark_api/md/MDSource.cs | 24 ++++++++++++++++-------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/client/spark/Daemons/MDDaemon.cs b/src/client/spark/Daemons/MDDaemon.cs index 816faac4a..4231dd0f9 100644 --- a/src/client/spark/Daemons/MDDaemon.cs +++ b/src/client/spark/Daemons/MDDaemon.cs @@ -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>, IInitializable, IDisposable { + class MDDaemon : md.MDSource, IObserver>, IInitializable, IDisposable { readonly Lazy _bus; readonly Lazy _token; readonly Lazy _ss; readonly Dictionary _deleted_synthetics = new Dictionary(); - static uint get_naggle() => mbts.config.Configuration.get().naggle_ms; + static uint get_naggle() => mbts.config.Configuration.get().naggle_ms; [ImportingConstructor] - public MDDaemon(Lazy bus, Lazy token, Lazy ss) : base(get_naggle(), true) { + public MDDaemon(Lazy bus, Lazy token, Lazy 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 diff --git a/src/client/spark_api/md/MDSource.cs b/src/client/spark_api/md/MDSource.cs index 9fdebb63b..d8559e799 100644 --- a/src/client/spark_api/md/MDSource.cs +++ b/src/client/spark_api/md/MDSource.cs @@ -15,20 +15,27 @@ using System.Reactive.Subjects; namespace bts.md { + [Flags] + enum MDSourceOptions { + DeferInit = 1, + DeferPublish = 2 + } class MDSource : IObserver>, IDisposable { protected readonly MDConsumer _consumer = new MDConsumer(); protected readonly Co2RelayControl _relay; - protected long _version = 0; + protected DateTime _version = DateTime.MinValue; protected readonly ConcurrentDictionary _exchange_sec_map = new ConcurrentDictionary(); protected List _dispose = new List(); + 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