Concurrent rpc balances queries

pull/1211/head
flicker-harmony 5 years ago
parent c63dc3251b
commit d12c5434f7
  1. 19
      cmd/client/wallet/main.go

@ -797,10 +797,17 @@ func FetchBalance(address common.Address) []*AccountState {
result[uint32(shardID)] = &AccountState{balance, 0} result[uint32(shardID)] = &AccountState{balance, 0}
LOOP: var wgShard sync.WaitGroup
for j := 0; j < len(walletProfile.RPCServer[shardID]); j++ { wgShard.Add(len(walletProfile.RPCServer[shardID]))
var mutexAccountState = &sync.Mutex{}
for rpcServerID := 0; rpcServerID < len(walletProfile.RPCServer[shardID]); rpcServerID++ {
go func(rpcServerID int) {
defer wgShard.Done()
log.Debug("Launched goroutiune");
for retry := 0; retry < rpcRetry; retry++ { for retry := 0; retry < rpcRetry; retry++ {
server := walletProfile.RPCServer[shardID][j] server := walletProfile.RPCServer[shardID][rpcServerID]
client, err := clientService.NewClient(server.IP, server.Port) client, err := clientService.NewClient(server.IP, server.Port)
if err != nil { if err != nil {
continue continue
@ -816,13 +823,17 @@ func FetchBalance(address common.Address) []*AccountState {
log.Debug("FetchBalance", "response", response) log.Debug("FetchBalance", "response", response)
respBalance := big.NewInt(0) respBalance := big.NewInt(0)
respBalance.SetBytes(response.Balance) respBalance.SetBytes(response.Balance)
mutexAccountState.Lock()
if balance.Cmp(respBalance) < 0 { if balance.Cmp(respBalance) < 0 {
balance.SetBytes(response.Balance) balance.SetBytes(response.Balance)
nonce = response.Nonce nonce = response.Nonce
} }
break LOOP mutexAccountState.Unlock()
} }
}(rpcServerID)
} }
wgShard.Wait()
result[shardID] = &AccountState{balance, nonce} result[shardID] = &AccountState{balance, nonce}
}(shardID) }(shardID)
} }

Loading…
Cancel
Save