ProcessOrderbook: added a condition to check for existing orderbooks (#197)

* ProcessOrderbook: added a condition to check for existing orderbooks

* ProcessOrderbook: simplified the logic by removing useless code

* Covered orderbook read/write stability with tests

* Fixes race condition writing to a test array
This commit is contained in:
Andrey Grehov
2018-10-30 20:24:15 -04:00
committed by Adrian Gallagher
parent 4a879bf1a1
commit 640a7e6a83
2 changed files with 70 additions and 15 deletions

View File

@@ -166,11 +166,6 @@ func ProcessOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew Bas
orderbookNew.CurrencyPair = p.Pair().String()
orderbookNew.LastUpdated = time.Now()
if len(Orderbooks) == 0 {
CreateNewOrderbook(exchangeName, p, orderbookNew, orderbookType)
return
}
orderbook, err := GetOrderbookByExchange(exchangeName)
if err != nil {
CreateNewOrderbook(exchangeName, p, orderbookNew, orderbookType)
@@ -178,16 +173,12 @@ func ProcessOrderbook(exchangeName string, p pair.CurrencyPair, orderbookNew Bas
}
if FirstCurrencyExists(exchangeName, p.FirstCurrency) {
if !SecondCurrencyExists(exchangeName, p) {
m.Lock()
a := orderbook.Orderbook[p.FirstCurrency]
b := make(map[string]Base)
b[orderbookType] = orderbookNew
a[p.SecondCurrency] = b
orderbook.Orderbook[p.FirstCurrency] = a
m.Unlock()
return
}
m.Lock()
a := make(map[string]Base)
a[orderbookType] = orderbookNew
orderbook.Orderbook[p.FirstCurrency][p.SecondCurrency] = a
m.Unlock()
return
}
m.Lock()