From 466327cdaaf42fb39bc25f754ece92a8ed91c38f Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Fri, 9 Jun 2023 02:39:07 +0100 Subject: [PATCH] orders: Fix manager not processing immediately (#1193) * orders: Fix manager not processing immediately This ensures that orders are processed as soon as orderManager claims to have started. Specifically this will fix an eager call to CancelAllOrders not finding anything until after the first Interval run. * orders: Fix reset timer call This fix is small niggle. The existing code was more complicated than it needed to be, whilst also ignoring the return of timer.Reset and the conditional need to drain the channel. It's unlikely but possible during transient resource starvation. * OrderManager: Simplify startup process This simplifies the sequence and Adds to the wg first. We won't bail from the run() during any type of processOrders, and that's something we can fix later --- engine/order_manager.go | 8 ++------ engine/order_manager_types.go | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/engine/order_manager.go b/engine/order_manager.go index 564c1d6e..938853e6 100644 --- a/engine/order_manager.go +++ b/engine/order_manager.go @@ -108,21 +108,17 @@ func (m *OrderManager) gracefulShutdown() { // run will periodically process orders func (m *OrderManager) run() { log.Debugln(log.OrderMgr, "Order manager started.") - timer := time.NewTimer(orderManagerDelay) + m.processOrders() for { select { case <-m.shutdown: m.gracefulShutdown() - if !timer.Stop() { - <-timer.C - } m.orderStore.wg.Done() log.Debugln(log.OrderMgr, "Order manager shutdown.") return - case <-timer.C: + case <-time.After(orderManagerInterval): // Process orders go routine allows shutdown procedures to continue go m.processOrders() - timer.Reset(orderManagerDelay) } } } diff --git a/engine/order_manager_types.go b/engine/order_manager_types.go index dd2029d5..b312dce5 100644 --- a/engine/order_manager_types.go +++ b/engine/order_manager_types.go @@ -24,7 +24,7 @@ var ( errNilCommunicationsManager = errors.New("cannot start with nil communications manager") errNilOrder = errors.New("nil order received") errFuturesTrackingDisabled = errors.New("tracking futures positions disabled. enable it via config under orderManager activelyTrackFuturesPositions") - orderManagerDelay = time.Second * 10 + orderManagerInterval = time.Second * 10 defaultOrderSeekTime = -time.Hour * 24 * 365 )