diff --git a/src/client/spark/Notifications/INotificationService.cs b/src/client/spark/Notifications/INotificationService.cs index b43008de4..c60326499 100644 --- a/src/client/spark/Notifications/INotificationService.cs +++ b/src/client/spark/Notifications/INotificationService.cs @@ -121,7 +121,7 @@ namespace bts.shell { void _on_orders(TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> update) { // must update notification_service_data_cache first, some of the predicate/triggers (esp. ToastNotifications) will use the notification_service_data_cache to lookup order-history - _data_cache.update(update.as_iincremental()); + _data_cache.update(update); foreach (var o in update.added.Concat(update.changed)) _order_notifications.process_item(o); } diff --git a/src/client/spark/Notifications/INotificationServiceDataCache.cs b/src/client/spark/Notifications/INotificationServiceDataCache.cs index 6467ccb79..bc901570a 100644 --- a/src/client/spark/Notifications/INotificationServiceDataCache.cs +++ b/src/client/spark/Notifications/INotificationServiceDataCache.cs @@ -1,4 +1,5 @@ -using bts.api; +using bts.api; +using bts.api.core; using bts.api.orderman; using mbts.spark; @@ -6,6 +7,7 @@ using mbts.utils; using System; using System.Collections.Generic; +using System.Linq; namespace bts.shell { // Any predicate-sound/notifications that need to reference previous data (i.e. notifications based on change/time, or state changes) should use the INotificationServiceDataCache. @@ -45,40 +47,61 @@ namespace bts.shell { const int _buffer_size = 10; readonly Dictionary _order_cache = new Dictionary(); readonly Dictionary>> _order_history = new Dictionary>>(); + readonly object _lock = new(); public IReadOnlyDictionary>> order_history => _order_history; public bool is_first_order_update { get; private set; } - internal void update(IIncrementalUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> data) { + internal void update(TableUpdate<(SecurityImmutable? sec, OrderRecordImmutable order), uint> table) { + // First, try processing the update per usual (incrementally). If it throws, + // then log the exception and reset the state. DateTime now = DateTime.Now; - if (data.removed.Count == _order_cache.Count) { - _order_cache.Clear(); + lock (_lock) { + try { + impl(); + return; + } catch (Exception ex) { + Log.Ui.error($"Error processing order update in NotificationService: {ex}"); + } + // This is the 'escape' path. It really shouldn't throw. + reset(); } - is_first_order_update = _order_cache.Count == 0 && data.added.Count > 0; - foreach (var kv in data.removed) { - if (data.added.ContainsKey(kv.Key)) { - _order_cache[kv.Key] = kv.Value; - _order_history[kv.Key].Add(new TimeStampedData(kv.Value, now)); - } else { - _order_cache.Remove(kv.Key); - // TODO: maybe these shouldn't be removed? - // if we add delta/time predicates that need to refer to order history, we might need to keep them around - // although, if the server is telling us to remove them, perhaps we shouldn't consider such data. - _order_history.Remove(kv.Key); + + void impl() { + if (table.state.Count == 0) { + _order_cache.Clear(); + } + is_first_order_update = _order_cache.Count == 0 && table.state.Count > 0; + foreach (var order in table.removed) { + var k = table.key(order); + _order_cache.Remove(k); + _order_history.Remove(k); + } + foreach (var ch in table.changes) { + var k = table.key(ch.New); + _order_cache[k] = ch.New; + _order_history[k].Add(new(ch.New, now)); + } + foreach (var order in table.added) { + var k = table.key(order); + _order_cache[k] = order; + var hist = _order_history.GetOrAdd(k, _ => new()); + hist.Add(new(order, now)); + if (hist.Count >= _buffer_size) { + hist.RemoveAt(0); + } } } - foreach (var kv in data.added) { - if (data.removed.ContainsKey(kv.Key)) - continue; - _order_cache.Add(kv.Key, kv.Value); - _order_history.AddOrUpdate(kv.Key, - k => new List> { new TimeStampedData(kv.Value, now) }, - (k, l) => { - l.Add(new TimeStampedData(kv.Value, now)); - if (l.Count >= 10) - l.RemoveAt(0); - }); + + void reset() { + _order_cache.Clear(); + _order_history.Clear(); + foreach (var (k, order) in table.state) { + _order_cache[k] = order; + _order_history.GetOrAdd(k, _ => new()).Add(new(order, now)); + } + is_first_order_update = true; } } }