From 77e31e92742b9ed9cfdc9a749b8e29feeddc6934 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 30 Jun 2023 01:12:10 +0800 Subject: [PATCH] types: split pendingRemoval lock scope --- pkg/types/ordermap.go | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/pkg/types/ordermap.go b/pkg/types/ordermap.go index 358d640a4..bc0939269 100644 --- a/pkg/types/ordermap.go +++ b/pkg/types/ordermap.go @@ -125,26 +125,34 @@ func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) { return exists } -func (m *SyncOrderMap) Add(o Order) { +func (m *SyncOrderMap) processPendingRemoval() { + if len(m.pendingRemoval) == 0 { + return + } + m.Lock() defer m.Unlock() - m.orders.Add(o) - - if len(m.pendingRemoval) > 0 { - expireTime := time.Now().Add(-5 * time.Minute) - removing := make(map[uint64]struct{}) - for orderID, creationTime := range m.pendingRemoval { - if m.orders.Exists(orderID) || creationTime.Before(expireTime) { - m.orders.Remove(orderID) - removing[orderID] = struct{}{} - } - } - - for orderID := range removing { - delete(m.pendingRemoval, orderID) + expireTime := time.Now().Add(-5 * time.Minute) + removing := make(map[uint64]struct{}) + for orderID, creationTime := range m.pendingRemoval { + if m.orders.Exists(orderID) || creationTime.Before(expireTime) { + m.orders.Remove(orderID) + removing[orderID] = struct{}{} } } + + for orderID := range removing { + delete(m.pendingRemoval, orderID) + } +} + +func (m *SyncOrderMap) Add(o Order) { + m.Lock() + m.orders.Add(o) + m.Unlock() + + m.processPendingRemoval() } func (m *SyncOrderMap) Update(o Order) {