From 22e4da3775cbe53d49fe194fe62e1a167780caca Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 29 Dec 2021 23:53:46 +0800 Subject: [PATCH] fix pendingRemoval lock --- pkg/bbgo/active_book.go | 3 +-- pkg/types/ordermap.go | 12 +++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/bbgo/active_book.go b/pkg/bbgo/active_book.go index e773ad785..c39067be5 100644 --- a/pkg/bbgo/active_book.go +++ b/pkg/bbgo/active_book.go @@ -11,8 +11,7 @@ import ( // LocalActiveOrderBook manages the local active order books. //go:generate callbackgen -type LocalActiveOrderBook type LocalActiveOrderBook struct { - Bids *types.SyncOrderMap - Asks *types.SyncOrderMap + Asks, Bids *types.SyncOrderMap filledCallbacks []func(o types.Order) } diff --git a/pkg/types/ordermap.go b/pkg/types/ordermap.go index 95cd26b56..f36f223ee 100644 --- a/pkg/types/ordermap.go +++ b/pkg/types/ordermap.go @@ -85,21 +85,23 @@ func NewSyncOrderMap() *SyncOrderMap { } } -func (m *SyncOrderMap) Backup() []SubmitOrder { +func (m *SyncOrderMap) Backup() (orders []SubmitOrder) { m.Lock() - defer m.Unlock() - return m.orders.Backup() + orders = m.orders.Backup() + m.Unlock() + return orders } func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) { - exists = m.Exists(orderID) if exists { m.Lock() m.orders.Remove(orderID) m.Unlock() } else { + m.Lock() m.pendingRemoval[orderID] = time.Now() + m.Unlock() } return exists @@ -107,6 +109,7 @@ func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) { func (m *SyncOrderMap) Add(o Order) { m.Lock() + defer m.Unlock() match := false if len(m.pendingRemoval) > 0 { @@ -132,7 +135,6 @@ func (m *SyncOrderMap) Add(o Order) { m.orders.Add(o) } - m.Unlock() } func (m *SyncOrderMap) Update(o Order) {