fix: Add lock, try/catch in notification serv order handler
Brandon has been experiencing a crash on startup. The stack trace shows this handler is throwing a duplicate-key ex on 'dictionary.Add' (unclear why).
This commit is contained in:
1 parent
98eea0ef09
commit
0b35c2ccde
2 files changed
+50
-27
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<uint, OrderSecurity> _order_cache = new Dictionary<uint, OrderSecurity>();
|
||||
readonly Dictionary<uint, List<TimeStampedData<OrderSecurity>>> _order_history = new Dictionary<uint, List<TimeStampedData<OrderSecurity>>>();
|
||||
readonly object _lock = new();
|
||||
|
||||
public IReadOnlyDictionary<uint, List<TimeStampedData<OrderSecurity>>> 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<OrderSecurity>(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<TimeStampedData<OrderSecurity>> { new TimeStampedData<OrderSecurity>(kv.Value, now) },
|
||||
(k, l) => {
|
||||
l.Add(new TimeStampedData<OrderSecurity>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user