Skip to content

Commit

Permalink
perf: getAssetsData fetch async
Browse files Browse the repository at this point in the history
  • Loading branch information
Thykof committed Jun 5, 2024
1 parent b4eec42 commit f860d36
Showing 1 changed file with 32 additions and 10 deletions.
42 changes: 32 additions & 10 deletions internal/handler/wallet/get_all_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wallet
import (
"fmt"
"sort"
"sync"

"github.com/go-openapi/runtime/middleware"
"github.com/massalabs/station-massa-wallet/api/server/models"
Expand Down Expand Up @@ -111,21 +112,42 @@ func (g *getAllAssets) getAssetsData(acc *account.Account) ([]*assets.AssetInfoW
assetsInfo := g.AssetsStore.All(acc.Nickname)

assetsWithBalance := make([]*assets.AssetInfoWithBalances, 0)
var wg sync.WaitGroup
mu := &sync.Mutex{}
resultsCh := make(chan *assets.AssetInfoWithBalances)

// Retrieve all assets from the selected nickname
for _, asset := range assetsInfo {
// First, check if the asset exists in the network
if !g.massaClient.AssetExistInNetwork(asset.AssetInfo.Address) {
logger.Infof("Asset %s does not exist in the network", asset.AssetInfo.Address)
continue
}
wg.Add(1)

go func(asset *assets.AssetInfoWithBalances) {
defer wg.Done()

// First, check if the asset exists in the network
if !g.massaClient.AssetExistInNetwork(asset.AssetInfo.Address) {
logger.Infof("Asset %s does not exist in the network", asset.AssetInfo.Address)
return
}

// Fetch the balance for the current asset
balance, dollarValue := g.fetchAssetData(asset, acc)

asset.Balance = balance
asset.DollarValue = dollarValue

resultsCh <- asset
}(asset)
}

// Fetch the balance for the current asset
balance, dollarValue := g.fetchAssetData(asset, acc)
go func() {
wg.Wait()
close(resultsCh)
}()

asset.Balance = balance
asset.DollarValue = dollarValue
assetsWithBalance = append(assetsWithBalance, asset)
for result := range resultsCh {
mu.Lock()
assetsWithBalance = append(assetsWithBalance, result)
mu.Unlock()
}

return assetsWithBalance, nil
Expand Down

0 comments on commit f860d36

Please sign in to comment.