Skip to content

Commit

Permalink
multi: resolve review issues (3 of x).
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed May 29, 2020
1 parent f0f934f commit dc969a1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
20 changes: 14 additions & 6 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2406,29 +2406,32 @@ func handleTradeSuspensionMsg(c *Core, dc *dexConnection, msg *msgjson.Message)
// Set the new scheduled suspend.
duration := time.Until(encode.UnixTimeMilli(int64(sp.SuspendTime)))
dc.pendingSuspends[sp.MarketID] = time.AfterFunc(duration, func() {
dc.pendingSuspendsMtx.Lock()
defer dc.pendingSuspendsMtx.Unlock()
dc.suspend(sp.MarketID)

dc.booksMtx.RLock()
if bookie := dc.books[sp.MarketID]; bookie != nil {
bookie.Reset()
}
dc.booksMtx.RUnlock()

if !sp.Persist {
dc.marketMtx.RLock()
mkt, ok := dc.marketMap[sp.MarketID]
if !ok {
log.Errorf("no market found with ID %s", sp.MarketID)
dc.marketMtx.Unlock()
return
}

dc.tradeMtx.Lock()
defer dc.tradeMtx.Unlock()
dc.marketMtx.RUnlock()

// Revoke all active orders of the suspended market for the dex.
dc.tradeMtx.RLock()
for _, tracker := range dc.trades {
if tracker.Order.Base() == mkt.BaseID &&
tracker.Order.Quote() == mkt.QuoteID &&
tracker.metaData.Host == dc.acct.host &&
(tracker.metaData.Status == order.OrderStatusEpoch ||
tracker.metaData.Status == order.OrderStatusBooked) {

md := tracker.metaData
md.Status = order.OrderStatusRevoked
metaOrder := &db.MetaOrder{
Expand All @@ -2442,8 +2445,13 @@ func handleTradeSuspensionMsg(c *Core, dc *dexConnection, msg *msgjson.Message)
}
}
}
dc.tradeMtx.RUnlock()
}

// Remove suspend after execution.
dc.pendingSuspendsMtx.Lock()
delete(dc.pendingSuspends, sp.MarketID)
dc.pendingSuspendsMtx.Unlock()
})

return nil
Expand Down
9 changes: 9 additions & 0 deletions client/order/bookside.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ func (d *bookSide) orders() []*Order {
return orders
}

// Reset clears out the book side.
func (d *bookSide) Reset() {
d.mtx.Lock()
defer d.mtx.Unlock()

d.bins = make(map[uint64][]*Order)
d.rateIndex = NewRateIndex()
}

// BestNOrders returns the best N orders of the book side.
func (d *bookSide) BestNOrders(n int) ([]*Order, bool) {
d.mtx.RLock()
Expand Down
28 changes: 23 additions & 5 deletions client/order/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type cachedOrderNote struct {
type OrderBook struct {
seqMtx sync.Mutex
seq uint64
idMtx sync.Mutex
marketID string
noteQueue []*cachedOrderNote
noteQueueMtx sync.Mutex
Expand Down Expand Up @@ -180,7 +181,10 @@ func (ob *OrderBook) Sync(snapshot *msgjson.OrderBook) error {
ob.seq = snapshot.Seq
ob.seqMtx.Unlock()

ob.idMtx.Lock()
ob.marketID = snapshot.MarketID
ob.idMtx.Unlock()

ob.orders = make(map[order.OrderID]*Order)
ob.buys = NewBookSide(descending)
ob.sells = NewBookSide(ascending)
Expand Down Expand Up @@ -232,7 +236,11 @@ func (ob *OrderBook) Sync(snapshot *msgjson.OrderBook) error {
// book is the workhorse of the exported Book function. It allows booking
// cached and uncached order notes.
func (ob *OrderBook) book(note *msgjson.BookOrderNote, cached bool) error {
if ob.marketID != note.MarketID {
ob.idMtx.Lock()
marketID := ob.marketID
ob.idMtx.Unlock()

if marketID != note.MarketID {
return fmt.Errorf("invalid note market id %s", note.MarketID)
}

Expand Down Expand Up @@ -287,7 +295,11 @@ func (ob *OrderBook) Book(note *msgjson.BookOrderNote) error {
// updateRemaining is the workhorse of the exported UpdateRemaining function. It
// allows updating cached and uncached orders.
func (ob *OrderBook) updateRemaining(note *msgjson.UpdateRemainingNote, cached bool) error {
if ob.marketID != note.MarketID {
ob.idMtx.Lock()
marketID := ob.marketID
ob.idMtx.Unlock()

if marketID != note.MarketID {
return fmt.Errorf("invalid update_remaining note market id %s", note.MarketID)
}

Expand Down Expand Up @@ -329,7 +341,11 @@ func (ob *OrderBook) UpdateRemaining(note *msgjson.UpdateRemainingNote) error {
// unbook is the workhorse of the exported Unbook function. It allows unbooking
// cached and uncached order notes.
func (ob *OrderBook) unbook(note *msgjson.UnbookOrderNote, cached bool) error {
if ob.marketID != note.MarketID {
ob.idMtx.Lock()
marketID := ob.marketID
ob.idMtx.Unlock()

if marketID != note.MarketID {
return fmt.Errorf("invalid note market id %s", note.MarketID)
}

Expand Down Expand Up @@ -444,7 +460,9 @@ func (ob *OrderBook) Reset() {
ob.seq = 0
ob.seqMtx.Unlock()

ob.idMtx.Lock()
ob.marketID = ""
ob.idMtx.Unlock()

ob.noteQueueMtx.Lock()
ob.noteQueue = make([]*cachedOrderNote, 0)
Expand All @@ -454,8 +472,8 @@ func (ob *OrderBook) Reset() {
ob.orders = make(map[order.OrderID]*Order)
ob.ordersMtx.Unlock()

ob.buys = NewBookSide(descending)
ob.sells = NewBookSide(ascending)
ob.buys.Reset()
ob.sells.Reset()

ob.ResetEpoch()
}
Expand Down

0 comments on commit dc969a1

Please sign in to comment.