Skip to content

Commit

Permalink
dont call RecordCancel under the bookMtx
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Jul 28, 2023
1 parent 2dbb4ac commit f2c34c9
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions server/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,16 @@ func (m *Market) processReadyEpoch(epoch *readyEpoch, notifyChan chan<- *updateS
cSum := epoch.cSum
misses := epoch.misses

// We can't call RecordCancel under the bookMtx since it can potentially
// trigger a user suspension and unbooking via UnbookUserOrders, which locks
// the bookMtx. So we'll track the info necessary to call RecordCancel and
// call them after the matches loop.
type cancelMatch struct {
co *order.CancelOrder
loEpoch int64
}
var cancels = make([]cancelMatch, 0)

// Perform order matching using the preimages to shuffle the queue.
m.bookMtx.Lock() // allow a coherent view of book orders with (*Market).Book
matchTime := time.Now() // considered as the time at which matched cancel orders are executed
Expand All @@ -2397,9 +2407,10 @@ func (m *Market) processReadyEpoch(epoch *readyEpoch, notifyChan chan<- *updateS
// Update order settling amounts.
for _, match := range ms.Matches() {
if co, ok := match.Taker.(*order.CancelOrder); ok {
epochGap := int32((co.ServerTime.UnixMilli() / epochDur) - (match.Maker.ServerTime.UnixMilli() / epochDur))
m.auth.RecordCancel(co.User(), co.ID(), co.TargetOrderID, epochGap, matchTime)
canceled = append(canceled, co.TargetOrderID)
cancels = append(cancels, cancelMatch{
co: co,
loEpoch: match.Maker.ServerTime.UnixMilli() / epochDur,
})
continue
}
m.settling[match.Taker.ID()] += match.Quantity
Expand All @@ -2413,6 +2424,13 @@ func (m *Market) processReadyEpoch(epoch *readyEpoch, notifyChan chan<- *updateS
}
m.bookMtx.Unlock()

for _, c := range cancels {
co, loEpoch := c.co, c.loEpoch
epochGap := int32((co.ServerTime.UnixMilli() / epochDur) - loEpoch)
m.auth.RecordCancel(co.User(), co.ID(), co.TargetOrderID, epochGap, matchTime)
canceled = append(canceled, co.TargetOrderID)
}

if len(ordersRevealed) > 0 {
log.Infof("Matching complete for market %v epoch %d:"+
" %d matches (%d partial fills), %d completed OK (not booked),"+
Expand Down

0 comments on commit f2c34c9

Please sign in to comment.