types: split pendingRemoval lock scope

This commit is contained in:
c9s 2023-06-30 01:12:10 +08:00
parent fc7edc5c80
commit 77e31e9274
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -125,26 +125,34 @@ func (m *SyncOrderMap) Remove(orderID uint64) (exists bool) {
return exists return exists
} }
func (m *SyncOrderMap) Add(o Order) { func (m *SyncOrderMap) processPendingRemoval() {
if len(m.pendingRemoval) == 0 {
return
}
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
m.orders.Add(o) expireTime := time.Now().Add(-5 * time.Minute)
removing := make(map[uint64]struct{})
if len(m.pendingRemoval) > 0 { for orderID, creationTime := range m.pendingRemoval {
expireTime := time.Now().Add(-5 * time.Minute) if m.orders.Exists(orderID) || creationTime.Before(expireTime) {
removing := make(map[uint64]struct{}) m.orders.Remove(orderID)
for orderID, creationTime := range m.pendingRemoval { removing[orderID] = struct{}{}
if m.orders.Exists(orderID) || creationTime.Before(expireTime) {
m.orders.Remove(orderID)
removing[orderID] = struct{}{}
}
}
for orderID := range removing {
delete(m.pendingRemoval, orderID)
} }
} }
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) { func (m *SyncOrderMap) Update(o Order) {