Skip to content

Commit

Permalink
orderbook: fix race condition when function scope is in error (thrash…
Browse files Browse the repository at this point in the history
…er-corp#1357)

* orderbook: fix race condition when in error

* Whoops
  • Loading branch information
shazbert authored Oct 3, 2023
1 parent ecf0a5b commit 033a72b
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions exchanges/orderbook/depth.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,22 @@ func (d *Depth) Retrieve() (*Base, error) {

// LoadSnapshot flushes the bids and asks with a snapshot
func (d *Depth) LoadSnapshot(bids, asks []Item, lastUpdateID int64, lastUpdated time.Time, updateByREST bool) error {
d.m.Lock()
defer d.m.Unlock()
if lastUpdated.IsZero() {
return fmt.Errorf("%s %s %s %w",
d.exchange,
d.pair,
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
d.lastUpdateID = lastUpdateID
d.lastUpdated = lastUpdated
d.restSnapshot = updateByREST
d.bids.load(bids, d.stack, lastUpdated)
d.asks.load(asks, d.stack, lastUpdated)
d.validationError = nil
d.Alert()
d.m.Unlock()
return nil
}

Expand Down Expand Up @@ -149,36 +149,38 @@ func (d *Depth) IsValid() bool {
// UpdateBidAskByPrice updates the bid and ask spread by supplied updates, this
// will trim total length of depth level to a specified supplied number
func (d *Depth) UpdateBidAskByPrice(update *Update) error {
d.m.Lock()
defer d.m.Unlock()
if update.UpdateTime.IsZero() {
return fmt.Errorf("%s %s %s %w",
d.exchange,
d.pair,
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
if len(update.Bids) != 0 {
d.bids.updateInsertByPrice(update.Bids, d.stack, d.options.maxDepth, update.UpdateTime)
}
if len(update.Asks) != 0 {
d.asks.updateInsertByPrice(update.Asks, d.stack, d.options.maxDepth, update.UpdateTime)
}
d.updateAndAlert(update)
d.m.Unlock()
return nil
}

// UpdateBidAskByID amends details by ID
func (d *Depth) UpdateBidAskByID(update *Update) error {
d.m.Lock()
defer d.m.Unlock()

if update.UpdateTime.IsZero() {
return fmt.Errorf("%s %s %s %w",
d.exchange,
d.pair,
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
defer d.m.Unlock()

if len(update.Bids) != 0 {
err := d.bids.updateByID(update.Bids)
if err != nil {
Expand All @@ -197,15 +199,15 @@ func (d *Depth) UpdateBidAskByID(update *Update) error {

// DeleteBidAskByID deletes a price level by ID
func (d *Depth) DeleteBidAskByID(update *Update, bypassErr bool) error {
d.m.Lock()
defer d.m.Unlock()
if update.UpdateTime.IsZero() {
return fmt.Errorf("%s %s %s %w",
d.exchange,
d.pair,
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
defer d.m.Unlock()
if len(update.Bids) != 0 {
err := d.bids.deleteByID(update.Bids, d.stack, bypassErr, update.UpdateTime)
if err != nil {
Expand All @@ -224,15 +226,15 @@ func (d *Depth) DeleteBidAskByID(update *Update, bypassErr bool) error {

// InsertBidAskByID inserts new updates
func (d *Depth) InsertBidAskByID(update *Update) error {
d.m.Lock()
defer d.m.Unlock()
if update.UpdateTime.IsZero() {
return fmt.Errorf("%s %s %s %w",
d.exchange,
d.pair,
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
defer d.m.Unlock()
if len(update.Bids) != 0 {
err := d.bids.insertUpdates(update.Bids, d.stack)
if err != nil {
Expand All @@ -251,15 +253,15 @@ func (d *Depth) InsertBidAskByID(update *Update) error {

// UpdateInsertByID updates or inserts by ID at current price level.
func (d *Depth) UpdateInsertByID(update *Update) error {
d.m.Lock()
defer d.m.Unlock()
if update.UpdateTime.IsZero() {
return fmt.Errorf("%s %s %s %w",
d.exchange,
d.pair,
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
defer d.m.Unlock()
if len(update.Bids) != 0 {
err := d.bids.updateInsertByID(update.Bids, d.stack)
if err != nil {
Expand Down

0 comments on commit 033a72b

Please sign in to comment.