fix(wallet): fix crash on market price cache concurrent write (#3307)

This commit is contained in:
IvanBelyakoff 2023-03-17 09:33:20 +03:00 committed by GitHub
parent 1453f5a0e3
commit fe4e079bea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 7 deletions

View File

@ -17,12 +17,12 @@ type DataPoint struct {
type DataPerTokenAndCurrency = map[string]map[string]DataPoint
type Manager struct {
main thirdparty.MarketDataProvider
fallback thirdparty.MarketDataProvider
priceCache DataPerTokenAndCurrency
IsConnected bool
LastCheckedAt int64
IsConnectedLock sync.RWMutex
main thirdparty.MarketDataProvider
fallback thirdparty.MarketDataProvider
priceCache DataPerTokenAndCurrency
IsConnected bool
LastCheckedAt int64
priceCacheLock sync.RWMutex
}
func NewManager(main thirdparty.MarketDataProvider, fallback thirdparty.MarketDataProvider) *Manager {
@ -186,6 +186,9 @@ func (pm *Manager) getCachedPricesFor(symbols []string, currencies []string) Dat
}
func (pm *Manager) updatePriceCache(prices map[string]map[string]float64) {
pm.priceCacheLock.Lock()
defer pm.priceCacheLock.Unlock()
for token, pricesPerCurrency := range prices {
_, present := pm.priceCache[token]
if !present {
@ -201,6 +204,9 @@ func (pm *Manager) updatePriceCache(prices map[string]map[string]float64) {
}
func (pm *Manager) GetCachedPrices() DataPerTokenAndCurrency {
pm.priceCacheLock.RLock()
defer pm.priceCacheLock.RUnlock()
return pm.priceCache
}
@ -212,7 +218,7 @@ func (pm *Manager) GetOrFetchPrices(symbols []string, currencies []string, maxAg
now := time.Now().Unix()
for _, symbol := range symbols {
tokenPriceCache, ok := pm.priceCache[symbol]
tokenPriceCache, ok := pm.GetCachedPrices()[symbol]
if !ok {
if !symbolsToFetchMap[symbol] {
symbolsToFetchMap[symbol] = true