orderbook: fix race condition when function scope is in error (#1357)

* orderbook: fix race condition when in error

* Whoops
This commit is contained in:
Ryan O'Hara-Reid
2023-10-03 16:08:31 +11:00
committed by GitHub
parent ecf0a5bcb3
commit 033a72b61a

View File

@@ -94,6 +94,8 @@ 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,
@@ -101,7 +103,6 @@ func (d *Depth) LoadSnapshot(bids, asks []Item, lastUpdateID int64, lastUpdated
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
d.lastUpdateID = lastUpdateID
d.lastUpdated = lastUpdated
d.restSnapshot = updateByREST
@@ -109,7 +110,6 @@ func (d *Depth) LoadSnapshot(bids, asks []Item, lastUpdateID int64, lastUpdated
d.asks.load(asks, d.stack, lastUpdated)
d.validationError = nil
d.Alert()
d.m.Unlock()
return nil
}
@@ -149,6 +149,8 @@ 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,
@@ -156,7 +158,6 @@ func (d *Depth) UpdateBidAskByPrice(update *Update) error {
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
if len(update.Bids) != 0 {
d.bids.updateInsertByPrice(update.Bids, d.stack, d.options.maxDepth, update.UpdateTime)
}
@@ -164,12 +165,14 @@ func (d *Depth) UpdateBidAskByPrice(update *Update) error {
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,
@@ -177,8 +180,7 @@ func (d *Depth) UpdateBidAskByID(update *Update) error {
d.asset,
errLastUpdatedNotSet)
}
d.m.Lock()
defer d.m.Unlock()
if len(update.Bids) != 0 {
err := d.bids.updateByID(update.Bids)
if err != nil {
@@ -197,6 +199,8 @@ 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,
@@ -204,8 +208,6 @@ func (d *Depth) DeleteBidAskByID(update *Update, bypassErr bool) error {
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 {
@@ -224,6 +226,8 @@ 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,
@@ -231,8 +235,6 @@ func (d *Depth) InsertBidAskByID(update *Update) error {
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 {
@@ -251,6 +253,8 @@ 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,
@@ -258,8 +262,6 @@ func (d *Depth) UpdateInsertByID(update *Update) error {
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 {