mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
Orderbook/RPCServer: Fix GetOrderbook/Retrieve race condition (refactored and sped up) (#555)
* Kraken - wsProcessOrderBook, the method was returning wrong bids data * additional map check to prevent panic * linter issue fix * The RPC method GetOrderbook has a race condition and causes panic - refactored and speed up * The method Retrieve (package orderbook) now return pointer of a copy of s.Books[exchange][p.Base.Item][p.Quote.Item][a].b * using extra var to optomize code * bids and asks slices filling optimisation Co-authored-by: Vazha Bezhanishvili <vazha.bezhanishvili@elegro.eu>
This commit is contained in:
@@ -400,21 +400,27 @@ func (s *RPCServer) GetOrderbook(_ context.Context, r *gctrpc.GetOrderbookReques
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var bids []*gctrpc.OrderbookItem
|
||||
for x := range ob.Bids {
|
||||
bids = append(bids, &gctrpc.OrderbookItem{
|
||||
Amount: ob.Bids[x].Amount,
|
||||
Price: ob.Bids[x].Price,
|
||||
})
|
||||
}
|
||||
bids := make([]*gctrpc.OrderbookItem, 0, len(ob.Bids))
|
||||
asks := make([]*gctrpc.OrderbookItem, 0, len(ob.Asks))
|
||||
ch := make(chan bool)
|
||||
|
||||
var asks []*gctrpc.OrderbookItem
|
||||
for x := range ob.Asks {
|
||||
go func() {
|
||||
for _, b := range ob.Bids {
|
||||
bids = append(bids, &gctrpc.OrderbookItem{
|
||||
Amount: b.Amount,
|
||||
Price: b.Price,
|
||||
})
|
||||
}
|
||||
ch <- true
|
||||
}()
|
||||
|
||||
for _, a := range ob.Asks {
|
||||
asks = append(asks, &gctrpc.OrderbookItem{
|
||||
Amount: ob.Asks[x].Amount,
|
||||
Price: ob.Asks[x].Price,
|
||||
Amount: a.Amount,
|
||||
Price: a.Price,
|
||||
})
|
||||
}
|
||||
<-ch
|
||||
|
||||
resp := &gctrpc.OrderbookResponse{
|
||||
Pair: r.Pair,
|
||||
|
||||
Reference in New Issue
Block a user