Skip to content

Commit

Permalink
multi: resolve review issues (5 of x).
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed Jun 6, 2020
1 parent bd13112 commit 8a23ff7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 49 deletions.
16 changes: 11 additions & 5 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,13 @@ func handleTradeSuspensionMsg(c *Core, dc *dexConnection, msg *msgjson.Message)

// Remove pending suspends for the market.
if sched := dc.pendingSuspends[sp.MarketID]; sched != nil {
sched.Stop()
if !sched.Stop() {
// TODO: too late, timer already fired. Need to request the
// current configuration for the market at this point.
return fmt.Errorf("unable to stop previously scheduled "+
"suspend for market %s on dex %s", sp.MarketID, dc.acct.host)
}

delete(dc.pendingSuspends, sp.MarketID)
}

Expand All @@ -2513,12 +2519,12 @@ func handleTradeSuspensionMsg(c *Core, dc *dexConnection, msg *msgjson.Message)
return
}

// Clear the order book associated with the suspended market.
dc.booksMtx.RLock()
// Clear the bookie associated with the suspended market.
dc.booksMtx.Lock()
if bookie := dc.books[sp.MarketID]; bookie != nil {
bookie.Reset()
dc.books[sp.MarketID] = newBookie(func() { c.unsub(dc, sp.MarketID) })
}
dc.booksMtx.RUnlock()
dc.booksMtx.Unlock()

if !sp.Persist {
// Revoke all active orders of the suspended market for the dex.
Expand Down
47 changes: 3 additions & 44 deletions client/order/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ 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 @@ -181,10 +180,7 @@ 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 @@ -236,11 +232,7 @@ 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 {
ob.idMtx.Lock()
marketID := ob.marketID
ob.idMtx.Unlock()

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

Expand Down Expand Up @@ -295,11 +287,7 @@ 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 {
ob.idMtx.Lock()
marketID := ob.marketID
ob.idMtx.Unlock()

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

Expand Down Expand Up @@ -341,11 +329,7 @@ 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 {
ob.idMtx.Lock()
marketID := ob.marketID
ob.idMtx.Unlock()

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

Expand Down Expand Up @@ -453,31 +437,6 @@ func (ob *OrderBook) ResetEpoch() {
ob.epochQueue.Reset()
}

// Reset clears the orderbook and its associated epoch queue. This should be
// called when a trade suspension does not persist the orderbook.
func (ob *OrderBook) Reset() {
ob.seqMtx.Lock()
ob.seq = 0
ob.seqMtx.Unlock()

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

ob.noteQueueMtx.Lock()
ob.noteQueue = make([]*cachedOrderNote, 0)
ob.noteQueueMtx.Unlock()

ob.ordersMtx.Lock()
ob.orders = make(map[order.OrderID]*Order)
ob.ordersMtx.Unlock()

ob.buys.Reset()
ob.sells.Reset()

ob.ResetEpoch()
}

// Enqueue appends the provided order note to the orderbook's epoch queue.
func (ob *OrderBook) Enqueue(note *msgjson.EpochOrderNote) error {
ob.setSeq(note.Seq)
Expand Down

0 comments on commit 8a23ff7

Please sign in to comment.