SyncManager: Optimise and fixes (#1229)

* SyncManager: Optimise and fixes

This is a fairly invasive change which addresses the amount of work the
sync manager does each cycle and the cycle intervals.
We switch to using discrete locks for each type of work on each pair,
so each worker can take a discrete chunk of work safely.
For performance and simplicity we now use a map for the currencyPairs.

* fix reporting when a websocket is reconnected
* fix not switching REST off after websocket available again
* fix race condition in isProcessing flag

This PR still could go further by avoiding cycling through everything
each time, and by pushing some one-time work for adding enabled pairs
down to a later stage off the hot path.
This was the smallest chunk of refactoring I felt could address
everything without changing too much.

Significant manual testing done with a variety of Timeouts to test for
edgecases and handling.

* SyncManager: Fix ticker/orderbook tracker linked

* SyncManager: Fix sync complete logging in update

* SyncManager: Fix pair format breaking sync key

Kraken seems to always switch to XBT_USDT format, but websockets still
pass around XBTUSDT format. Just to be safe this just removes the
delimiter to avoid any such issues

* SyncManager: Remove unused error

* SyncManager: Remove unused IsProcessing flag

* SyncManager: Fix Update test add() pair format

We had to unify pair format inside sync manager, so test needs to do the
same
This commit is contained in:
Gareth Kirwan
2023-07-12 01:17:39 +01:00
committed by GitHub
parent b403e12d3e
commit 54f745e943
4 changed files with 356 additions and 381 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
)
@@ -283,52 +284,55 @@ func TestSyncManagerUpdate(t *testing.T) {
m.initSyncStarted = 1
// orderbook not enabled
err = m.Update("", currency.EMPTYPAIR, 1, 1, nil)
err = m.Update("", currency.EMPTYPAIR, asset.Spot, SyncItemOrderbook, nil)
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}
m.config.SynchronizeOrderbook = true
// ticker not enabled
err = m.Update("", currency.EMPTYPAIR, 1, 0, nil)
err = m.Update("", currency.EMPTYPAIR, asset.Spot, SyncItemTicker, nil)
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}
m.config.SynchronizeTicker = true
// trades not enabled
err = m.Update("", currency.EMPTYPAIR, 1, 2, nil)
err = m.Update("", currency.EMPTYPAIR, asset.Spot, SyncItemTrade, nil)
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}
m.config.SynchronizeTrades = true
err = m.Update("", currency.EMPTYPAIR, 1, 1336, nil)
err = m.Update("", currency.EMPTYPAIR, asset.Spot, 1336, nil)
if !errors.Is(err, errUnknownSyncItem) {
t.Fatalf("received %v, but expected: %v", err, errUnknownSyncItem)
}
err = m.Update("", currency.EMPTYPAIR, 1, 1, nil)
err = m.Update("", currency.EMPTYPAIR, asset.Spot, SyncItemOrderbook, nil)
if !errors.Is(err, errCouldNotSyncNewData) {
t.Fatalf("received %v, but expected: %v", err, errCouldNotSyncNewData)
}
m.currencyPairs = append(m.currencyPairs, currencyPairSyncAgent{AssetType: 1})
m.add(currencyPairKey{
AssetType: asset.Spot,
Pair: currency.EMPTYPAIR.Format(currency.PairFormat{Uppercase: true}),
}, syncBase{})
m.initSyncWG.Add(3)
// orderbook match
err = m.Update("", currency.EMPTYPAIR, 1, 1, errors.New("test"))
err = m.Update("", currency.EMPTYPAIR, asset.Spot, SyncItemOrderbook, errors.New("test"))
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}
// ticker match
err = m.Update("", currency.EMPTYPAIR, 1, 0, errors.New("test"))
err = m.Update("", currency.EMPTYPAIR, asset.Spot, SyncItemTicker, errors.New("test"))
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}
// trades match
err = m.Update("", currency.EMPTYPAIR, 1, 2, errors.New("test"))
err = m.Update("", currency.EMPTYPAIR, asset.Spot, SyncItemTrade, errors.New("test"))
if !errors.Is(err, nil) {
t.Fatalf("received %v, but expected: %v", err, nil)
}